@cleverbrush/schema 1.1.11 → 3.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 (98) hide show
  1. package/README.md +1721 -104
  2. package/dist/builders/AnySchemaBuilder.d.ts +54 -14
  3. package/dist/builders/AnySchemaBuilder.js +2 -112
  4. package/dist/builders/AnySchemaBuilder.js.map +1 -0
  5. package/dist/builders/ArraySchemaBuilder.d.ts +126 -23
  6. package/dist/builders/ArraySchemaBuilder.js +2 -284
  7. package/dist/builders/ArraySchemaBuilder.js.map +1 -0
  8. package/dist/builders/BooleanSchemaBuilder.d.ts +109 -19
  9. package/dist/builders/BooleanSchemaBuilder.js +2 -150
  10. package/dist/builders/BooleanSchemaBuilder.js.map +1 -0
  11. package/dist/builders/DateSchemaBuilder.d.ts +154 -34
  12. package/dist/builders/DateSchemaBuilder.js +2 -433
  13. package/dist/builders/DateSchemaBuilder.js.map +1 -0
  14. package/dist/builders/ExternSchemaBuilder.d.ts +202 -0
  15. package/dist/builders/ExternSchemaBuilder.js +2 -0
  16. package/dist/builders/ExternSchemaBuilder.js.map +1 -0
  17. package/dist/builders/FunctionSchemaBuilder.d.ts +205 -18
  18. package/dist/builders/FunctionSchemaBuilder.js +2 -113
  19. package/dist/builders/FunctionSchemaBuilder.js.map +1 -0
  20. package/dist/builders/GenericSchemaBuilder.d.ts +294 -0
  21. package/dist/builders/LazySchemaBuilder.d.ts +169 -0
  22. package/dist/builders/NullSchemaBuilder.d.ts +162 -0
  23. package/dist/builders/NumberSchemaBuilder.d.ts +159 -31
  24. package/dist/builders/NumberSchemaBuilder.js +2 -386
  25. package/dist/builders/NumberSchemaBuilder.js.map +1 -0
  26. package/dist/builders/ObjectSchemaBuilder.d.ts +486 -61
  27. package/dist/builders/ObjectSchemaBuilder.js +2 -589
  28. package/dist/builders/ObjectSchemaBuilder.js.map +1 -0
  29. package/dist/builders/ParseStringSchemaBuilder.d.ts +204 -0
  30. package/dist/builders/ParseStringSchemaBuilder.js +2 -0
  31. package/dist/builders/ParseStringSchemaBuilder.js.map +1 -0
  32. package/dist/builders/PromiseSchemaBuilder.d.ts +213 -0
  33. package/dist/builders/PromiseSchemaBuilder.js +2 -0
  34. package/dist/builders/PromiseSchemaBuilder.js.map +1 -0
  35. package/dist/builders/PropertyValidationResult.d.ts +68 -0
  36. package/dist/builders/RecordSchemaBuilder.d.ts +343 -0
  37. package/dist/builders/RecordSchemaBuilder.js +2 -0
  38. package/dist/builders/RecordSchemaBuilder.js.map +1 -0
  39. package/dist/builders/SchemaBuilder.d.ts +907 -30
  40. package/dist/builders/StringSchemaBuilder.d.ts +154 -37
  41. package/dist/builders/StringSchemaBuilder.js +2 -414
  42. package/dist/builders/StringSchemaBuilder.js.map +1 -0
  43. package/dist/builders/TupleSchemaBuilder.d.ts +250 -0
  44. package/dist/builders/TupleSchemaBuilder.js +2 -0
  45. package/dist/builders/TupleSchemaBuilder.js.map +1 -0
  46. package/dist/builders/UnionSchemaBuilder.d.ts +141 -39
  47. package/dist/builders/UnionSchemaBuilder.js +2 -216
  48. package/dist/builders/UnionSchemaBuilder.js.map +1 -0
  49. package/dist/chunk-3JMDGYDT.js +2 -0
  50. package/dist/chunk-3JMDGYDT.js.map +1 -0
  51. package/dist/chunk-BUEVZ3KA.js +2 -0
  52. package/dist/chunk-BUEVZ3KA.js.map +1 -0
  53. package/dist/chunk-CFIJQ4GP.js +2 -0
  54. package/dist/chunk-CFIJQ4GP.js.map +1 -0
  55. package/dist/chunk-DY7J6RNN.js +2 -0
  56. package/dist/chunk-DY7J6RNN.js.map +1 -0
  57. package/dist/chunk-EIVZX4ZO.js +2 -0
  58. package/dist/chunk-EIVZX4ZO.js.map +1 -0
  59. package/dist/chunk-GXPV6UQK.js +2 -0
  60. package/dist/chunk-GXPV6UQK.js.map +1 -0
  61. package/dist/chunk-HN774HD7.js +2 -0
  62. package/dist/chunk-HN774HD7.js.map +1 -0
  63. package/dist/chunk-K6Z47OQY.js +2 -0
  64. package/dist/chunk-K6Z47OQY.js.map +1 -0
  65. package/dist/chunk-NUW3VXZV.js +2 -0
  66. package/dist/chunk-NUW3VXZV.js.map +1 -0
  67. package/dist/chunk-PHE4LIAN.js +2 -0
  68. package/dist/chunk-PHE4LIAN.js.map +1 -0
  69. package/dist/chunk-QARCEYGO.js +2 -0
  70. package/dist/chunk-QARCEYGO.js.map +1 -0
  71. package/dist/chunk-WDMJBGBD.js +2 -0
  72. package/dist/chunk-WDMJBGBD.js.map +1 -0
  73. package/dist/chunk-WQDYWDOE.js +2 -0
  74. package/dist/chunk-WQDYWDOE.js.map +1 -0
  75. package/dist/chunk-YQZHDMRF.js +2 -0
  76. package/dist/chunk-YQZHDMRF.js.map +1 -0
  77. package/dist/chunk-ZC6YBKCP.js +2 -0
  78. package/dist/chunk-ZC6YBKCP.js.map +1 -0
  79. package/dist/chunk-ZFI27R3L.js +2 -0
  80. package/dist/chunk-ZFI27R3L.js.map +1 -0
  81. package/dist/core.d.ts +28 -0
  82. package/dist/core.js +2 -0
  83. package/dist/core.js.map +1 -0
  84. package/dist/extension.d.ts +421 -0
  85. package/dist/extensions/array.d.ts +112 -0
  86. package/dist/extensions/enum.d.ts +190 -0
  87. package/dist/extensions/index.d.ts +112 -0
  88. package/dist/extensions/nullable.d.ts +26 -0
  89. package/dist/extensions/number.d.ts +228 -0
  90. package/dist/extensions/string.d.ts +332 -0
  91. package/dist/extensions/util.d.ts +45 -0
  92. package/dist/index.d.ts +10 -20
  93. package/dist/index.js +2 -19
  94. package/dist/index.js.map +1 -0
  95. package/dist/utils/transaction.d.ts +27 -4
  96. package/package.json +83 -7
  97. package/dist/builders/SchemaBuilder.js +0 -275
  98. package/dist/utils/transaction.js +0 -178
