@theshelf/validation 0.0.3 → 0.1.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.
@@ -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;
@@ -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>;
@@ -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
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "@theshelf/validation",
3
3
  "private": false,
4
- "version": "0.0.3",
4
+ "version": "0.1.0",
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",