@cleverbrush/schema 1.0.0-beta.4 → 1.0.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.
Files changed (76) hide show
  1. package/README.md +118 -320
  2. package/dist/builders/AnySchemaBuilder.d.ts +76 -0
  3. package/dist/builders/AnySchemaBuilder.js +112 -0
  4. package/dist/builders/ArraySchemaBuilder.d.ts +112 -14
  5. package/dist/builders/ArraySchemaBuilder.js +254 -111
  6. package/dist/builders/BooleanSchemaBuilder.d.ts +69 -29
  7. package/dist/builders/BooleanSchemaBuilder.js +134 -74
  8. package/dist/builders/DateSchemaBuilder.d.ts +177 -0
  9. package/dist/builders/DateSchemaBuilder.js +433 -0
  10. package/dist/builders/FunctionSchemaBuilder.d.ts +59 -23
  11. package/dist/builders/FunctionSchemaBuilder.js +103 -56
  12. package/dist/builders/NumberSchemaBuilder.d.ts +159 -59
  13. package/dist/builders/NumberSchemaBuilder.js +345 -165
  14. package/dist/builders/ObjectSchemaBuilder.d.ts +357 -81
  15. package/dist/builders/ObjectSchemaBuilder.js +519 -283
  16. package/dist/builders/SchemaBuilder.d.ts +158 -34
  17. package/dist/builders/SchemaBuilder.js +258 -71
  18. package/dist/builders/StringSchemaBuilder.d.ts +138 -13
  19. package/dist/builders/StringSchemaBuilder.js +261 -115
  20. package/dist/builders/UnionSchemaBuilder.d.ts +129 -9
  21. package/dist/builders/UnionSchemaBuilder.js +196 -75
  22. package/dist/index.d.ts +20 -40
  23. package/dist/index.js +19 -32
  24. package/dist/utils/transaction.d.ts +46 -0
  25. package/dist/utils/transaction.js +178 -0
  26. package/package.json +27 -14
  27. package/dist/defaultSchemas.d.ts +0 -4
  28. package/dist/defaultSchemas.js +0 -45
  29. package/dist/schema.d.ts +0 -230
  30. package/dist/schema.js +0 -2
  31. package/dist/schemaRegistry.d.ts +0 -49
  32. package/dist/schemaRegistry.js +0 -278
  33. package/dist/validators/validateArray.d.ts +0 -3
  34. package/dist/validators/validateArray.js +0 -60
  35. package/dist/validators/validateBoolean.d.ts +0 -2
  36. package/dist/validators/validateBoolean.js +0 -30
  37. package/dist/validators/validateFunction.d.ts +0 -2
  38. package/dist/validators/validateFunction.js +0 -21
  39. package/dist/validators/validateNumber.d.ts +0 -2
  40. package/dist/validators/validateNumber.js +0 -69
  41. package/dist/validators/validateObject.d.ts +0 -3
  42. package/dist/validators/validateObject.js +0 -95
  43. package/dist/validators/validateString.d.ts +0 -2
  44. package/dist/validators/validateString.js +0 -50
  45. package/dist/validators/validateUnion.d.ts +0 -3
  46. package/dist/validators/validateUnion.js +0 -30
  47. package/src/builders/ArraySchemaBuilder.test.ts +0 -270
  48. package/src/builders/ArraySchemaBuilder.ts +0 -381
  49. package/src/builders/BooleanSchemaBuilder.test.ts +0 -196
  50. package/src/builders/BooleanSchemaBuilder.ts +0 -155
  51. package/src/builders/FunctionSchemaBuilder.test.ts +0 -134
  52. package/src/builders/FunctionSchemaBuilder.ts +0 -109
  53. package/src/builders/NumberSchemaBuilder.test.ts +0 -493
  54. package/src/builders/NumberSchemaBuilder.ts +0 -789
  55. package/src/builders/ObjectSchemaBuilder.test.ts +0 -657
  56. package/src/builders/ObjectSchemaBuilder.ts +0 -794
  57. package/src/builders/SchemaBuilder.test.ts +0 -73
  58. package/src/builders/SchemaBuilder.ts +0 -135
  59. package/src/builders/StringSchemaBuilder.test.ts +0 -318
  60. package/src/builders/StringSchemaBuilder.ts +0 -392
  61. package/src/builders/UnionSchemaBuilder.test.ts +0 -162
  62. package/src/builders/UnionSchemaBuilder.ts +0 -154
  63. package/src/defaultSchemas.ts +0 -44
  64. package/src/index.ts +0 -66
  65. package/src/schema.ts +0 -827
  66. package/src/schemaRegistry.builders.test.ts +0 -1393
  67. package/src/schemaRegistry.test.ts +0 -118
  68. package/src/schemaRegistry.ts +0 -461
  69. package/src/validators/validateArray.ts +0 -76
  70. package/src/validators/validateBoolean.ts +0 -36
  71. package/src/validators/validateFunction.ts +0 -25
  72. package/src/validators/validateNumber.ts +0 -82
  73. package/src/validators/validateObject.ts +0 -119
  74. package/src/validators/validateString.ts +0 -59
  75. package/src/validators/validateUnion.ts +0 -36
  76. package/tsconfig.json +0 -16