@@ -1,4 +1,4 @@
1
- import { Preprocessor, SchemaBuilder, ValidationResult, ValidationContext, Validator } from './SchemaBuilder.js';
1
+ import { type BRAND, type PreprocessorEntry, SchemaBuilder, type ValidationContext, type ValidationErrorMessageProvider, type ValidationResult, type ValidatorEntry } from './SchemaBuilder.js';
2
2
  type StringSchemaBuilderCreateProps<T = string, R extends boolean = true> = Partial<ReturnType<StringSchemaBuilder<T, R>['introspect']>>;
3
3
  /**
4
4
  * Allows to define a schema for a string. It can be: required or optional,
@@ -12,165 +12,282 @@ type StringSchemaBuilderCreateProps<T = string, R extends boolean = true> = Part
12
12
  *
13
13
  * @example ```ts
14
14
  * const schema = string().equals('hello');
15
- * const result = await schema.validate('hello');
15
+ * const result = schema.validate('hello');
16
16
  * // result.valid === true
17
17
  * // result.object === 'hello'
18
18
  * ```
19
19
  *
20
20
  * @example ```ts
21
21
  * const schema = string().equals('hello');
22
- * const result = await schema.validate('world');
22
+ * const result = schema.validate('world');
23
23
  * // result.valid === false
24
24
  * // result.errors[0].message === "is expected to be equal to 'hello'"
25
25
  * ```
26
26
  *
27
27
  * @example ```ts
28
28
  * const schema = string().minLength(5);
29
- * const result = await schema.validate('hello');
29
+ * const result = schema.validate('hello');
30
30
  * // result.valid === true
31
31
  * // result.object === 'hello'
32
32
  * ```
33
33
  *
34
34
  * @example ```ts
35
35
  * const schema = string().minLength(5);
36
- * const result = await schema.validate('hi');
36
+ * const result = schema.validate('hi');
37
37
  * // result.valid === false
38
38
  * // result.errors[0].message === 'is expected to have a length of at least 5'
39
39
  * ```
40
40
  *
41
41
  * @example ```ts
42
42
  * const schema = string().minLength(2).maxLength(5);
43
- * const result = await schema.validate('yes');
43
+ * const result = schema.validate('yes');
44
44
  * // result.valid === true
45
45
  * // result.object === 'yes'
46
46
  * ```
47
47
  *
48
48
  * @example ```ts
49
49
  * const schema = string('no');
50
- * const result = await schema.validate('yes');
50
+ * const result = schema.validate('yes');
51
51
  * // result.valid === false
52
52
  * // result.errors[0].message === "is expected to be equal to 'no'"
53
53
  * ```
54
54
  *
55
55
  * @see {@link string}
56
56
  */
