@theshelf/validation 0.0.2 → 0.0.4
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/dist/definitions/constants.d.ts +2 -0
- package/dist/definitions/constants.js +3 -1
- package/dist/definitions/interfaces.d.ts +2 -2
- package/dist/definitions/types.d.ts +7 -1
- package/dist/errors/UnknownImplementation.d.ts +1 -1
- package/dist/errors/UnknownImplementation.js +1 -1
- package/dist/errors/UnknownValidator.d.ts +1 -1
- package/dist/errors/UnknownValidator.js +1 -1
- package/dist/implementation.d.ts +1 -1
- package/dist/implementation.js +2 -2
- package/dist/implementations/zod/Zod.d.ts +3 -3
- package/dist/implementations/zod/Zod.js +15 -3
- package/dist/implementations/zod/create.d.ts +1 -1
- package/dist/implementations/zod/create.js +1 -1
- package/dist/index.d.ts +6 -6
- package/dist/index.js +6 -6
- package/package.json +4 -1
|
@@ -3,10 +3,12 @@ declare const FieldTypes: {
|
|
|
3
3
|
NUMBER: string;
|
|
4
4
|
BOOLEAN: string;
|
|
5
5
|
DATE: string;
|
|
6
|
+
DATETIME: string;
|
|
6
7
|
UUID: string;
|
|
7
8
|
EMAIL: string;
|
|
8
9
|
ARRAY: string;
|
|
9
10
|
URL: string;
|
|
11
|
+
ENUM: string;
|
|
10
12
|
};
|
|
11
13
|
declare const MAX_EMAIL_LENGTH = 320;
|
|
12
14
|
declare const MAX_URL_LENGTH = 2083;
|
|
@@ -3,10 +3,12 @@ const FieldTypes = {
|
|
|
3
3
|
NUMBER: 'number',
|
|
4
4
|
BOOLEAN: 'boolean',
|
|
5
5
|
DATE: 'date',
|
|
6
|
+
DATETIME: 'datetime',
|
|
6
7
|
UUID: 'uuid',
|
|
7
8
|
EMAIL: 'email',
|
|
8
9
|
ARRAY: 'array',
|
|
9
|
-
URL: 'url'
|
|
10
|
+
URL: 'url',
|
|
11
|
+
ENUM: 'enum'
|
|
10
12
|
};
|
|
11
13
|
Object.freeze(FieldTypes);
|
|
12
14
|
const MAX_EMAIL_LENGTH = 320;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type ValidationResult from './ValidationResult';
|
|
2
|
-
import type { ValidationSchema } from './types';
|
|
1
|
+
import type ValidationResult from './ValidationResult.js';
|
|
2
|
+
import type { ValidationSchema } from './types.js';
|
|
3
3
|
export interface Validator {
|
|
4
4
|
validate(data: unknown, schema: ValidationSchema): ValidationResult;
|
|
5
5
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { FieldTypes } from './constants';
|
|
1
|
+
import type { FieldTypes } from './constants.js';
|
|
2
2
|
export type ValidationType = keyof typeof FieldTypes;
|
|
3
3
|
type DefaultProperties = {
|
|
4
4
|
required: boolean;
|
|
@@ -19,11 +19,15 @@ export type ArrayProperties = DefaultProperties & {
|
|
|
19
19
|
};
|
|
20
20
|
export type BooleanProperties = DefaultProperties;
|
|
21
21
|
export type DateProperties = DefaultProperties;
|
|
22
|
+
export type DateTimeProperties = DefaultProperties;
|
|
22
23
|
export type UUIDProperties = DefaultProperties;
|
|
23
24
|
export type EmailProperties = DefaultProperties;
|
|
24
25
|
export type URLProperties = DefaultProperties & {
|
|
25
26
|
protocols?: string[];
|
|
26
27
|
};
|
|
28
|
+
export type EnumProperties = DefaultProperties & {
|
|
29
|
+
values?: string[];
|
|
30
|
+
};
|
|
27
31
|
export type Message = {
|
|
28
32
|
message: string;
|
|
29
33
|
};
|
|
@@ -32,10 +36,12 @@ export type ValidationTypes = {
|
|
|
32
36
|
NUMBER: NumberProperties;
|
|
33
37
|
BOOLEAN: BooleanProperties;
|
|
34
38
|
DATE: DateProperties;
|
|
39
|
+
DATETIME: DateTimeProperties;
|
|
35
40
|
UUID: UUIDProperties;
|
|
36
41
|
EMAIL: EmailProperties;
|
|
37
42
|
ARRAY: ArrayProperties;
|
|
38
43
|
URL: URLProperties;
|
|
44
|
+
ENUM: EnumProperties;
|
|
39
45
|
};
|
|
40
46
|
export type Validation = Partial<ValidationTypes | Message>;
|
|
41
47
|
export type ValidationSchema = Record<string, Validation>;
|
package/dist/implementation.d.ts
CHANGED
package/dist/implementation.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import UnknownImplementation from './errors/UnknownImplementation';
|
|
2
|
-
import createZod from './implementations/zod/create';
|
|
1
|
+
import UnknownImplementation from './errors/UnknownImplementation.js';
|
|
2
|
+
import createZod from './implementations/zod/create.js';
|
|
3
3
|
const implementations = new Map([
|
|
4
4
|
['zod', createZod]
|
|
5
5
|
]);
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import ValidationResult from '../../definitions/ValidationResult';
|
|
2
|
-
import type { Validator } from '../../definitions/interfaces';
|
|
3
|
-
import type { ValidationSchema } from '../../definitions/types';
|
|
1
|
+
import ValidationResult from '../../definitions/ValidationResult.js';
|
|
2
|
+
import type { Validator } from '../../definitions/interfaces.js';
|
|
3
|
+
import type { ValidationSchema } from '../../definitions/types.js';
|
|
4
4
|
export default class Zod implements Validator {
|
|
5
5
|
#private;
|
|
6
6
|
constructor();
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { z } from 'zod';
|
|
2
|
-
import ValidationResult from '../../definitions/ValidationResult';
|
|
3
|
-
import { FieldTypes, MAX_EMAIL_LENGTH, MAX_URL_LENGTH } from '../../definitions/constants';
|
|
4
|
-
import UnknownValidator from '../../errors/UnknownValidator';
|
|
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 {
|
|
@@ -11,10 +11,12 @@ export default class Zod {
|
|
|
11
11
|
this.#validations.set(FieldTypes.NUMBER, (value) => this.#validateNumber(value));
|
|
12
12
|
this.#validations.set(FieldTypes.BOOLEAN, (value) => this.#validateBoolean(value));
|
|
13
13
|
this.#validations.set(FieldTypes.DATE, (value) => this.#validateDate(value));
|
|
14
|
+
this.#validations.set(FieldTypes.DATETIME, (value) => this.#validateDateTime(value));
|
|
14
15
|
this.#validations.set(FieldTypes.UUID, (value) => this.#validateUuid(value));
|
|
15
16
|
this.#validations.set(FieldTypes.EMAIL, (value) => this.#validateEmail(value));
|
|
16
17
|
this.#validations.set(FieldTypes.ARRAY, (value) => this.#validateArray(value));
|
|
17
18
|
this.#validations.set(FieldTypes.URL, (value) => this.#validateUrl(value));
|
|
19
|
+
this.#validations.set(FieldTypes.ENUM, (value) => this.#validateEnum(value));
|
|
18
20
|
}
|
|
19
21
|
validate(data, schema) {
|
|
20
22
|
const validator = this.#buildValidator(schema);
|
|
@@ -68,6 +70,10 @@ export default class Zod {
|
|
|
68
70
|
return this.#checkRequired(value, validation);
|
|
69
71
|
}
|
|
70
72
|
#validateDate(value) {
|
|
73
|
+
const validation = z.iso.date();
|
|
74
|
+
return this.#checkRequired(value, validation);
|
|
75
|
+
}
|
|
76
|
+
#validateDateTime(value) {
|
|
71
77
|
const validation = z.iso.datetime();
|
|
72
78
|
return this.#checkRequired(value, validation);
|
|
73
79
|
}
|
|
@@ -97,6 +103,12 @@ export default class Zod {
|
|
|
97
103
|
}
|
|
98
104
|
return this.#checkRequired(value, validation);
|
|
99
105
|
}
|
|
106
|
+
#validateEnum(value) {
|
|
107
|
+
const validation = value.values === undefined
|
|
108
|
+
? z.enum([])
|
|
109
|
+
: z.enum(value.values);
|
|
110
|
+
return this.#checkRequired(value, validation);
|
|
111
|
+
}
|
|
100
112
|
#checkRequired(value, validation) {
|
|
101
113
|
return value.required
|
|
102
114
|
? validation
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import Zod from './Zod';
|
|
1
|
+
import Zod from './Zod.js';
|
|
2
2
|
export default function create(): Zod;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export * from './definitions/constants';
|
|
2
|
-
export * from './definitions/types';
|
|
3
|
-
export { default as UnknownImplementation } from './errors/UnknownImplementation';
|
|
4
|
-
export { default as UnknownValidator } from './errors/UnknownValidator';
|
|
5
|
-
export { default as ValidationError } from './errors/ValidationError';
|
|
6
|
-
export { default } from './implementation';
|
|
1
|
+
export * from './definitions/constants.js';
|
|
2
|
+
export * from './definitions/types.js';
|
|
3
|
+
export { default as UnknownImplementation } from './errors/UnknownImplementation.js';
|
|
4
|
+
export { default as UnknownValidator } from './errors/UnknownValidator.js';
|
|
5
|
+
export { default as ValidationError } from './errors/ValidationError.js';
|
|
6
|
+
export { default } from './implementation.js';
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export * from './definitions/constants';
|
|
2
|
-
export * from './definitions/types';
|
|
3
|
-
export { default as UnknownImplementation } from './errors/UnknownImplementation';
|
|
4
|
-
export { default as UnknownValidator } from './errors/UnknownValidator';
|
|
5
|
-
export { default as ValidationError } from './errors/ValidationError';
|
|
6
|
-
export { default } from './implementation';
|
|
1
|
+
export * from './definitions/constants.js';
|
|
2
|
+
export * from './definitions/types.js';
|
|
3
|
+
export { default as UnknownImplementation } from './errors/UnknownImplementation.js';
|
|
4
|
+
export { default as UnknownValidator } from './errors/UnknownValidator.js';
|
|
5
|
+
export { default as ValidationError } from './errors/ValidationError.js';
|
|
6
|
+
export { default } from './implementation.js';
|
package/package.json
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@theshelf/validation",
|
|
3
3
|
"private": false,
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.4",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"repository": {
|
|
7
|
+
"url": "https://github.com/MaskingTechnology/theshelf"
|
|
8
|
+
},
|
|
6
9
|
"scripts": {
|
|
7
10
|
"build": "tsc",
|
|
8
11
|
"clean": "rimraf dist",
|