@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.
- package/README.md +1721 -104
- package/dist/builders/AnySchemaBuilder.d.ts +54 -14
- package/dist/builders/AnySchemaBuilder.js +2 -112
- package/dist/builders/AnySchemaBuilder.js.map +1 -0
- package/dist/builders/ArraySchemaBuilder.d.ts +126 -23
- package/dist/builders/ArraySchemaBuilder.js +2 -284
- package/dist/builders/ArraySchemaBuilder.js.map +1 -0
- package/dist/builders/BooleanSchemaBuilder.d.ts +109 -19
- package/dist/builders/BooleanSchemaBuilder.js +2 -150
- package/dist/builders/BooleanSchemaBuilder.js.map +1 -0
- package/dist/builders/DateSchemaBuilder.d.ts +154 -34
- package/dist/builders/DateSchemaBuilder.js +2 -433
- package/dist/builders/DateSchemaBuilder.js.map +1 -0
- package/dist/builders/ExternSchemaBuilder.d.ts +202 -0
- package/dist/builders/ExternSchemaBuilder.js +2 -0
- package/dist/builders/ExternSchemaBuilder.js.map +1 -0
- package/dist/builders/FunctionSchemaBuilder.d.ts +205 -18
- package/dist/builders/FunctionSchemaBuilder.js +2 -113
- package/dist/builders/FunctionSchemaBuilder.js.map +1 -0
- package/dist/builders/GenericSchemaBuilder.d.ts +294 -0
- package/dist/builders/LazySchemaBuilder.d.ts +169 -0
- package/dist/builders/NullSchemaBuilder.d.ts +162 -0
- package/dist/builders/NumberSchemaBuilder.d.ts +159 -31
- package/dist/builders/NumberSchemaBuilder.js +2 -386
- package/dist/builders/NumberSchemaBuilder.js.map +1 -0
- package/dist/builders/ObjectSchemaBuilder.d.ts +486 -61
- package/dist/builders/ObjectSchemaBuilder.js +2 -589
- package/dist/builders/ObjectSchemaBuilder.js.map +1 -0
- package/dist/builders/ParseStringSchemaBuilder.d.ts +204 -0
- package/dist/builders/ParseStringSchemaBuilder.js +2 -0
- package/dist/builders/ParseStringSchemaBuilder.js.map +1 -0
- package/dist/builders/PromiseSchemaBuilder.d.ts +213 -0
- package/dist/builders/PromiseSchemaBuilder.js +2 -0
- package/dist/builders/PromiseSchemaBuilder.js.map +1 -0
- package/dist/builders/PropertyValidationResult.d.ts +68 -0
- package/dist/builders/RecordSchemaBuilder.d.ts +343 -0
- package/dist/builders/RecordSchemaBuilder.js +2 -0
- package/dist/builders/RecordSchemaBuilder.js.map +1 -0
- package/dist/builders/SchemaBuilder.d.ts +907 -30
- package/dist/builders/StringSchemaBuilder.d.ts +154 -37
- package/dist/builders/StringSchemaBuilder.js +2 -414
- package/dist/builders/StringSchemaBuilder.js.map +1 -0
- package/dist/builders/TupleSchemaBuilder.d.ts +250 -0
- package/dist/builders/TupleSchemaBuilder.js +2 -0
- package/dist/builders/TupleSchemaBuilder.js.map +1 -0
- package/dist/builders/UnionSchemaBuilder.d.ts +141 -39
- package/dist/builders/UnionSchemaBuilder.js +2 -216
- package/dist/builders/UnionSchemaBuilder.js.map +1 -0
- package/dist/chunk-3JMDGYDT.js +2 -0
- package/dist/chunk-3JMDGYDT.js.map +1 -0
- package/dist/chunk-BUEVZ3KA.js +2 -0
- package/dist/chunk-BUEVZ3KA.js.map +1 -0
- package/dist/chunk-CFIJQ4GP.js +2 -0
- package/dist/chunk-CFIJQ4GP.js.map +1 -0
- package/dist/chunk-DY7J6RNN.js +2 -0
- package/dist/chunk-DY7J6RNN.js.map +1 -0
- package/dist/chunk-EIVZX4ZO.js +2 -0
- package/dist/chunk-EIVZX4ZO.js.map +1 -0
- package/dist/chunk-GXPV6UQK.js +2 -0
- package/dist/chunk-GXPV6UQK.js.map +1 -0
- package/dist/chunk-HN774HD7.js +2 -0
- package/dist/chunk-HN774HD7.js.map +1 -0
- package/dist/chunk-K6Z47OQY.js +2 -0
- package/dist/chunk-K6Z47OQY.js.map +1 -0
- package/dist/chunk-NUW3VXZV.js +2 -0
- package/dist/chunk-NUW3VXZV.js.map +1 -0
- package/dist/chunk-PHE4LIAN.js +2 -0
- package/dist/chunk-PHE4LIAN.js.map +1 -0
- package/dist/chunk-QARCEYGO.js +2 -0
- package/dist/chunk-QARCEYGO.js.map +1 -0
- package/dist/chunk-WDMJBGBD.js +2 -0
- package/dist/chunk-WDMJBGBD.js.map +1 -0
- package/dist/chunk-WQDYWDOE.js +2 -0
- package/dist/chunk-WQDYWDOE.js.map +1 -0
- package/dist/chunk-YQZHDMRF.js +2 -0
- package/dist/chunk-YQZHDMRF.js.map +1 -0
- package/dist/chunk-ZC6YBKCP.js +2 -0
- package/dist/chunk-ZC6YBKCP.js.map +1 -0
- package/dist/chunk-ZFI27R3L.js +2 -0
- package/dist/chunk-ZFI27R3L.js.map +1 -0
- package/dist/core.d.ts +28 -0
- package/dist/core.js +2 -0
- package/dist/core.js.map +1 -0
- package/dist/extension.d.ts +421 -0
- package/dist/extensions/array.d.ts +112 -0
- package/dist/extensions/enum.d.ts +190 -0
- package/dist/extensions/index.d.ts +112 -0
- package/dist/extensions/nullable.d.ts +26 -0
- package/dist/extensions/number.d.ts +228 -0
- package/dist/extensions/string.d.ts +332 -0
- package/dist/extensions/util.d.ts +45 -0
- package/dist/index.d.ts +10 -20
- package/dist/index.js +2 -19
- package/dist/index.js.map +1 -0
- package/dist/utils/transaction.d.ts +27 -4
- package/package.json +83 -7
- package/dist/builders/SchemaBuilder.js +0 -275
- package/dist/utils/transaction.js +0 -178
|
@@ -1,61 +1,248 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type BRAND, type InferType, SchemaBuilder, type ValidationContext, type ValidationErrorMessageProvider, type ValidationResult } from './SchemaBuilder.js';
|
|
2
2
|
type FunctionSchemaBuilderCreateProps<R extends boolean = true> = Partial<ReturnType<FunctionSchemaBuilder<R>['introspect']>>;
|
|
3
|
+
type InferParameters<TParams extends SchemaBuilder<any, any, any, any, any>[]> = {
|
|
4
|
+
[K in keyof TParams]: InferType<TParams[K]>;
|
|
5
|
+
};
|
|
3
6
|
/**
|
|
4
7
|
* Schema builder for functions. Allows to define a schema for a function.
|
|
5
|
-
* It can be
|
|
8
|
+
* It can be required or optional, and may carry typed parameter and return-type
|
|
9
|
+
* schemas so that the inferred TypeScript function signature is fully typed.
|
|
6
10
|
*
|
|
7
11
|
* **NOTE** this class is exported only to give opportunity to extend it
|
|
8
12
|
* by inheriting. It is not recommended to create an instance of this class
|
|
9
13
|
* directly. Use {@link func | func()} function instead.
|
|
10
14
|
*
|
|
11
|
-
* @example
|
|
15
|
+
* @example Basic validation
|
|
12
16
|
* ```ts
|
|
13
17
|
* const schema = func();
|
|
14
|
-
* const result =
|
|
18
|
+
* const result = schema.validate(() => {});
|
|
15
19
|
* // result.valid === true
|
|
16
20
|
* // result.object === () => {}
|
|
17
21
|
* ```
|
|
18
22
|
*
|
|
19
|
-
* @example
|
|
23
|
+
* @example Optional function schema
|
|
20
24
|
* ```ts
|
|
21
25
|
* const schema = func().optional();
|
|
22
|
-
* const result =
|
|
26
|
+
* const result = schema.validate(undefined);
|
|
23
27
|
* // result.valid === true
|
|
24
28
|
* // result.object === undefined
|
|
25
29
|
* ```
|
|
26
30
|
*
|
|
31
|
+
* @example Typed parameters and return type
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { func, string, number, InferType } from '@cleverbrush/schema';
|
|
34
|
+
*
|
|
35
|
+
* const greet = func()
|
|
36
|
+
* .addParameter(string()) // first param: string
|
|
37
|
+
* .addParameter(number().optional()) // second param: number | undefined
|
|
38
|
+
* .hasReturnType(string()); // return type: string
|
|
39
|
+
*
|
|
40
|
+
* type Greet = InferType<typeof greet>;
|
|
41
|
+
* // → (param0: string, param1: number | undefined) => string
|
|
42
|
+
*
|
|
43
|
+
* // Introspect at runtime
|
|
44
|
+
* const info = greet.introspect();
|
|
45
|
+
* // info.parameters → [StringSchemaBuilder, NumberSchemaBuilder]
|
|
46
|
+
* // info.returnType → StringSchemaBuilder
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
27
49
|
* @see {@link func}
|
|
28
50
|
*/
|
|
29
|
-
export declare class FunctionSchemaBuilder<TRequired extends boolean = true, TExplicitType = undefined, TResult = TExplicitType extends undefined ? (...args: any[]) => any : TExplicitType> extends SchemaBuilder<TResult, TRequired> {
|
|
30
|
-
|
|
31
|
-
|
|
51
|
+
export declare class FunctionSchemaBuilder<TRequired extends boolean = true, TNullable extends boolean = false, TExplicitType = undefined, THasDefault extends boolean = false, TExtensions = {}, TParameters extends SchemaBuilder<any, any, any, any, any>[] = [], TReturnTypeSchema extends SchemaBuilder<any, any, any, any, any> | undefined = undefined, TResult = TExplicitType extends undefined ? (...args: TParameters extends [] ? any[] : InferParameters<TParameters>) => TReturnTypeSchema extends SchemaBuilder<any, any, any, any, any> ? InferType<TReturnTypeSchema> : any : TExplicitType> extends SchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> {
|
|
52
|
+
#private;
|
|
53
|
+
/**
|
|
54
|
+
* @hidden
|
|
55
|
+
*/
|
|
56
|
+
static create(props: FunctionSchemaBuilderCreateProps<any>): FunctionSchemaBuilder<true, false, undefined, false, {}, [], undefined, (...args: any[]) => any>;
|
|
57
|
+
protected constructor(props: FunctionSchemaBuilderCreateProps<TRequired>);
|
|
32
58
|
/**
|
|
33
59
|
* @hidden
|
|
34
60
|
*/
|
|
35
|
-
hasType<T>(
|
|
61
|
+
hasType<T>(_notUsed?: T): FunctionSchemaBuilder<true, TNullable, T, THasDefault, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
36
62
|
/**
|
|
37
63
|
* @hidden
|
|
38
64
|
*/
|
|
39
|
-
clearHasType(): FunctionSchemaBuilder<TRequired, undefined
|
|
65
|
+
clearHasType(): FunctionSchemaBuilder<TRequired, TNullable, undefined, THasDefault, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
66
|
+
/**
|
|
67
|
+
* Returns an object describing the current schema configuration.
|
|
68
|
+
*
|
|
69
|
+
* In addition to the base fields exposed by {@link SchemaBuilder.introspect},
|
|
70
|
+
* the following fields are included:
|
|
71
|
+
*
|
|
72
|
+
* - `parameters` — an array of {@link SchemaBuilder} instances accumulated via
|
|
73
|
+
* {@link addParameter}. Each element describes one positional parameter of the
|
|
74
|
+
* function in the order they were added.
|
|
75
|
+
* - `returnType` — the {@link SchemaBuilder} set via {@link hasReturnType}, or
|
|
76
|
+
* `undefined` when no return-type schema has been configured.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* const schema = func()
|
|
81
|
+
* .addParameter(string())
|
|
82
|
+
* .addParameter(number())
|
|
83
|
+
* .hasReturnType(boolean());
|
|
84
|
+
*
|
|
85
|
+
* const info = schema.introspect();
|
|
86
|
+
* // info.parameters.length === 2
|
|
87
|
+
* // info.returnType instanceof BooleanSchemaBuilder
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
introspect(): {
|
|
91
|
+
/** List of parameter schemas added via {@link addParameter}. */
|
|
92
|
+
parameters: SchemaBuilder<any, any, any, any, any>[];
|
|
93
|
+
/** Return type schema set via {@link hasReturnType}, or `undefined` if not set. */
|
|
94
|
+
returnType: SchemaBuilder<any, any, any, any, any> | undefined;
|
|
95
|
+
type: string;
|
|
96
|
+
isRequired: boolean;
|
|
97
|
+
isNullable: boolean;
|
|
98
|
+
isReadonly: boolean;
|
|
99
|
+
preprocessors: readonly import("./SchemaBuilder.js").PreprocessorEntry<TResult>[];
|
|
100
|
+
validators: readonly import("./SchemaBuilder.js").ValidatorEntry<TResult>[];
|
|
101
|
+
requiredValidationErrorMessageProvider: ValidationErrorMessageProvider<SchemaBuilder<any, any, any, any, any>>;
|
|
102
|
+
extensions: {
|
|
103
|
+
[x: string]: unknown;
|
|
104
|
+
};
|
|
105
|
+
hasDefault: boolean;
|
|
106
|
+
defaultValue: TResult | (() => TResult) | undefined;
|
|
107
|
+
description: string | undefined;
|
|
108
|
+
schemaName: string | undefined;
|
|
109
|
+
hasCatch: boolean;
|
|
110
|
+
catchValue: TResult | (() => TResult) | undefined;
|
|
111
|
+
example: unknown;
|
|
112
|
+
};
|
|
113
|
+
/** {@inheritDoc SchemaBuilder.validate} */
|
|
114
|
+
validate(object: TResult, context?: ValidationContext): ValidationResult<TResult>;
|
|
115
|
+
/** {@inheritDoc SchemaBuilder.validateAsync} */
|
|
116
|
+
validateAsync(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
|
|
117
|
+
/**
|
|
118
|
+
* Performs synchronous validation of the schema over `object`.
|
|
119
|
+
* Throws if any preprocessor, validator, or error message provider returns a Promise.
|
|
120
|
+
* @param context Optional `ValidationContext` settings.
|
|
121
|
+
*/
|
|
122
|
+
protected _validate(object: TResult, context?: ValidationContext): ValidationResult<TResult>;
|
|
40
123
|
/**
|
|
41
|
-
* Performs
|
|
42
|
-
* validators,
|
|
124
|
+
* Performs async validation of the schema over `object`.
|
|
125
|
+
* Supports async preprocessors, validators, and error message providers.
|
|
43
126
|
* @param context Optional `ValidationContext` settings.
|
|
44
127
|
*/
|
|
45
|
-
|
|
128
|
+
protected _validateAsync(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
|
|
46
129
|
protected createFromProps<TReq extends boolean>(props: FunctionSchemaBuilderCreateProps<TReq>): this;
|
|
47
130
|
/**
|
|
48
131
|
* @hidden
|
|
49
132
|
*/
|
|
50
|
-
required(): FunctionSchemaBuilder<true, TExplicitType
|
|
133
|
+
required(errorMessage?: ValidationErrorMessageProvider): FunctionSchemaBuilder<true, TNullable, TExplicitType, THasDefault, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
134
|
+
/**
|
|
135
|
+
* @hidden
|
|
136
|
+
*/
|
|
137
|
+
optional(): FunctionSchemaBuilder<false, TNullable, TExplicitType, THasDefault, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
138
|
+
/**
|
|
139
|
+
* @hidden
|
|
140
|
+
*/
|
|
141
|
+
default(value: TResult | (() => TResult)): FunctionSchemaBuilder<true, TNullable, TExplicitType, true, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
51
142
|
/**
|
|
52
143
|
* @hidden
|
|
53
144
|
*/
|
|
54
|
-
|
|
145
|
+
clearDefault(): FunctionSchemaBuilder<TRequired, TNullable, TExplicitType, false, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
146
|
+
/**
|
|
147
|
+
* @hidden
|
|
148
|
+
*/
|
|
149
|
+
brand<TBrand extends string | symbol>(_name?: TBrand): FunctionSchemaBuilder<TRequired, TNullable, TResult & {
|
|
150
|
+
readonly [K in BRAND]: TBrand;
|
|
151
|
+
}, THasDefault, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
152
|
+
/**
|
|
153
|
+
* Marks the inferred type as `Readonly<Function>`. Sets the
|
|
154
|
+
* `isReadonly` introspection flag for tooling consistency.
|
|
155
|
+
*
|
|
156
|
+
* @see {@link SchemaBuilder.readonly}
|
|
157
|
+
*/
|
|
158
|
+
readonly(): FunctionSchemaBuilder<TRequired, TNullable, Readonly<TResult>, THasDefault, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
159
|
+
/**
|
|
160
|
+
* @hidden
|
|
161
|
+
*/
|
|
162
|
+
nullable(): FunctionSchemaBuilder<TRequired, true, TExplicitType, THasDefault, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
163
|
+
/**
|
|
164
|
+
* Appends a positional parameter schema to the function schema.
|
|
165
|
+
*
|
|
166
|
+
* Each call extends the inferred function signature by one parameter.
|
|
167
|
+
* The full list of parameter schemas is available at runtime via
|
|
168
|
+
* `introspect().parameters`.
|
|
169
|
+
*
|
|
170
|
+
* Parameter order matches the call order — the first `addParameter()` call
|
|
171
|
+
* defines the type of the first argument, the second call defines the second
|
|
172
|
+
* argument, and so on.
|
|
173
|
+
*
|
|
174
|
+
* @param schema - The schema describing the parameter. Pass an optional schema
|
|
175
|
+
* (e.g. `string().optional()`) to make the corresponding argument optional.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```ts
|
|
179
|
+
* const fn = func()
|
|
180
|
+
* .addParameter(string()) // (a: string, ...) => any
|
|
181
|
+
* .addParameter(number().optional()) // (..., b?: number) => any
|
|
182
|
+
* .addParameter(boolean()); // (..., c: boolean) => any
|
|
183
|
+
*
|
|
184
|
+
* type Fn = InferType<typeof fn>;
|
|
185
|
+
* // → (a: string, b: number | undefined, c: boolean) => any
|
|
186
|
+
* ```
|
|
187
|
+
*/
|
|
188
|
+
addParameter<TSchema extends SchemaBuilder<any, any, any, any, any>>(schema: TSchema): FunctionSchemaBuilder<TRequired, TNullable, TExplicitType, THasDefault, TExtensions, [
|
|
189
|
+
...TParameters,
|
|
190
|
+
TSchema
|
|
191
|
+
], TReturnTypeSchema> & TExtensions;
|
|
192
|
+
/**
|
|
193
|
+
* Sets the return type schema for the function schema.
|
|
194
|
+
*
|
|
195
|
+
* Replaces any previously set return type. The inferred function signature
|
|
196
|
+
* gains a concrete return type instead of `any`. The schema is accessible at
|
|
197
|
+
* runtime via `introspect().returnType`.
|
|
198
|
+
*
|
|
199
|
+
* @param schema - The schema describing the return type of the function.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* ```ts
|
|
203
|
+
* const fn = func()
|
|
204
|
+
* .addParameter(string())
|
|
205
|
+
* .hasReturnType(number());
|
|
206
|
+
*
|
|
207
|
+
* type Fn = InferType<typeof fn>;
|
|
208
|
+
* // → (param0: string) => number
|
|
209
|
+
*
|
|
210
|
+
* fn.introspect().returnType; // NumberSchemaBuilder
|
|
211
|
+
* ```
|
|
212
|
+
*/
|
|
213
|
+
hasReturnType<TSchema extends SchemaBuilder<any, any, any, any, any>>(schema: TSchema): FunctionSchemaBuilder<TRequired, TNullable, TExplicitType, THasDefault, TExtensions, TParameters, TSchema> & TExtensions;
|
|
214
|
+
/**
|
|
215
|
+
* @hidden
|
|
216
|
+
*/
|
|
217
|
+
notNullable(): FunctionSchemaBuilder<TRequired, false, TExplicitType, THasDefault, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
55
218
|
}
|
|
56
219
|
/**
|
|
57
|
-
* Creates a `function` schema.
|
|
58
|
-
*
|
|
220
|
+
* Creates a `function` schema that validates the value is a JavaScript function.
|
|
221
|
+
*
|
|
222
|
+
* The returned builder is immutable and fully chainable. Use
|
|
223
|
+
* {@link FunctionSchemaBuilder.addParameter} to annotate the expected parameter
|
|
224
|
+
* types and {@link FunctionSchemaBuilder.hasReturnType} to annotate the return
|
|
225
|
+
* type — the inferred TypeScript function signature is updated automatically.
|
|
226
|
+
*
|
|
227
|
+
* @returns A new {@link FunctionSchemaBuilder} with `isRequired` set to `true`.
|
|
228
|
+
*
|
|
229
|
+
* @example
|
|
230
|
+
* ```ts
|
|
231
|
+
* import { func, string, number, InferType } from '@cleverbrush/schema';
|
|
232
|
+
*
|
|
233
|
+
* const schema = func()
|
|
234
|
+
* .addParameter(string())
|
|
235
|
+
* .addParameter(number().optional())
|
|
236
|
+
* .hasReturnType(string());
|
|
237
|
+
*
|
|
238
|
+
* type Fn = InferType<typeof schema>;
|
|
239
|
+
* // → (param0: string, param1?: number) => string
|
|
240
|
+
*
|
|
241
|
+
* schema.validate(() => 'hello'); // { valid: true }
|
|
242
|
+
* schema.validate('not a fn'); // { valid: false }
|
|
243
|
+
* ```
|
|
244
|
+
*
|
|
245
|
+
* @see {@link FunctionSchemaBuilder}
|
|
59
246
|
*/
|
|
60
247
|
export declare const func: () => FunctionSchemaBuilder<true>;
|
|
61
248
|
export {};
|
|
@@ -1,113 +1,2 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
* Schema builder for functions. Allows to define a schema for a function.
|
|
4
|
-
* It can be: required or optional.
|
|
5
|
-
*
|
|
6
|
-
* **NOTE** this class is exported only to give opportunity to extend it
|
|
7
|
-
* by inheriting. It is not recommended to create an instance of this class
|
|
8
|
-
* directly. Use {@link func | func()} function instead.
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
* ```ts
|
|
12
|
-
* const schema = func();
|
|
13
|
-
* const result = await schema.validate(() => {});
|
|
14
|
-
* // result.valid === true
|
|
15
|
-
* // result.object === () => {}
|
|
16
|
-
* ```
|
|
17
|
-
*
|
|
18
|
-
* @example
|
|
19
|
-
* ```ts
|
|
20
|
-
* const schema = func().optional();
|
|
21
|
-
* const result = await schema.validate(undefined);
|
|
22
|
-
* // result.valid === true
|
|
23
|
-
* // result.object === undefined
|
|
24
|
-
* ```
|
|
25
|
-
*
|
|
26
|
-
* @see {@link func}
|
|
27
|
-
*/
|
|
28
|
-
export class FunctionSchemaBuilder extends SchemaBuilder {
|
|
29
|
-
static create(props) {
|
|
30
|
-
return new FunctionSchemaBuilder({
|
|
31
|
-
type: 'function',
|
|
32
|
-
...props
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
constructor(props) {
|
|
36
|
-
super(props);
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* @hidden
|
|
40
|
-
*/
|
|
41
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
42
|
-
hasType(notUsed) {
|
|
43
|
-
return this.createFromProps({
|
|
44
|
-
...this.introspect()
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* @hidden
|
|
49
|
-
*/
|
|
50
|
-
clearHasType() {
|
|
51
|
-
return this.createFromProps({
|
|
52
|
-
...this.introspect()
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Performs validion of the schema over `object`. Basically runs
|
|
57
|
-
* validators, preprocessors and checks for required (if schema is not optional).
|
|
58
|
-
* @param context Optional `ValidationContext` settings.
|
|
59
|
-
*/
|
|
60
|
-
async validate(object, context) {
|
|
61
|
-
const superResult = await super.preValidate(object, context);
|
|
62
|
-
const { valid, context: prevalidationContext, transaction: preValidationTransaction, errors } = superResult;
|
|
63
|
-
const { path } = prevalidationContext;
|
|
64
|
-
if (!valid) {
|
|
65
|
-
return { valid, errors };
|
|
66
|
-
}
|
|
67
|
-
const { object: { validatedObject: objToValidate } } = preValidationTransaction;
|
|
68
|
-
if ((typeof objToValidate === 'undefined' || objToValidate === null) &&
|
|
69
|
-
this.isRequired === false) {
|
|
70
|
-
return {
|
|
71
|
-
valid: true,
|
|
72
|
-
object: objToValidate
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
if (typeof objToValidate !== 'function') {
|
|
76
|
-
return {
|
|
77
|
-
valid: false,
|
|
78
|
-
errors: [
|
|
79
|
-
{
|
|
80
|
-
message: `expected type function, but saw ${typeof objToValidate}`,
|
|
81
|
-
path: path
|
|
82
|
-
}
|
|
83
|
-
]
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
return {
|
|
87
|
-
valid: true,
|
|
88
|
-
object: objToValidate
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
createFromProps(props) {
|
|
92
|
-
return FunctionSchemaBuilder.create(props);
|
|
93
|
-
}
|
|
94
|
-
/**
|
|
95
|
-
* @hidden
|
|
96
|
-
*/
|
|
97
|
-
required() {
|
|
98
|
-
return super.required();
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* @hidden
|
|
102
|
-
*/
|
|
103
|
-
optional() {
|
|
104
|
-
return super.optional();
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Creates a `function` schema.
|
|
109
|
-
* @retuns {@link FunctionSchemaBuilder}
|
|
110
|
-
*/
|
|
111
|
-
export const func = () => FunctionSchemaBuilder.create({
|
|
112
|
-
isRequired: true
|
|
113
|
-
});
|
|
1
|
+
import{a,b}from"../chunk-ZC6YBKCP.js";import"../chunk-3JMDGYDT.js";export{a as FunctionSchemaBuilder,b as func};
|
|
2
|
+
//# sourceMappingURL=FunctionSchemaBuilder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { type BRAND, SchemaBuilder, type ValidationContext, type ValidationErrorMessageProvider, type ValidationResult } from './SchemaBuilder.js';
|
|
2
|
+
type GenericSchemaBuilderCreateProps<TRequired extends boolean = true> = Partial<ReturnType<GenericSchemaBuilder<any, TRequired>['introspect']>>;
|
|
3
|
+
/**
|
|
4
|
+
* Schema builder that wraps a generic template function, enabling reusable
|
|
5
|
+
* parameterized schemas. Call {@link GenericSchemaBuilder.apply | `.apply()`}
|
|
6
|
+
* with concrete schema arguments to obtain a fully typed concrete schema
|
|
7
|
+
* builder whose TypeScript type is inferred from the template function's
|
|
8
|
+
* generic signature.
|
|
9
|
+
*
|
|
10
|
+
* **NOTE** this class is exported only to give opportunity to extend it
|
|
11
|
+
* by inheriting. It is not recommended to create an instance of this class
|
|
12
|
+
* directly. Use {@link generic | generic()} function instead.
|
|
13
|
+
*
|
|
14
|
+
* @example Single type parameter
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { generic, object, array, number, string, InferType } from '@cleverbrush/schema';
|
|
17
|
+
*
|
|
18
|
+
* const PaginatedList = generic(
|
|
19
|
+
* <T extends SchemaBuilder<any, any, any, any, any>>(itemSchema: T) =>
|
|
20
|
+
* object({
|
|
21
|
+
* items: array(itemSchema),
|
|
22
|
+
* total: number(),
|
|
23
|
+
* page: number(),
|
|
24
|
+
* })
|
|
25
|
+
* );
|
|
26
|
+
*
|
|
27
|
+
* const userSchema = object({ name: string(), age: number() });
|
|
28
|
+
* const PaginatedUsers = PaginatedList.apply(userSchema);
|
|
29
|
+
*
|
|
30
|
+
* type PaginatedUsersType = InferType<typeof PaginatedUsers>;
|
|
31
|
+
* // → { items: { name: string; age: number }[]; total: number; page: number }
|
|
32
|
+
* ```
|
|
33
|
+
*
|
|
34
|
+
* @example Multiple type parameters
|
|
35
|
+
* ```ts
|
|
36
|
+
* const Result = generic(
|
|
37
|
+
* <T extends SchemaBuilder<any, any, any, any, any>,
|
|
38
|
+
* E extends SchemaBuilder<any, any, any, any, any>>(
|
|
39
|
+
* valueSchema: T,
|
|
40
|
+
* errorSchema: E
|
|
41
|
+
* ) =>
|
|
42
|
+
* object({
|
|
43
|
+
* ok: boolean(),
|
|
44
|
+
* value: valueSchema.optional(),
|
|
45
|
+
* error: errorSchema.optional(),
|
|
46
|
+
* })
|
|
47
|
+
* );
|
|
48
|
+
*
|
|
49
|
+
* const StringResult = Result.apply(string(), number());
|
|
50
|
+
* // InferType → { ok: boolean; value?: string; error?: number }
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @example With default arguments (enables direct `.validate()` on the template)
|
|
54
|
+
* ```ts
|
|
55
|
+
* const AnyList = generic(
|
|
56
|
+
* [any()], // default args — one per template parameter
|
|
57
|
+
* <T extends SchemaBuilder<any, any, any, any, any>>(itemSchema: T) =>
|
|
58
|
+
* object({ items: array(itemSchema), total: number() })
|
|
59
|
+
* );
|
|
60
|
+
*
|
|
61
|
+
* // Validate directly using defaults:
|
|
62
|
+
* AnyList.validate({ items: [1, 'two', true], total: 3 }); // valid
|
|
63
|
+
*
|
|
64
|
+
* // Or apply concrete schemas first:
|
|
65
|
+
* AnyList.apply(string()).validate({ items: ['a', 'b'], total: 2 }); // valid
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* @see {@link generic}
|
|
69
|
+
*
|
|
70
|
+
* @typeParam TFn - The generic template function type. Its return type
|
|
71
|
+
* determines `TResult` (the validated value type) when no explicit type
|
|
72
|
+
* override has been applied via `.hasType<T>()`.
|
|
73
|
+
* @typeParam TRequired - `true` when the schema is required (default),
|
|
74
|
+
* `false` after calling `.optional()`. Governs whether `undefined` is a
|
|
75
|
+
* valid value.
|
|
76
|
+
* @typeParam TNullable - `true` after calling `.nullable()`. Governs whether
|
|
77
|
+
* `null` is a valid value.
|
|
78
|
+
* @typeParam TExplicitType - Type override set via `.hasType<T>()`. When
|
|
79
|
+
* `undefined` (the default), `TResult` is derived from `TFn`'s return type.
|
|
80
|
+
* @typeParam THasDefault - `true` after calling `.default(value)`. Governs
|
|
81
|
+
* whether `InferType` emits `T` instead of `T | undefined` for optional
|
|
82
|
+
* schemas with a default.
|
|
83
|
+
* @typeParam TExtensions - Object type carrying extension methods added via
|
|
84
|
+
* `withExtensions()`. Defaults to `{}`.
|
|
85
|
+
* @typeParam TResult - The inferred result type: `TExplicitType` when set,
|
|
86
|
+
* otherwise the value type inferred from `ReturnType<TFn>`.
|
|
87
|
+
*/
|
|
88
|
+
export declare class GenericSchemaBuilder<TFn extends (...args: any[]) => SchemaBuilder<any, any, any, any, any>, TRequired extends boolean = true, TNullable extends boolean = false, TExplicitType = undefined, THasDefault extends boolean = false, TExtensions = {}, TResult = TExplicitType extends undefined ? ReturnType<TFn> extends SchemaBuilder<infer R, any, any, any, any> ? R : any : TExplicitType> extends SchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> {
|
|
89
|
+
#private;
|
|
90
|
+
/**
|
|
91
|
+
* Applies the template function with concrete schema arguments, returning
|
|
92
|
+
* a fully typed concrete schema builder. TypeScript infers the result type
|
|
93
|
+
* from the template function's own generic signature.
|
|
94
|
+
*
|
|
95
|
+
* The returned builder is independent of this `GenericSchemaBuilder` and
|
|
96
|
+
* can be used like any other schema: `.validate()`, `.optional()`, etc.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* const Wrapper = generic(
|
|
101
|
+
* <T extends SchemaBuilder<any, any, any, any, any>>(schema: T) =>
|
|
102
|
+
* object({ data: schema })
|
|
103
|
+
* );
|
|
104
|
+
*
|
|
105
|
+
* const s = Wrapper.apply(string());
|
|
106
|
+
* // InferType<typeof s> → { data: string }
|
|
107
|
+
* s.validate({ data: 'hello' }); // { valid: true }
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
readonly apply: TFn;
|
|
111
|
+
/**
|
|
112
|
+
* @hidden
|
|
113
|
+
*/
|
|
114
|
+
static create(props: GenericSchemaBuilderCreateProps<any>): GenericSchemaBuilder<(...args: any[]) => SchemaBuilder<any, any, any, any, any>, true, false, undefined, false, {}, any>;
|
|
115
|
+
protected constructor(props: GenericSchemaBuilderCreateProps<TRequired>);
|
|
116
|
+
/**
|
|
117
|
+
* Returns an object describing the current schema configuration.
|
|
118
|
+
*
|
|
119
|
+
* In addition to the base fields exposed by {@link SchemaBuilder.introspect},
|
|
120
|
+
* the following fields are included:
|
|
121
|
+
*
|
|
122
|
+
* - `templateFn` — the template function passed to {@link generic}.
|
|
123
|
+
* - `defaults` — the default argument list passed to the two-argument
|
|
124
|
+
* form of {@link generic}, or `undefined` when no defaults were provided.
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* const schema = generic([string()], <T>(s: T) => object({ data: s }));
|
|
129
|
+
*
|
|
130
|
+
* const info = schema.introspect();
|
|
131
|
+
* // info.type → 'generic'
|
|
132
|
+
* // info.templateFn → [Function]
|
|
133
|
+
* // info.defaults → [StringSchemaBuilder]
|
|
134
|
+
* ```
|
|
135
|
+
*/
|
|
136
|
+
introspect(): {
|
|
137
|
+
/** Template function passed to {@link generic}. */
|
|
138
|
+
templateFn: ((...args: any[]) => SchemaBuilder<any, any, any, any, any>) | undefined;
|
|
139
|
+
/** Default positional arguments for the template function, or `undefined`. */
|
|
140
|
+
defaults: readonly any[] | undefined;
|
|
141
|
+
type: string;
|
|
142
|
+
isRequired: boolean;
|
|
143
|
+
isNullable: boolean;
|
|
144
|
+
isReadonly: boolean;
|
|
145
|
+
preprocessors: readonly import("./SchemaBuilder.js").PreprocessorEntry<TResult>[];
|
|
146
|
+
validators: readonly import("./SchemaBuilder.js").ValidatorEntry<TResult>[];
|
|
147
|
+
requiredValidationErrorMessageProvider: ValidationErrorMessageProvider<SchemaBuilder<any, any, any, any, any>>;
|
|
148
|
+
extensions: {
|
|
149
|
+
[x: string]: unknown;
|
|
150
|
+
};
|
|
151
|
+
hasDefault: boolean;
|
|
152
|
+
defaultValue: TResult | (() => TResult) | undefined;
|
|
153
|
+
description: string | undefined;
|
|
154
|
+
schemaName: string | undefined;
|
|
155
|
+
hasCatch: boolean;
|
|
156
|
+
catchValue: TResult | (() => TResult) | undefined;
|
|
157
|
+
example: unknown;
|
|
158
|
+
};
|
|
159
|
+
/** {@inheritDoc SchemaBuilder.validate} */
|
|
160
|
+
validate(object: TResult, context?: ValidationContext): ValidationResult<TResult>;
|
|
161
|
+
/** {@inheritDoc SchemaBuilder.validateAsync} */
|
|
162
|
+
validateAsync(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
|
|
163
|
+
/**
|
|
164
|
+
* Performs synchronous validation of the schema over `object`.
|
|
165
|
+
* Throws if any preprocessor, validator, or error message provider returns a Promise.
|
|
166
|
+
* @param context Optional `ValidationContext` settings.
|
|
167
|
+
*/
|
|
168
|
+
protected _validate(object: TResult, context?: ValidationContext): ValidationResult<TResult>;
|
|
169
|
+
/**
|
|
170
|
+
* Performs async validation of the schema over `object`.
|
|
171
|
+
* Supports async preprocessors, validators, and error message providers.
|
|
172
|
+
* @param context Optional `ValidationContext` settings.
|
|
173
|
+
*/
|
|
174
|
+
protected _validateAsync(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
|
|
175
|
+
protected createFromProps<TReq extends boolean>(props: GenericSchemaBuilderCreateProps<TReq>): this;
|
|
176
|
+
/**
|
|
177
|
+
* @hidden
|
|
178
|
+
*/
|
|
179
|
+
hasType<T>(_notUsed?: T): GenericSchemaBuilder<TFn, true, TNullable, T, THasDefault, TExtensions> & TExtensions;
|
|
180
|
+
/**
|
|
181
|
+
* @hidden
|
|
182
|
+
*/
|
|
183
|
+
clearHasType(): GenericSchemaBuilder<TFn, TRequired, TNullable, undefined, THasDefault, TExtensions> & TExtensions;
|
|
184
|
+
/**
|
|
185
|
+
* @hidden
|
|
186
|
+
*/
|
|
187
|
+
required(errorMessage?: ValidationErrorMessageProvider): GenericSchemaBuilder<TFn, true, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
188
|
+
/**
|
|
189
|
+
* @hidden
|
|
190
|
+
*/
|
|
191
|
+
optional(): GenericSchemaBuilder<TFn, false, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
192
|
+
/**
|
|
193
|
+
* @hidden
|
|
194
|
+
*/
|
|
195
|
+
nullable(): GenericSchemaBuilder<TFn, TRequired, true, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
196
|
+
/**
|
|
197
|
+
* @hidden
|
|
198
|
+
*/
|
|
199
|
+
notNullable(): GenericSchemaBuilder<TFn, TRequired, false, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
200
|
+
/**
|
|
201
|
+
* @hidden
|
|
202
|
+
*/
|
|
203
|
+
default(value: TResult | (() => TResult)): GenericSchemaBuilder<TFn, true, TNullable, TExplicitType, true, TExtensions> & TExtensions;
|
|
204
|
+
/**
|
|
205
|
+
* @hidden
|
|
206
|
+
*/
|
|
207
|
+
clearDefault(): GenericSchemaBuilder<TFn, TRequired, TNullable, TExplicitType, false, TExtensions> & TExtensions;
|
|
208
|
+
/**
|
|
209
|
+
* @hidden
|
|
210
|
+
*/
|
|
211
|
+
brand<TBrand extends string | symbol>(_name?: TBrand): GenericSchemaBuilder<TFn, TRequired, TNullable, TResult & {
|
|
212
|
+
readonly [K in BRAND]: TBrand;
|
|
213
|
+
}, THasDefault, TExtensions> & TExtensions;
|
|
214
|
+
/**
|
|
215
|
+
* @hidden
|
|
216
|
+
*/
|
|
217
|
+
readonly(): GenericSchemaBuilder<TFn, TRequired, TNullable, Readonly<TResult>, THasDefault, TExtensions> & TExtensions;
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Creates a generic schema template — a reusable, parameterized schema factory
|
|
221
|
+
* whose TypeScript type is inferred from the template function's generic
|
|
222
|
+
* signature.
|
|
223
|
+
*
|
|
224
|
+
* Call {@link GenericSchemaBuilder.apply | `.apply()`} on the returned builder
|
|
225
|
+
* to instantiate the template with concrete schema arguments and receive a
|
|
226
|
+
* fully typed concrete schema.
|
|
227
|
+
*
|
|
228
|
+
* There are two overloads:
|
|
229
|
+
*
|
|
230
|
+
* 1. **`generic(templateFn)`** — Provide only the template function. The
|
|
231
|
+
* template must be called via `.apply()` before validation.
|
|
232
|
+
* 2. **`generic(defaults, templateFn)`** — Provide positional default arguments
|
|
233
|
+
* followed by the template function. The template can be validated directly
|
|
234
|
+
* using those defaults (without calling `.apply()` first).
|
|
235
|
+
*
|
|
236
|
+
* @param templateFn - A (generic) function that accepts schema arguments and
|
|
237
|
+
* returns a concrete schema. TypeScript infers the result type from this
|
|
238
|
+
* function's generic signature when `.apply()` is called.
|
|
239
|
+
*
|
|
240
|
+
* @returns A new {@link GenericSchemaBuilder} with `isRequired` set to `true`.
|
|
241
|
+
*
|
|
242
|
+
* @example Single type parameter
|
|
243
|
+
* ```ts
|
|
244
|
+
* import { generic, object, array, number, string, any, InferType } from '@cleverbrush/schema';
|
|
245
|
+
*
|
|
246
|
+
* const PaginatedList = generic(
|
|
247
|
+
* <T extends SchemaBuilder<any, any, any, any, any>>(itemSchema: T) =>
|
|
248
|
+
* object({ items: array(itemSchema), total: number(), page: number() })
|
|
249
|
+
* );
|
|
250
|
+
*
|
|
251
|
+
* const UserList = PaginatedList.apply(object({ name: string() }));
|
|
252
|
+
* type UserListType = InferType<typeof UserList>;
|
|
253
|
+
* // → { items: { name: string }[]; total: number; page: number }
|
|
254
|
+
*
|
|
255
|
+
* UserList.validate({ items: [{ name: 'Alice' }], total: 1, page: 1 }); // valid
|
|
256
|
+
* ```
|
|
257
|
+
*
|
|
258
|
+
* @example Multiple type parameters
|
|
259
|
+
* ```ts
|
|
260
|
+
* const Result = generic(
|
|
261
|
+
* <T extends SchemaBuilder<any, any, any, any, any>,
|
|
262
|
+
* E extends SchemaBuilder<any, any, any, any, any>>(
|
|
263
|
+
* valueSchema: T,
|
|
264
|
+
* errorSchema: E
|
|
265
|
+
* ) =>
|
|
266
|
+
* union(
|
|
267
|
+
* object({ ok: boolean().equalsTo(true), value: valueSchema }),
|
|
268
|
+
* object({ ok: boolean().equalsTo(false), error: errorSchema })
|
|
269
|
+
* )
|
|
270
|
+
* );
|
|
271
|
+
*
|
|
272
|
+
* const StringResult = Result.apply(string(), number());
|
|
273
|
+
* ```
|
|
274
|
+
*
|
|
275
|
+
* @example With defaults (enables direct validation on the template)
|
|
276
|
+
* ```ts
|
|
277
|
+
* const AnyList = generic(
|
|
278
|
+
* [any()], // default args — positional, one per template parameter
|
|
279
|
+
* <T extends SchemaBuilder<any, any, any, any, any>>(itemSchema: T) =>
|
|
280
|
+
* object({ items: array(itemSchema), total: number() })
|
|
281
|
+
* );
|
|
282
|
+
*
|
|
283
|
+
* // Validate directly — uses the default any() schema:
|
|
284
|
+
* AnyList.validate({ items: [1, 'two'], total: 2 }); // valid
|
|
285
|
+
*
|
|
286
|
+
* // Or apply concrete schemas first:
|
|
287
|
+
* AnyList.apply(string()).validate({ items: ['x'], total: 1 }); // valid
|
|
288
|
+
* ```
|
|
289
|
+
*
|
|
290
|
+
* @see {@link GenericSchemaBuilder}
|
|
291
|
+
*/
|
|
292
|
+
export declare function generic<TFn extends (...args: any[]) => SchemaBuilder<any, any, any, any, any>>(templateFn: TFn): GenericSchemaBuilder<TFn, true, false, undefined, false, {}>;
|
|
293
|
+
export declare function generic<TFn extends (...args: any[]) => SchemaBuilder<any, any, any, any, any>>(defaults: readonly any[], templateFn: TFn): GenericSchemaBuilder<TFn, true, false, undefined, false, {}>;
|
|
294
|
+
export {};
|