@theshelf/validation 0.0.4 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -13
- package/dist/Validator.d.ts +8 -0
- package/dist/Validator.js +9 -0
- package/dist/definitions/constants.d.ts +10 -10
- package/dist/definitions/constants.js +0 -1
- package/dist/definitions/interfaces.d.ts +1 -1
- package/dist/drivers/Zod.d.ts +8 -0
- package/dist/{implementations/zod → drivers}/Zod.js +4 -4
- package/dist/errors/ValidationError.js +4 -0
- package/dist/index.d.ts +4 -3
- package/dist/index.js +2 -3
- package/package.json +1 -1
- package/dist/errors/UnknownImplementation.d.ts +0 -4
- package/dist/errors/UnknownImplementation.js +0 -6
- package/dist/implementation.d.ts +0 -3
- package/dist/implementation.js +0 -12
- package/dist/implementations/zod/Zod.d.ts +0 -8
- package/dist/implementations/zod/create.d.ts +0 -2
- package/dist/implementations/zod/create.js +0 -4
package/README.md
CHANGED
|
@@ -9,30 +9,33 @@ The validation package provides a universal interaction layer with an actual dat
|
|
|
9
9
|
npm install @theshelf/validation
|
|
10
10
|
```
|
|
11
11
|
|
|
12
|
-
##
|
|
12
|
+
## Drivers
|
|
13
13
|
|
|
14
|
-
Currently, there is only one
|
|
14
|
+
Currently, there is only one driver available:
|
|
15
15
|
|
|
16
|
-
* **Zod** -
|
|
17
|
-
|
|
18
|
-
## Configuration
|
|
19
|
-
|
|
20
|
-
The used implementation needs to be configured in the `.env` file.
|
|
21
|
-
|
|
22
|
-
```env
|
|
23
|
-
VALIDATION_IMPLEMENTATION="zod"
|
|
24
|
-
```
|
|
16
|
+
* **Zod** - driver for the currently popular Zod library.
|
|
25
17
|
|
|
26
18
|
## How to use
|
|
27
19
|
|
|
28
|
-
|
|
20
|
+
The instance of the validator needs to be imported and one of the drivers must be set.
|
|
29
21
|
|
|
30
22
|
```ts
|
|
31
|
-
import validator from '@theshelf/validation';
|
|
23
|
+
import validator, { Zod as SelectedDriver } from '@theshelf/validation';
|
|
24
|
+
|
|
25
|
+
// Set the driver before performing any operation (the Zod driver is used by default)
|
|
26
|
+
validator.driver = new SelectedDriver(/* configuration */);
|
|
32
27
|
|
|
33
28
|
// Perform operations with the validator instance
|
|
34
29
|
```
|
|
35
30
|
|
|
31
|
+
### Configuration
|
|
32
|
+
|
|
33
|
+
The validator instance does not have any configuration options.
|
|
34
|
+
|
|
35
|
+
#### Zod driver
|
|
36
|
+
|
|
37
|
+
No configuration options.
|
|
38
|
+
|
|
36
39
|
### Operations
|
|
37
40
|
|
|
38
41
|
```ts
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Driver } from './definitions/interfaces.js';
|
|
2
|
+
import type { ValidationSchema } from './definitions/types.js';
|
|
3
|
+
import type ValidationResult from './definitions/ValidationResult.js';
|
|
4
|
+
export default class Validator implements Driver {
|
|
5
|
+
#private;
|
|
6
|
+
constructor(driver: Driver);
|
|
7
|
+
validate(data: unknown, schema: ValidationSchema): ValidationResult;
|
|
8
|
+
}
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
declare const FieldTypes: {
|
|
2
|
-
STRING: string;
|
|
3
|
-
NUMBER:
|
|
4
|
-
BOOLEAN:
|
|
5
|
-
DATE:
|
|
6
|
-
DATETIME:
|
|
7
|
-
UUID:
|
|
8
|
-
EMAIL:
|
|
9
|
-
ARRAY:
|
|
10
|
-
URL:
|
|
11
|
-
ENUM:
|
|
2
|
+
readonly STRING: "string";
|
|
3
|
+
readonly NUMBER: "number";
|
|
4
|
+
readonly BOOLEAN: "boolean";
|
|
5
|
+
readonly DATE: "date";
|
|
6
|
+
readonly DATETIME: "datetime";
|
|
7
|
+
readonly UUID: "uuid";
|
|
8
|
+
readonly EMAIL: "email";
|
|
9
|
+
readonly ARRAY: "array";
|
|
10
|
+
readonly URL: "url";
|
|
11
|
+
readonly ENUM: "enum";
|
|
12
12
|
};
|
|
13
13
|
declare const MAX_EMAIL_LENGTH = 320;
|
|
14
14
|
declare const MAX_URL_LENGTH = 2083;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import ValidationResult from '../definitions/ValidationResult.js';
|
|
2
|
+
import type { Driver } from '../definitions/interfaces.js';
|
|
3
|
+
import type { ValidationSchema } from '../definitions/types.js';
|
|
4
|
+
export default class Zod implements Driver {
|
|
5
|
+
#private;
|
|
6
|
+
constructor();
|
|
7
|
+
validate(data: unknown, schema: ValidationSchema): ValidationResult;
|
|
8
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
-
import ValidationResult from '
|
|
3
|
-
import { FieldTypes, MAX_EMAIL_LENGTH, MAX_URL_LENGTH } from '
|
|
4
|
-
import UnknownValidator from '
|
|
2
|
+
import ValidationResult from '../definitions/ValidationResult.js';
|
|
3
|
+
import { FieldTypes, MAX_EMAIL_LENGTH, MAX_URL_LENGTH } from '../definitions/constants.js';
|
|
4
|
+
import UnknownValidator from '../errors/UnknownValidator.js';
|
|
5
5
|
// Zod is so type heavy that we've chosen for inferred types to be used.
|
|
6
6
|
// This is a trade-off between readability and verbosity.
|
|
7
7
|
export default class Zod {
|
|
@@ -39,7 +39,7 @@ export default class Zod {
|
|
|
39
39
|
for (const [key, validation] of Object.entries(schema)) {
|
|
40
40
|
if (key === 'message')
|
|
41
41
|
continue;
|
|
42
|
-
const validator = this.#validations.get(key.
|
|
42
|
+
const validator = this.#validations.get(key.toLowerCase());
|
|
43
43
|
if (validator === undefined) {
|
|
44
44
|
throw new UnknownValidator(key);
|
|
45
45
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export * from './definitions/constants.js';
|
|
2
|
-
export * from './definitions/
|
|
3
|
-
export
|
|
2
|
+
export type * from './definitions/interfaces.js';
|
|
3
|
+
export type * from './definitions/types.js';
|
|
4
4
|
export { default as UnknownValidator } from './errors/UnknownValidator.js';
|
|
5
5
|
export { default as ValidationError } from './errors/ValidationError.js';
|
|
6
|
-
export { default } from './
|
|
6
|
+
export { default as ZodDriver } from './drivers/Zod.js';
|
|
7
|
+
export { default } from './Validator.js';
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
export * from './definitions/constants.js';
|
|
2
|
-
export * from './definitions/types.js';
|
|
3
|
-
export { default as UnknownImplementation } from './errors/UnknownImplementation.js';
|
|
4
2
|
export { default as UnknownValidator } from './errors/UnknownValidator.js';
|
|
5
3
|
export { default as ValidationError } from './errors/ValidationError.js';
|
|
6
|
-
export { default } from './
|
|
4
|
+
export { default as ZodDriver } from './drivers/Zod.js';
|
|
5
|
+
export { default } from './Validator.js';
|
package/package.json
CHANGED
package/dist/implementation.d.ts
DELETED
package/dist/implementation.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import UnknownImplementation from './errors/UnknownImplementation.js';
|
|
2
|
-
import createZod from './implementations/zod/create.js';
|
|
3
|
-
const implementations = new Map([
|
|
4
|
-
['zod', createZod]
|
|
5
|
-
]);
|
|
6
|
-
const DEFAULT_VALIDATION_IMPLEMENTATION = 'zod';
|
|
7
|
-
const implementationName = process.env.VALIDATION_IMPLEMENTATION ?? DEFAULT_VALIDATION_IMPLEMENTATION;
|
|
8
|
-
const creator = implementations.get(implementationName.toLowerCase());
|
|
9
|
-
if (creator === undefined) {
|
|
10
|
-
throw new UnknownImplementation(implementationName);
|
|
11
|
-
}
|
|
12
|
-
export default creator();
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import ValidationResult from '../../definitions/ValidationResult.js';
|
|
2
|
-
import type { Validator } from '../../definitions/interfaces.js';
|
|
3
|
-
import type { ValidationSchema } from '../../definitions/types.js';
|
|
4
|
-
export default class Zod implements Validator {
|
|
5
|
-
#private;
|
|
6
|
-
constructor();
|
|
7
|
-
validate(data: unknown, schema: ValidationSchema): ValidationResult;
|
|
8
|
-
}
|