@@ -1,14 +1,139 @@
1
- import { ISchemaBuilder } from './SchemaBuilder.js';
2
- export interface IStringSchemaBuilder<TRequired extends boolean = true, TNullable extends boolean = false, TMinLength extends number | undefined = undefined, TMaxLength extends number | undefined = undefined, TEqualsTo extends string | undefined = undefined> extends ISchemaBuilder<TRequired, TNullable> {
3
- optional(): IStringSchemaBuilder<false, TNullable, TMinLength, TMaxLength, TEqualsTo>;
4
- required(): IStringSchemaBuilder<true, TNullable, TMinLength, TMaxLength, TEqualsTo>;
5
- nullable(): IStringSchemaBuilder<TRequired, true, TMinLength, TMaxLength, TEqualsTo>;
6
- notNullable(): IStringSchemaBuilder<TRequired, false, TMinLength, TMaxLength, TEqualsTo>;
7
- minLength<K extends number>(val: K): IStringSchemaBuilder<TRequired, TNullable, K, TMaxLength, TEqualsTo>;
8
- clearMinLength(): IStringSchemaBuilder<TRequired, TNullable, undefined, TMaxLength, TEqualsTo>;
9
- maxLength<K extends number>(val: K): IStringSchemaBuilder<TRequired, TNullable, TMinLength, K, TEqualsTo>;
10
- clearMaxLength(): IStringSchemaBuilder<TRequired, TNullable, TMinLength, undefined, TEqualsTo>;
11
- equals<T extends string>(val: T): IStringSchemaBuilder<TRequired, TNullable, TMinLength, TMaxLength, T>;
12
- clearEquals(): IStringSchemaBuilder<TRequired, TNullable, TMinLength, TMaxLength, undefined>;
1
+ import { Preprocessor, SchemaBuilder, ValidationResult, ValidationContext, Validator } from './SchemaBuilder.js';
2
+ type StringSchemaBuilderCreateProps<T = string, R extends boolean = true> = Partial<ReturnType<StringSchemaBuilder<T, R>['introspect']>>;
3
+ /**
4
+ * Allows to define a schema for a string. It can be: required or optional,
5
+ * restricted to be equal to a certain value, restricted to have a certain
6
+ * length.
7
+ *
8
+ * **NOTE** this class is exported only to give opportunity to extend it
9
+ * by inheriting. It is not recommended to create an instance of this class
10
+ * directly. Use {@link string | string()} function instead.
11
+ *
12
+ * @example ```ts
13
+ * const schema = string().equals('hello');
14
+ * const result = await schema.validate('hello');
15
+ * // result.valid === true
16
+ * // result.object === 'hello'
17
+ * ```
18
+ *
19
+ * @example ```ts
20
+ * const schema = string().equals('hello');
21
+ * const result = await schema.validate('world');
22
+ * // result.valid === false
23
+ * // result.errors[0].message === "is expected to be equal to 'hello'"
24
+ * ```
25
+ *
26
+ * @example ```ts
27
+ * const schema = string().minLength(5);
28
+ * const result = await schema.validate('hello');
29
+ * // result.valid === true
30
+ * // result.object === 'hello'
31
+ * ```
32
+ *
33
+ * @example ```ts
34
+ * const schema = string().minLength(5);
35
+ * const result = await schema.validate('hi');
36
+ * // result.valid === false
37
+ * // result.errors[0].message === 'is expected to have a length of at least 5'
38
+ * ```
39
+ *
40
+ * @example ```ts
41
+ * const schema = string().minLength(2).maxLength(5);
42
+ * const result = await schema.validate('yes');
43
+ * // result.valid === true
44
+ * // result.object === 'yes'
45
+ * ```
46
+ *
47
+ * @example ```ts
48
+ * const schema = string('no');
49
+ * const result = await schema.validate('yes');
50
+ * // result.valid === false
51
+ * // result.errors[0].message === "is expected to be equal to 'no'"
52
+ * ```
53
+ *
54
+ * @see {@link string}
55
+ */
56
+ export declare class StringSchemaBuilder<TResult = string, TRequired extends boolean = true> extends SchemaBuilder<TResult, TRequired> {
57
+ #private;
58
+ static create(props: StringSchemaBuilderCreateProps): StringSchemaBuilder<string, true>;
59
+ private constructor();
60
+ introspect(): {
61
+ /**
62
+ * Min length of the string (if defined).
63
+ */
64
+ minLength: number | undefined;
65
+ /**
66
+ * Max length of the string (if defined).
67
+ */
68
+ maxLength: number | undefined;
69
+ /**
70
+ * If set, restrict object to be equal to a certain value.
71
+ */
72
+ equalsTo: string | undefined;
73
+ /**
74
+ * Array of preprocessor functions
75
+ */
76
+ preprocessors: Preprocessor<TResult>[];
77
+ /**
78
+ * Array of validator functions
79
+ */
80
+ validators: Validator<TResult>[];
81
+ type: string;
82
+ isRequired: boolean;
83
+ };
84
+ /**
85
+ * @hidden
86
+ */
87
+ hasType<T>(notUsed?: T): StringSchemaBuilder<T, true>;
88
+ /**
89
+ * @hidden
90
+ */
91
+ clearHasType(): StringSchemaBuilder<string, TRequired>;
92
+ /**
93
+ * Performs validion of string schema over `object`.
94
+ * @param context Optional `ValidationContext` settings.
95
+ */
96
+ validate(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
97
+ protected createFromProps<T, TReq extends boolean>(props: StringSchemaBuilderCreateProps<T, TReq>): this;
98
+ /**
99
+ * Restricts object to be equal to `value`.
100
+ */
101
+ equals<T extends string>(value: T): StringSchemaBuilder<T, TRequired>;
102
+ /**
103
+ * Cancels `equals()` call.
104
+ */
105
+ clearEquals(): StringSchemaBuilder<string, TRequired>;
106
+ /**
107
+ * @hidden
108
+ */
109
+ required(): StringSchemaBuilder<TResult, true>;
110
+ /**
111
+ * @hidden
112
+ */
113
+ optional(): StringSchemaBuilder<TResult, false>;
114
+ /**
115
+ * Set minimal length of the valid value for schema.
116
+ * @param {number} length
117
+ */
118
+ minLength(length: number): StringSchemaBuilder<TResult, TRequired>;
119
+ /**
120
+ * Cancel `minLength()` call.
121
+ */
122
+ clearMinLength(): StringSchemaBuilder<TResult, TRequired>;
123
+ /**
124
+ * Set maximal length of the valid value for schema.
125
+ * @length {number} length
126
+ */
127
+ maxLength(length: number): StringSchemaBuilder<TResult, TRequired>;
128
+ /**
129
+ * cancel `maxLength()` call.
130
+ */
131
+ clearMaxLength(): StringSchemaBuilder<TResult, TRequired>;
13
132
  }
14
- export declare const string: <T extends string = undefined>(equals?: T) => IStringSchemaBuilder<true, false, undefined, undefined, T extends undefined ? undefined : T>;
133
+ /**
134
+ * Creates a string schema restricted to be equal to `equals`.
135
+ * @param equals number value
136
+ */
137
+ export declare function string<T extends string>(equals: T): StringSchemaBuilder<T, true>;
138
+ export declare function string(): StringSchemaBuilder<string, true>;
139
+ export {};
@@ -1,140 +1,286 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.string = void 0;
4
- const SchemaBuilder_js_1 = require("./SchemaBuilder.js");
5
- const defaultSchemas_js_1 = require("../defaultSchemas.js");
6
- class StringSchemaBuilder extends SchemaBuilder_js_1.SchemaBuilder {
7
- _minLength;
8
- _maxLength;
9
- _equals;
10
- type = 'string';
11
- clone() {
12
- return StringSchemaBuilder.create(this._schema);
13
- }
14
- get _schema() {
15
- return Object.assign({
16
- type: this.type
17
- }, this.getCommonSchema(), typeof this._minLength !== 'undefined'
18
- ? { minLength: this._minLength }
19
- : {}, typeof this._maxLength !== 'undefined'
20
- ? { maxLength: this._maxLength }
21
- : {}, typeof this._equals !== 'undefined' ? { equals: this._equals } : {});
1
+ import { SchemaBuilder } from './SchemaBuilder.js';
2
+ /**
3
+ * Allows to define a schema for a string. It can be: required or optional,
4
+ * restricted to be equal to a certain value, restricted to have a certain
5
+ * length.
6
+ *
7
+ * **NOTE** this class is exported only to give opportunity to extend it
8
+ * by inheriting. It is not recommended to create an instance of this class
9
+ * directly. Use {@link string | string()} function instead.
10
+ *
11
+ * @example ```ts
12
+ * const schema = string().equals('hello');
13
+ * const result = await schema.validate('hello');
14
+ * // result.valid === true
15
+ * // result.object === 'hello'
16
+ * ```
17
+ *
18
+ * @example ```ts
19
+ * const schema = string().equals('hello');
20
+ * const result = await schema.validate('world');
21
+ * // result.valid === false
22
+ * // result.errors[0].message === "is expected to be equal to 'hello'"
23
+ * ```
24
+ *
25
+ * @example ```ts
26
+ * const schema = string().minLength(5);
27
+ * const result = await schema.validate('hello');
28
+ * // result.valid === true
29
+ * // result.object === 'hello'
30
+ * ```
31
+ *
32
+ * @example ```ts
33
+ * const schema = string().minLength(5);
34
+ * const result = await schema.validate('hi');
35
+ * // result.valid === false
36
+ * // result.errors[0].message === 'is expected to have a length of at least 5'
37
+ * ```
38
+ *
39
+ * @example ```ts
40
+ * const schema = string().minLength(2).maxLength(5);
41
+ * const result = await schema.validate('yes');
42
+ * // result.valid === true
43
+ * // result.object === 'yes'
44
+ * ```
45
+ *
46
+ * @example ```ts
47
+ * const schema = string('no');
48
+ * const result = await schema.validate('yes');
49
+ * // result.valid === false
50
+ * // result.errors[0].message === "is expected to be equal to 'no'"
51
+ * ```
52
+ *
53
+ * @see {@link string}
54
+ */
55
+ export class StringSchemaBuilder extends SchemaBuilder {
56
+ #minLength;
57
+ #maxLength;
58
+ #equalsTo;
59
+ static create(props) {
60
+ return new StringSchemaBuilder({
61
+ type: 'string',
62
+ ...props
63
+ });
22
64
  }
23
- optional() {
24
- if (this.isRequired === false) {
25
- return this;
65
+ constructor(props) {
66
+ super(props);
67
+ if (typeof props.minLength === 'number') {
68
+ this.#minLength = props.minLength;
26
69
  }
27
- return StringSchemaBuilder.create({
28
- ...this._schema,
29
- isRequired: false
70
+ if (typeof props.maxLength === 'number') {
71
+ this.#maxLength = props.maxLength;
72
+ }
73
+ if (typeof props.equalsTo === 'string' ||
74
+ typeof props.equalsTo === 'undefined') {
75
+ this.#equalsTo = props.equalsTo;
76
+ }
77
+ }
78
+ introspect() {
79
+ return {
80
+ ...super.introspect(),
81
+ /**
82
+ * Min length of the string (if defined).
83
+ */
84
+ minLength: this.#minLength,
85
+ /**
86
+ * Max length of the string (if defined).
87
+ */
88
+ maxLength: this.#maxLength,
89
+ /**
90
+ * If set, restrict object to be equal to a certain value.
91
+ */
92
+ equalsTo: this.#equalsTo,
93
+ /**
94
+ * Array of preprocessor functions
95
+ */
96
+ preprocessors: this.preprocessors,
97
+ /**
98
+ * Array of validator functions
99
+ */
100
+ validators: this.validators
101
+ };
102
+ }
103
+ /**
104
+ * @hidden
105
+ */
106
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
107
+ hasType(notUsed) {
108
+ return this.createFromProps({
109
+ ...this.introspect()
30
110
  });
31
111
  }
32
- required() {
33
- if (this.isRequired === true) {
34
- return this;
35
- }
36
- return StringSchemaBuilder.create({
37
- ...this._schema,
38
- isRequired: true
112
+ /**
113
+ * @hidden
114
+ */
115
+ clearHasType() {
116
+ return this.createFromProps({
117
+ ...this.introspect()
39
118
  });
40
119
  }
41
- nullable() {
42
- if (this.isNullable === true) {
43
- return this;
120
+ /**
121
+ * Performs validion of string schema over `object`.
122
+ * @param context Optional `ValidationContext` settings.
123
+ */
124
+ async validate(object, context) {
125
+ const superResult = await super.preValidate(object, context);
126
+ const { valid, context: prevalidationContext, transaction: preValidationTransaction, errors } = superResult;
127
+ const { path } = prevalidationContext;
128
+ if (!valid) {
129
+ return {
130
+ valid,
131
+ errors
132
+ };
44
133
  }
45
- return StringSchemaBuilder.create({
46
- ...this._schema,
47
- isNullable: true
134
+ const { object: { validatedObject: objToValidate } } = preValidationTransaction;
135
+ if ((typeof objToValidate === 'undefined' || objToValidate === null) &&
136
+ this.isRequired === false) {
137
+ return {
138
+ valid: true,
139
+ object: objToValidate
140
+ };
141
+ }
142
+ if (typeof objToValidate !== 'string')
143
+ return {
144
+ valid: false,
145
+ errors: [
146
+ {
147
+ message: `expected type string, but saw ${typeof objToValidate}`,
148
+ path: path
149
+ }
150
+ ]
151
+ };
152
+ if (typeof this.#equalsTo !== 'undefined' &&
153
+ objToValidate !== this.#equalsTo) {
154
+ return {
155
+ valid: false,
156
+ errors: [
157
+ {
158
+ message: `is expected to be equal to ${this.#equalsTo}`,
159
+ path: path
160
+ }
161
+ ]
162
+ };
163
+ }
164
+ if (typeof this.#minLength !== 'undefined') {
165
+ if (objToValidate.length < this.#minLength)
166
+ return {
167
+ valid: false,
168
+ errors: [
169
+ {
170
+ message: `expected to has at least ${this.#minLength} characters length`,
171
+ path: path
172
+ }
173
+ ]
174
+ };
175
+ }
176
+ if (typeof this.#maxLength !== 'undefined') {
177
+ if (objToValidate.length > this.#maxLength)
178
+ return {
179
+ valid: false,
180
+ errors: [
181
+ {
182
+ message: `must not exceed ${this.#maxLength} characters`,
183
+ path: path
184
+ }
185
+ ]
186
+ };
187
+ }
188
+ return {
189
+ valid: true,
190
+ object: objToValidate
191
+ };
192
+ }
193
+ createFromProps(props) {
194
+ return StringSchemaBuilder.create(props);
195
+ }
196
+ /**
197
+ * Restricts object to be equal to `value`.
198
+ */
199
+ equals(value) {
200
+ if (typeof value !== 'string')
201
+ throw new Error('string expected');
202
+ return this.createFromProps({
203
+ ...this.introspect(),
204
+ equalsTo: value
48
205
  });
49
206
  }
50
- notNullable() {
51
- if (this.isNullable === false) {
52
- return this;
53
- }
54
- return StringSchemaBuilder.create({
55
- ...this._schema,
56
- isNullable: false
207
+ /**
208
+ * Cancels `equals()` call.
209
+ */
210
+ clearEquals() {
211
+ return this.createFromProps({
212
+ ...this.introspect(),
213
+ equalsTo: undefined
57
214
  });
58
215
  }
59
- minLength(val) {
60
- if (this._minLength === val) {
61
- return this;
62
- }
63
- return StringSchemaBuilder.create({
64
- ...this._schema,
65
- minLength: val
216
+ /**
217
+ * @hidden
218
+ */
219
+ required() {
220
+ return super.required();
221
+ }
222
+ /**
223
+ * @hidden
224
+ */
225
+ optional() {
226
+ return super.optional();
227
+ }
228
+ /**
229
+ * Set minimal length of the valid value for schema.
230
+ * @param {number} length
231
+ */
232
+ minLength(length) {
233
+ if (typeof length !== 'number')
234
+ throw new Error('length must be a number');
235
+ return this.createFromProps({
236
+ ...this.introspect(),
237
+ minLength: length
66
238
  });
67
239
  }
240
+ /**
241
+ * Cancel `minLength()` call.
242
+ */
68
243
  clearMinLength() {
69
- if (typeof this._minLength === 'undefined') {
70
- return this;
71
- }
72
- return StringSchemaBuilder.create({
73
- ...this._schema,
74
- minLength: undefined
244
+ const schema = this.introspect();
245
+ delete schema.minLength;
246
+ return this.createFromProps({
247
+ ...schema
75
248
  });
76
249
  }
77
- maxLength(val) {
78
- if (this._maxLength === val) {
79
- return this;
80
- }
81
- return StringSchemaBuilder.create({
82
- ...this._schema,
83
- maxLength: val
250
+ /**
251
+ * Set maximal length of the valid value for schema.
252
+ * @length {number} length
253
+ */
254
+ maxLength(length) {
255
+ if (typeof length !== 'number')
256
+ throw new Error('length must be a number');
257
+ return this.createFromProps({
258
+ ...this.introspect(),
259
+ maxLength: length
84
260
  });
85
261
  }
262
+ /**
263
+ * cancel `maxLength()` call.
264
+ */
86
265
  clearMaxLength() {
87
- if (typeof this._maxLength === 'undefined') {
88
- return this;
89
- }
90
- return StringSchemaBuilder.create({
91
- ...this._schema,
92
- maxLength: undefined
266
+ const schema = this.introspect();
267
+ delete schema.maxLength;
268
+ return this.createFromProps({
269
+ ...schema
93
270
  });
94
271
  }
95
- equals(val) {
96
- if (this._equals === val) {
97
- return this;
98
- }
99
- return StringSchemaBuilder.create({
100
- ...this._schema,
101
- equals: val
102
- });
103
- }
104
- clearEquals() {
105
- if (typeof this._equals === 'undefined') {
106
- return this;
107
- }
272
+ }
273
+ /**
274
+ * Creates a string schema.
275
+ */
276
+ export function string(equals) {
277
+ if (typeof equals === 'string') {
108
278
  return StringSchemaBuilder.create({
109
- ...this._schema,
110
- equals: undefined
279
+ isRequired: true,
280
+ equalsTo: equals
111
281
  });
112
282
  }
113
- static create(obj) {
114
- return new StringSchemaBuilder(obj);
115
- }
116
- constructor(obj) {
117
- super(obj);
118
- if (typeof obj === 'object' && obj) {
119
- if (typeof obj.minLength !== 'undefined') {
120
- this._minLength = obj.minLength;
121
- }
122
- if (typeof obj.maxLength !== 'undefined') {
123
- this._maxLength = obj.maxLength;
124
- }
125
- if (typeof obj.equals !== 'undefined') {
126
- this._equals = obj.equals;
127
- }
128
- }
129
- else {
130
- const defaultSchema = defaultSchemas_js_1.defaultSchemas['string'];
131
- this.isRequired = defaultSchema.isRequired;
132
- this.isNullable = defaultSchema.isNullable;
133
- }
134
- }
283
+ return StringSchemaBuilder.create({
284
+ isRequired: true
285
+ });
135
286
  }
136
- const string = (equals) => {
137
- const res = StringSchemaBuilder.create();
138
- return typeof equals === 'string' ? res.equals(equals) : res;
139
- };
140
- exports.string = string;