@cleverbrush/schema 2.0.0 → 3.0.1
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 +412 -8
- package/dist/builders/AnySchemaBuilder.js +1 -1
- package/dist/builders/ArraySchemaBuilder.d.ts +2 -0
- package/dist/builders/ArraySchemaBuilder.js +1 -1
- package/dist/builders/BooleanSchemaBuilder.d.ts +15 -0
- package/dist/builders/BooleanSchemaBuilder.js +1 -1
- package/dist/builders/DateSchemaBuilder.d.ts +20 -0
- package/dist/builders/DateSchemaBuilder.js +1 -1
- package/dist/builders/ExternSchemaBuilder.d.ts +2 -0
- package/dist/builders/ExternSchemaBuilder.js +1 -1
- package/dist/builders/FunctionSchemaBuilder.d.ts +162 -18
- package/dist/builders/FunctionSchemaBuilder.js +1 -1
- package/dist/builders/GenericSchemaBuilder.d.ts +294 -0
- package/dist/builders/LazySchemaBuilder.d.ts +2 -0
- package/dist/builders/NumberSchemaBuilder.d.ts +16 -0
- package/dist/builders/NumberSchemaBuilder.js +1 -1
- package/dist/builders/ObjectSchemaBuilder.d.ts +262 -42
- package/dist/builders/ObjectSchemaBuilder.js +1 -1
- 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/RecordSchemaBuilder.d.ts +2 -0
- package/dist/builders/RecordSchemaBuilder.js +1 -1
- package/dist/builders/SchemaBuilder.d.ts +89 -0
- package/dist/builders/StringSchemaBuilder.d.ts +2 -0
- package/dist/builders/StringSchemaBuilder.js +1 -1
- package/dist/builders/TupleSchemaBuilder.d.ts +2 -0
- package/dist/builders/TupleSchemaBuilder.js +1 -1
- package/dist/builders/UnionSchemaBuilder.d.ts +8 -0
- package/dist/builders/UnionSchemaBuilder.js +1 -1
- package/dist/chunk-3JMDGYDT.js +2 -0
- package/dist/chunk-3JMDGYDT.js.map +1 -0
- package/dist/{chunk-KESLT5EE.js → chunk-BUEVZ3KA.js} +2 -2
- package/dist/{chunk-KESLT5EE.js.map → chunk-BUEVZ3KA.js.map} +1 -1
- package/dist/{chunk-KFTO7TMB.js → chunk-CFIJQ4GP.js} +2 -2
- package/dist/{chunk-KFTO7TMB.js.map → chunk-CFIJQ4GP.js.map} +1 -1
- package/dist/chunk-DY7J6RNN.js +2 -0
- package/dist/chunk-DY7J6RNN.js.map +1 -0
- package/dist/{chunk-YWBNVHR6.js → chunk-EIVZX4ZO.js} +2 -2
- package/dist/{chunk-YWBNVHR6.js.map → chunk-EIVZX4ZO.js.map} +1 -1
- package/dist/chunk-GXPV6UQK.js +2 -0
- package/dist/chunk-GXPV6UQK.js.map +1 -0
- package/dist/{chunk-ZAN4ZHCJ.js → chunk-HN774HD7.js} +2 -2
- 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-MKAYFPAR.js → chunk-PHE4LIAN.js} +2 -2
- 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-FWUEUW2N.js → chunk-YQZHDMRF.js} +2 -2
- package/dist/chunk-ZC6YBKCP.js +2 -0
- package/dist/chunk-ZC6YBKCP.js.map +1 -0
- package/dist/{chunk-IJ6FO37G.js → chunk-ZFI27R3L.js} +2 -2
- package/dist/core.d.ts +7 -2
- package/dist/core.js +1 -1
- package/dist/extension.d.ts +10 -2
- package/dist/extensions/index.d.ts +5 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/package.json +12 -4
- package/dist/chunk-BFCMDDTX.js +0 -2
- package/dist/chunk-BFCMDDTX.js.map +0 -1
- package/dist/chunk-CTP4RHDG.js +0 -2
- package/dist/chunk-CTP4RHDG.js.map +0 -1
- package/dist/chunk-EUQ5AE3H.js +0 -2
- package/dist/chunk-EUQ5AE3H.js.map +0 -1
- package/dist/chunk-NXPH3ZUW.js +0 -2
- package/dist/chunk-NXPH3ZUW.js.map +0 -1
- package/dist/chunk-S5TC6NSU.js +0 -2
- package/dist/chunk-S5TC6NSU.js.map +0 -1
- package/dist/chunk-Z7QZ7IXT.js +0 -2
- package/dist/chunk-Z7QZ7IXT.js.map +0 -1
- package/dist/chunk-ZUPYV5TI.js +0 -2
- package/dist/chunk-ZUPYV5TI.js.map +0 -1
- /package/dist/{chunk-ZAN4ZHCJ.js.map → chunk-HN774HD7.js.map} +0 -0
- /package/dist/{chunk-MKAYFPAR.js.map → chunk-PHE4LIAN.js.map} +0 -0
- /package/dist/{chunk-FWUEUW2N.js.map → chunk-YQZHDMRF.js.map} +0 -0
- /package/dist/{chunk-IJ6FO37G.js.map → chunk-ZFI27R3L.js.map} +0 -0
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
import { type BRAND, SchemaBuilder, type ValidationContext, type ValidationErrorMessageProvider, type ValidationResult } from './SchemaBuilder.js';
|
|
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
18
|
* const result = schema.validate(() => {});
|
|
@@ -16,7 +20,7 @@ type FunctionSchemaBuilderCreateProps<R extends boolean = true> = Partial<Return
|
|
|
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
26
|
* const result = schema.validate(undefined);
|
|
@@ -24,23 +28,88 @@ type FunctionSchemaBuilderCreateProps<R extends boolean = true> = Partial<Return
|
|
|
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, TNullable extends boolean = false, TExplicitType = undefined, THasDefault extends boolean = false, TExtensions = {}, TResult = TExplicitType extends undefined ? (...args: any[]) => any : TExplicitType> extends SchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> {
|
|
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> {
|
|
30
52
|
#private;
|
|
31
53
|
/**
|
|
32
54
|
* @hidden
|
|
33
55
|
*/
|
|
34
|
-
static create(props: FunctionSchemaBuilderCreateProps<any>): FunctionSchemaBuilder<true, false, undefined, false, {}, (...args: any[]) => any>;
|
|
56
|
+
static create(props: FunctionSchemaBuilderCreateProps<any>): FunctionSchemaBuilder<true, false, undefined, false, {}, [], undefined, (...args: any[]) => any>;
|
|
35
57
|
protected constructor(props: FunctionSchemaBuilderCreateProps<TRequired>);
|
|
36
58
|
/**
|
|
37
59
|
* @hidden
|
|
38
60
|
*/
|
|
39
|
-
hasType<T>(_notUsed?: T): FunctionSchemaBuilder<true, TNullable, T, THasDefault, TExtensions> & TExtensions;
|
|
61
|
+
hasType<T>(_notUsed?: T): FunctionSchemaBuilder<true, TNullable, T, THasDefault, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
40
62
|
/**
|
|
41
63
|
* @hidden
|
|
42
64
|
*/
|
|
43
|
-
clearHasType(): FunctionSchemaBuilder<TRequired, TNullable, undefined, THasDefault, TExtensions> & TExtensions;
|
|
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
|
+
};
|
|
44
113
|
/** {@inheritDoc SchemaBuilder.validate} */
|
|
45
114
|
validate(object: TResult, context?: ValidationContext): ValidationResult<TResult>;
|
|
46
115
|
/** {@inheritDoc SchemaBuilder.validateAsync} */
|
|
@@ -61,44 +130,119 @@ export declare class FunctionSchemaBuilder<TRequired extends boolean = true, TNu
|
|
|
61
130
|
/**
|
|
62
131
|
* @hidden
|
|
63
132
|
*/
|
|
64
|
-
required(errorMessage?: ValidationErrorMessageProvider): FunctionSchemaBuilder<true, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
133
|
+
required(errorMessage?: ValidationErrorMessageProvider): FunctionSchemaBuilder<true, TNullable, TExplicitType, THasDefault, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
65
134
|
/**
|
|
66
135
|
* @hidden
|
|
67
136
|
*/
|
|
68
|
-
optional(): FunctionSchemaBuilder<false, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
137
|
+
optional(): FunctionSchemaBuilder<false, TNullable, TExplicitType, THasDefault, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
69
138
|
/**
|
|
70
139
|
* @hidden
|
|
71
140
|
*/
|
|
72
|
-
default(value: TResult | (() => TResult)): FunctionSchemaBuilder<true, TNullable, TExplicitType, true, TExtensions> & TExtensions;
|
|
141
|
+
default(value: TResult | (() => TResult)): FunctionSchemaBuilder<true, TNullable, TExplicitType, true, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
73
142
|
/**
|
|
74
143
|
* @hidden
|
|
75
144
|
*/
|
|
76
|
-
clearDefault(): FunctionSchemaBuilder<TRequired, TNullable, TExplicitType, false, TExtensions> & TExtensions;
|
|
145
|
+
clearDefault(): FunctionSchemaBuilder<TRequired, TNullable, TExplicitType, false, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
77
146
|
/**
|
|
78
147
|
* @hidden
|
|
79
148
|
*/
|
|
80
149
|
brand<TBrand extends string | symbol>(_name?: TBrand): FunctionSchemaBuilder<TRequired, TNullable, TResult & {
|
|
81
150
|
readonly [K in BRAND]: TBrand;
|
|
82
|
-
}, THasDefault, TExtensions> & TExtensions;
|
|
151
|
+
}, THasDefault, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
83
152
|
/**
|
|
84
153
|
* Marks the inferred type as `Readonly<Function>`. Sets the
|
|
85
154
|
* `isReadonly` introspection flag for tooling consistency.
|
|
86
155
|
*
|
|
87
156
|
* @see {@link SchemaBuilder.readonly}
|
|
88
157
|
*/
|
|
89
|
-
readonly(): FunctionSchemaBuilder<TRequired, TNullable, Readonly<TResult>, THasDefault, TExtensions> & TExtensions;
|
|
158
|
+
readonly(): FunctionSchemaBuilder<TRequired, TNullable, Readonly<TResult>, THasDefault, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
90
159
|
/**
|
|
91
160
|
* @hidden
|
|
92
161
|
*/
|
|
93
|
-
nullable(): FunctionSchemaBuilder<TRequired, true, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
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;
|
|
94
214
|
/**
|
|
95
215
|
* @hidden
|
|
96
216
|
*/
|
|
97
|
-
notNullable(): FunctionSchemaBuilder<TRequired, false, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
217
|
+
notNullable(): FunctionSchemaBuilder<TRequired, false, TExplicitType, THasDefault, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
98
218
|
}
|
|
99
219
|
/**
|
|
100
|
-
* Creates a `function` schema.
|
|
101
|
-
*
|
|
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}
|
|
102
246
|
*/
|
|
103
247
|
export declare const func: () => FunctionSchemaBuilder<true>;
|
|
104
248
|
export {};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{a,b}from"../chunk-
|
|
1
|
+
import{a,b}from"../chunk-ZC6YBKCP.js";import"../chunk-3JMDGYDT.js";export{a as FunctionSchemaBuilder,b as func};
|
|
2
2
|
//# sourceMappingURL=FunctionSchemaBuilder.js.map
|
|
@@ -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 {};
|
|
@@ -68,8 +68,10 @@ export declare class LazySchemaBuilder<TResult = any, TRequired extends boolean
|
|
|
68
68
|
hasDefault: boolean;
|
|
69
69
|
defaultValue: TResult | (() => TResult) | undefined;
|
|
70
70
|
description: string | undefined;
|
|
71
|
+
schemaName: string | undefined;
|
|
71
72
|
hasCatch: boolean;
|
|
72
73
|
catchValue: TResult | (() => TResult) | undefined;
|
|
74
|
+
example: unknown;
|
|
73
75
|
};
|
|
74
76
|
/** {@inheritDoc SchemaBuilder.validate} */
|
|
75
77
|
validate(object: TResult, context?: ValidationContext): ValidationResult<TResult>;
|
|
@@ -117,8 +117,10 @@ export declare class NumberSchemaBuilder<TResult = number, TRequired extends boo
|
|
|
117
117
|
hasDefault: boolean;
|
|
118
118
|
defaultValue: TResult | (() => TResult) | undefined;
|
|
119
119
|
description: string | undefined;
|
|
120
|
+
schemaName: string | undefined;
|
|
120
121
|
hasCatch: boolean;
|
|
121
122
|
catchValue: TResult | (() => TResult) | undefined;
|
|
123
|
+
example: unknown;
|
|
122
124
|
};
|
|
123
125
|
/**
|
|
124
126
|
* @inheritdoc
|
|
@@ -228,6 +230,20 @@ export declare class NumberSchemaBuilder<TResult = number, TRequired extends boo
|
|
|
228
230
|
* Consider `Infinity` as valid.
|
|
229
231
|
*/
|
|
230
232
|
canBeInfinite(): NumberSchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
233
|
+
/**
|
|
234
|
+
* Adds a preprocessor that coerces a string value to a number
|
|
235
|
+
* using `Number(value)`. Useful when the input is captured from
|
|
236
|
+
* a string source (e.g. a parse-string schema, URL parameter,
|
|
237
|
+
* or form input).
|
|
238
|
+
*
|
|
239
|
+
* @example ```ts
|
|
240
|
+
* const schema = number().coerce();
|
|
241
|
+
* const result = schema.validate('42');
|
|
242
|
+
* // result.valid === true
|
|
243
|
+
* // result.object === 42
|
|
244
|
+
* ```
|
|
245
|
+
*/
|
|
246
|
+
coerce(): NumberSchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
231
247
|
/**
|
|
232
248
|
* Restrict number to be at least `minValue`.
|
|
233
249
|
*/
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{a,b}from"../chunk-
|
|
1
|
+
import{a,b}from"../chunk-BUEVZ3KA.js";import"../chunk-3JMDGYDT.js";export{a as NumberSchemaBuilder,b as number};
|
|
2
2
|
//# sourceMappingURL=NumberSchemaBuilder.js.map
|