57
- export declare class StringSchemaBuilder<TResult = string, TRequired extends boolean = true> extends SchemaBuilder<TResult, TRequired> {
57
+ export declare class StringSchemaBuilder<TResult = string, TRequired extends boolean = true, TNullable extends boolean = false, THasDefault extends boolean = false, TExtensions = {}> extends SchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> {
58
58
  #private;
59
- static create(props: StringSchemaBuilderCreateProps): StringSchemaBuilder<string, true>;
60
- private constructor();
59
+ /**
60
+ * @hidden
61
+ */
62
+ static create(props: StringSchemaBuilderCreateProps): StringSchemaBuilder<string, true, false, false, {}>;
63
+ protected constructor(props: StringSchemaBuilderCreateProps);
61
64
  introspect(): {
62
65
  /**
63
66
  * Min length of the string (if defined).
64
67
  */
65
68
  minLength: number | undefined;
69
+ /**
70
+ * Min length validation error message provider.
71
+ * If not provided, default error message will be used.
72
+ */
73
+ minLengthValidationErrorMessageProvider: ValidationErrorMessageProvider<StringSchemaBuilder<TResult, TRequired, false, false, {}>>;
66
74
  /**
67
75
  * Max length of the string (if defined).
68
76
  */
69
77
  maxLength: number | undefined;
78
+ /**
79
+ * Max length validation error message provider.
80
+ * If not provided, default error message will be used.
81
+ */
82
+ maxLengthValidationErrorMessageProvider: ValidationErrorMessageProvider<StringSchemaBuilder<TResult, TRequired, false, false, {}>>;
70
83
  /**
71
84
  * If set, restrict object to be equal to a certain value.
72
85
  */
73
86
  equalsTo: string | undefined;
87
+ /**
88
+ * Equals validation error message provider.
89
+ * If not provided, default error message will be used.
90
+ */
91
+ equalsToValidationErrorMessageProvider: ValidationErrorMessageProvider<StringSchemaBuilder<TResult, TRequired, false, false, {}>>;
74
92
  /**
75
93
  * If set, restrict string to start with a certain value.
76
94
  */
77
95
  startsWith: string | undefined;
96
+ /**
97
+ * Starts with validation error message provider.
98
+ * If not provided, default error message will be used.
99
+ */
100
+ startsWithValidationErrorMessageProvider: ValidationErrorMessageProvider<StringSchemaBuilder<TResult, TRequired, false, false, {}>>;
78
101
  /**
79
102
  * If set, restrict string to end with a certain value.
80
103
  */
81
104
  endsWith: string | undefined;
105
+ /**
106
+ * Ends with validation error message provider.
107
+ * If not provided, default error message will be used.
108
+ */
109
+ endsWithValidationErrorMessageProvider: ValidationErrorMessageProvider<StringSchemaBuilder<TResult, TRequired, false, false, {}>>;
82
110
  /**
83
111
  * If set, restrict string to match a certain regular expression.
84
112
  */
85
113
  matches: RegExp | undefined;
114
+ /**
115
+ * Matches validation error message provider.
116
+ * If not provided, default error message will be used.
117
+ */
118
+ matchesValidationErrorMessageProvider: ValidationErrorMessageProvider<StringSchemaBuilder<TResult, TRequired, false, false, {}>>;
86
119
  /**
87
120
  * Array of preprocessor functions
88
121
  */
89
- preprocessors: Preprocessor<TResult>[];
122
+ preprocessors: PreprocessorEntry<TResult>[];
90
123
  /**
91
124
  * Array of validator functions
92
125
  */
93
- validators: Validator<TResult>[];
126
+ validators: ValidatorEntry<TResult>[];
94
127
  type: string;
95
128
  isRequired: boolean;
129
+ isNullable: boolean;
130
+ isReadonly: boolean;
131
+ requiredValidationErrorMessageProvider: ValidationErrorMessageProvider<SchemaBuilder<any, any, any, any, any>>;
132
+ extensions: {
133
+ [x: string]: unknown;
134
+ };
135
+ hasDefault: boolean;
136
+ defaultValue: TResult | (() => TResult) | undefined;
137
+ description: string | undefined;
138
+ schemaName: string | undefined;
139
+ hasCatch: boolean;
140
+ catchValue: TResult | (() => TResult) | undefined;
141
+ example: unknown;
96
142
  };
97
143
  /**
98
- * @hidden
144
+ * @inheritdoc
99
145
  */
100
- hasType<T>(notUsed?: T): StringSchemaBuilder<T, true>;
146
+ hasType<T>(_notUsed?: T): StringSchemaBuilder<T, true, TNullable, THasDefault, TExtensions> & TExtensions;
101
147
  /**
102
- * @hidden
148
+ * @inheritdoc
149
+ */
150
+ clearHasType(): StringSchemaBuilder<string, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
151
+ /** {@inheritDoc SchemaBuilder.validate} */
152
+ validate(object: TResult, context?: ValidationContext): ValidationResult<TResult>;
153
+ /** {@inheritDoc SchemaBuilder.validateAsync} */
154
+ validateAsync(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
155
+ /**
156
+ * Performs synchronous validation of string schema over `object`.
157
+ * Throws if any preprocessor, validator, or error message provider returns a Promise.
158
+ * @param context Optional `ValidationContext` settings.
103
159
  */
104
- clearHasType(): StringSchemaBuilder<string, TRequired>;
160
+ protected _validate(object: TResult, context?: ValidationContext): ValidationResult<TResult>;
105
161
  /**
106
- * Performs validion of string schema over `object`.
162
+ * Performs async validation of string schema over `object`.
163
+ * Supports async preprocessors, validators, and error message providers.
107
164
  * @param context Optional `ValidationContext` settings.
108
165
  */
109
- validate(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
166
+ protected _validateAsync(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
110
167
  protected createFromProps<T, TReq extends boolean>(props: StringSchemaBuilderCreateProps<T, TReq>): this;
111
168
  /**
112
- * Restricts object to be equal to `value`.
169
+ * Restricts string to be equal to `value`.
113
170
  */
114
- equals<T extends string>(value: T): StringSchemaBuilder<T, TRequired>;
171
+ equals<T extends string>(value: T,
172
+ /**
173
+ * Custom error message provider.
174
+ */
175
+ errorMessage?: ValidationErrorMessageProvider<StringSchemaBuilder<TResult, TRequired>>): StringSchemaBuilder<T, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
115
176
  /**
116
177
  * Cancels `equals()` call.
117
178
  */
118
- clearEquals(): StringSchemaBuilder<string, TRequired>;
179
+ clearEquals(): StringSchemaBuilder<string, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
180
+ /**
181
+ * @hidden
182
+ */
183
+ nullable(): StringSchemaBuilder<TResult, TRequired, true, THasDefault, TExtensions> & TExtensions;
119
184
  /**
120
185
  * @hidden
121
186
  */
122
- required(): StringSchemaBuilder<TResult, true>;
187
+ notNullable(): StringSchemaBuilder<TResult, TRequired, false, THasDefault, TExtensions> & TExtensions;
123
188
  /**
124
189
  * @hidden
125
190
  */
126
- optional(): StringSchemaBuilder<TResult, false>;
191
+ required(errorMessage?: ValidationErrorMessageProvider): StringSchemaBuilder<TResult, true, TNullable, THasDefault, TExtensions> & TExtensions;
192
+ /**
193
+ * @hidden
194
+ */
195
+ optional(): StringSchemaBuilder<TResult, false, TNullable, THasDefault, TExtensions> & TExtensions;
196
+ /**
197
+ * @hidden
198
+ */
199
+ default(value: TResult | (() => TResult)): StringSchemaBuilder<TResult, true, TNullable, true, TExtensions> & TExtensions;
200
+ /**
201
+ * @hidden
202
+ */
203
+ clearDefault(): StringSchemaBuilder<TResult, TRequired, TNullable, false, TExtensions> & TExtensions;
204
+ /**
205
+ * @hidden
206
+ */
207
+ brand<TBrand extends string | symbol>(_name?: TBrand): StringSchemaBuilder<TResult & {
208
+ readonly [K in BRAND]: TBrand;
209
+ }, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
210
+ /**
211
+ * Marks the inferred type as `Readonly<string>`. Since strings are
212
+ * already immutable this is an identity operation, but it sets the
213
+ * `isReadonly` introspection flag for tooling consistency.
214
+ *
215
+ * @see {@link SchemaBuilder.readonly}
216
+ */
217
+ readonly(): StringSchemaBuilder<Readonly<TResult>, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
127
218
  /**
128
219
  * Set minimal length of the valid value for schema.
129
- * @param {number} length
220
+ * @param length minimum string length
130
221
  */
131
- minLength(length: number): StringSchemaBuilder<TResult, TRequired>;
222
+ minLength(length: number,
223
+ /**
224
+ * Custom error message provider.
225
+ */
226
+ errorMessage?: ValidationErrorMessageProvider<StringSchemaBuilder<TResult, TRequired>>): StringSchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
132
227
  /**
133
228
  * Cancel `minLength()` call.
134
229
  */
135
- clearMinLength(): StringSchemaBuilder<TResult, TRequired>;
230
+ clearMinLength(): StringSchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
136
231
  /**
137
232
  * Set maximal length of the valid value for schema.
138
- * @length {number} length
233
+ * @param length maximum string length
234
+ */
235
+ maxLength(length: number,
236
+ /**
237
+ * Custom error message provider.
139
238
  */
140
- maxLength(length: number): StringSchemaBuilder<TResult, TRequired>;
239
+ errorMessage?: ValidationErrorMessageProvider<StringSchemaBuilder<TResult, TRequired>>): StringSchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
141
240
  /**
142
- * cancel `maxLength()` call.
241
+ * Cancel `maxLength()` call.
143
242
  */
144
- clearMaxLength(): StringSchemaBuilder<TResult, TRequired>;
243
+ clearMaxLength(): StringSchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
145
244
  /**
146
245
  * Restricts string to start with `val`.
147
246
  */
148
- startsWith(val: string): StringSchemaBuilder<TResult, TRequired>;
247
+ startsWith<T extends string>(val: T,
248
+ /**
249
+ * Custom error message provider.
250
+ */
251
+ errorMessage?: ValidationErrorMessageProvider<StringSchemaBuilder<TResult, TRequired>>): StringSchemaBuilder<TResult extends string ? `${T}${TResult}` : TResult, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
149
252
  /**
150
253
  * Cancels `startsWith()` call.
151
254
  */
152
- clearStartsWith(): StringSchemaBuilder<TResult, TRequired>;
255
+ clearStartsWith(): StringSchemaBuilder<string, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
153
256
  /**
154
257
  * Restricts string to end with `val`.
155
258
  */
156
- endsWith(val: string): StringSchemaBuilder<TResult, TRequired>;
259
+ endsWith<T extends string>(val: T,
260
+ /**
261
+ * Custom error message provider.
262
+ */
263
+ errorMessage?: ValidationErrorMessageProvider<StringSchemaBuilder<TResult, TRequired>>): StringSchemaBuilder<TResult extends string ? `${TResult}${T}` : TResult, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
157
264
  /**
158
265
  * Cancels `endsWith()` call.
159
266
  */
160
- clearEndsWith(): StringSchemaBuilder<TResult, TRequired>;
267
+ clearEndsWith(): StringSchemaBuilder<string, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
161
268
  /**
162
269
  * Restricts string to match `regexp`.
270
+ * @param regexp regular expression pattern to match against
271
+ */
272
+ matches(regexp: RegExp,
273
+ /**
274
+ * Custom error message provider.
163
275
  */
164
- matches(regexp: RegExp): StringSchemaBuilder<TResult, TRequired>;
276
+ errorMessage?: ValidationErrorMessageProvider<StringSchemaBuilder<TResult, TRequired>>): StringSchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
165
277
  /**
166
278
  * Cancels `matches()` call.
167
279
  */
168
- clearMatches(): StringSchemaBuilder<TResult, TRequired>;
280
+ clearMatches(): StringSchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
169
281
  }
170
282
  /**
171
283
  * Creates a string schema restricted to be equal to `equals`.
172
- * @param equals number value
284
+ * @param equals string value the schema is restricted to
173
285
  */
174
286
  export declare function string<T extends string>(equals: T): StringSchemaBuilder<T, true>;
287
+ /**
288
+ * Creates a string schema restricted to be equal to `equals` with a custom error message.
289
+ * @param equals string value the schema is restricted to
290
+ */
291
+ export declare function string<T extends string>(equals: T, errorMessage: ValidationErrorMessageProvider<StringSchemaBuilder<T, true>>): StringSchemaBuilder<T, true>;
175
292
  export declare function string(): StringSchemaBuilder<string, true>;
176
293
  export {};