@cleverbrush/schema 0.0.0-beta-20260410073748
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 +1409 -0
- package/dist/builders/AnySchemaBuilder.d.ts +116 -0
- package/dist/builders/AnySchemaBuilder.js +2 -0
- package/dist/builders/AnySchemaBuilder.js.map +1 -0
- package/dist/builders/ArraySchemaBuilder.d.ts +214 -0
- package/dist/builders/ArraySchemaBuilder.js +2 -0
- package/dist/builders/ArraySchemaBuilder.js.map +1 -0
- package/dist/builders/BooleanSchemaBuilder.d.ts +146 -0
- package/dist/builders/BooleanSchemaBuilder.js +2 -0
- package/dist/builders/BooleanSchemaBuilder.js.map +1 -0
- package/dist/builders/DateSchemaBuilder.d.ts +277 -0
- package/dist/builders/DateSchemaBuilder.js +2 -0
- package/dist/builders/DateSchemaBuilder.js.map +1 -0
- package/dist/builders/ExternSchemaBuilder.d.ts +200 -0
- package/dist/builders/ExternSchemaBuilder.js +2 -0
- package/dist/builders/ExternSchemaBuilder.js.map +1 -0
- package/dist/builders/FunctionSchemaBuilder.d.ts +246 -0
- package/dist/builders/FunctionSchemaBuilder.js +2 -0
- package/dist/builders/FunctionSchemaBuilder.js.map +1 -0
- package/dist/builders/LazySchemaBuilder.d.ts +167 -0
- package/dist/builders/NullSchemaBuilder.d.ts +162 -0
- package/dist/builders/NumberSchemaBuilder.d.ts +273 -0
- package/dist/builders/NumberSchemaBuilder.js +2 -0
- package/dist/builders/NumberSchemaBuilder.js.map +1 -0
- package/dist/builders/ObjectSchemaBuilder.d.ts +517 -0
- package/dist/builders/ObjectSchemaBuilder.js +2 -0
- package/dist/builders/ObjectSchemaBuilder.js.map +1 -0
- package/dist/builders/PropertyValidationResult.d.ts +68 -0
- package/dist/builders/RecordSchemaBuilder.d.ts +341 -0
- package/dist/builders/RecordSchemaBuilder.js +2 -0
- package/dist/builders/RecordSchemaBuilder.js.map +1 -0
- package/dist/builders/SchemaBuilder.d.ts +947 -0
- package/dist/builders/StringSchemaBuilder.d.ts +291 -0
- package/dist/builders/StringSchemaBuilder.js +2 -0
- package/dist/builders/StringSchemaBuilder.js.map +1 -0
- package/dist/builders/TupleSchemaBuilder.d.ts +248 -0
- package/dist/builders/TupleSchemaBuilder.js +2 -0
- package/dist/builders/TupleSchemaBuilder.js.map +1 -0
- package/dist/builders/UnionSchemaBuilder.d.ts +227 -0
- package/dist/builders/UnionSchemaBuilder.js +2 -0
- package/dist/builders/UnionSchemaBuilder.js.map +1 -0
- package/dist/chunk-CTP4RHDG.js +2 -0
- package/dist/chunk-CTP4RHDG.js.map +1 -0
- package/dist/chunk-FWUEUW2N.js +2 -0
- package/dist/chunk-FWUEUW2N.js.map +1 -0
- package/dist/chunk-IJ6FO37G.js +2 -0
- package/dist/chunk-IJ6FO37G.js.map +1 -0
- package/dist/chunk-KESLT5EE.js +2 -0
- package/dist/chunk-KESLT5EE.js.map +1 -0
- package/dist/chunk-KFTO7TMB.js +2 -0
- package/dist/chunk-KFTO7TMB.js.map +1 -0
- package/dist/chunk-MKAYFPAR.js +2 -0
- package/dist/chunk-MKAYFPAR.js.map +1 -0
- package/dist/chunk-NXPH3ZUW.js +2 -0
- package/dist/chunk-NXPH3ZUW.js.map +1 -0
- package/dist/chunk-OOPF7RXS.js +2 -0
- package/dist/chunk-OOPF7RXS.js.map +1 -0
- package/dist/chunk-S5TC6NSU.js +2 -0
- package/dist/chunk-S5TC6NSU.js.map +1 -0
- package/dist/chunk-YWBNVHR6.js +2 -0
- package/dist/chunk-YWBNVHR6.js.map +1 -0
- package/dist/chunk-Z72HXYKX.js +2 -0
- package/dist/chunk-Z72HXYKX.js.map +1 -0
- package/dist/chunk-Z7QZ7IXT.js +2 -0
- package/dist/chunk-Z7QZ7IXT.js.map +1 -0
- package/dist/chunk-ZAN4ZHCJ.js +2 -0
- package/dist/chunk-ZAN4ZHCJ.js.map +1 -0
- package/dist/chunk-ZUPYV5TI.js +2 -0
- package/dist/chunk-ZUPYV5TI.js.map +1 -0
- package/dist/core.d.ts +23 -0
- package/dist/core.js +2 -0
- package/dist/core.js.map +1 -0
- package/dist/extension.d.ts +413 -0
- package/dist/extensions/array.d.ts +112 -0
- package/dist/extensions/enum.d.ts +190 -0
- package/dist/extensions/index.d.ts +108 -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 +7 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/utils/transaction.d.ts +69 -0
- package/package.json +104 -0
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { type BRAND, type InferType, SchemaBuilder, type ValidationContext, type ValidationErrorMessageProvider, type ValidationResult } from './SchemaBuilder.js';
|
|
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
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Schema builder for functions. Allows to define a schema for a function.
|
|
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.
|
|
10
|
+
*
|
|
11
|
+
* **NOTE** this class is exported only to give opportunity to extend it
|
|
12
|
+
* by inheriting. It is not recommended to create an instance of this class
|
|
13
|
+
* directly. Use {@link func | func()} function instead.
|
|
14
|
+
*
|
|
15
|
+
* @example Basic validation
|
|
16
|
+
* ```ts
|
|
17
|
+
* const schema = func();
|
|
18
|
+
* const result = schema.validate(() => {});
|
|
19
|
+
* // result.valid === true
|
|
20
|
+
* // result.object === () => {}
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @example Optional function schema
|
|
24
|
+
* ```ts
|
|
25
|
+
* const schema = func().optional();
|
|
26
|
+
* const result = schema.validate(undefined);
|
|
27
|
+
* // result.valid === true
|
|
28
|
+
* // result.object === undefined
|
|
29
|
+
* ```
|
|
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
|
+
*
|
|
49
|
+
* @see {@link func}
|
|
50
|
+
*/
|
|
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>);
|
|
58
|
+
/**
|
|
59
|
+
* @hidden
|
|
60
|
+
*/
|
|
61
|
+
hasType<T>(_notUsed?: T): FunctionSchemaBuilder<true, TNullable, T, THasDefault, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
62
|
+
/**
|
|
63
|
+
* @hidden
|
|
64
|
+
*/
|
|
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
|
+
hasCatch: boolean;
|
|
109
|
+
catchValue: TResult | (() => TResult) | undefined;
|
|
110
|
+
};
|
|
111
|
+
/** {@inheritDoc SchemaBuilder.validate} */
|
|
112
|
+
validate(object: TResult, context?: ValidationContext): ValidationResult<TResult>;
|
|
113
|
+
/** {@inheritDoc SchemaBuilder.validateAsync} */
|
|
114
|
+
validateAsync(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
|
|
115
|
+
/**
|
|
116
|
+
* Performs synchronous validation of the schema over `object`.
|
|
117
|
+
* Throws if any preprocessor, validator, or error message provider returns a Promise.
|
|
118
|
+
* @param context Optional `ValidationContext` settings.
|
|
119
|
+
*/
|
|
120
|
+
protected _validate(object: TResult, context?: ValidationContext): ValidationResult<TResult>;
|
|
121
|
+
/**
|
|
122
|
+
* Performs async validation of the schema over `object`.
|
|
123
|
+
* Supports async preprocessors, validators, and error message providers.
|
|
124
|
+
* @param context Optional `ValidationContext` settings.
|
|
125
|
+
*/
|
|
126
|
+
protected _validateAsync(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
|
|
127
|
+
protected createFromProps<TReq extends boolean>(props: FunctionSchemaBuilderCreateProps<TReq>): this;
|
|
128
|
+
/**
|
|
129
|
+
* @hidden
|
|
130
|
+
*/
|
|
131
|
+
required(errorMessage?: ValidationErrorMessageProvider): FunctionSchemaBuilder<true, TNullable, TExplicitType, THasDefault, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
132
|
+
/**
|
|
133
|
+
* @hidden
|
|
134
|
+
*/
|
|
135
|
+
optional(): FunctionSchemaBuilder<false, TNullable, TExplicitType, THasDefault, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
136
|
+
/**
|
|
137
|
+
* @hidden
|
|
138
|
+
*/
|
|
139
|
+
default(value: TResult | (() => TResult)): FunctionSchemaBuilder<true, TNullable, TExplicitType, true, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
140
|
+
/**
|
|
141
|
+
* @hidden
|
|
142
|
+
*/
|
|
143
|
+
clearDefault(): FunctionSchemaBuilder<TRequired, TNullable, TExplicitType, false, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
144
|
+
/**
|
|
145
|
+
* @hidden
|
|
146
|
+
*/
|
|
147
|
+
brand<TBrand extends string | symbol>(_name?: TBrand): FunctionSchemaBuilder<TRequired, TNullable, TResult & {
|
|
148
|
+
readonly [K in BRAND]: TBrand;
|
|
149
|
+
}, THasDefault, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
150
|
+
/**
|
|
151
|
+
* Marks the inferred type as `Readonly<Function>`. Sets the
|
|
152
|
+
* `isReadonly` introspection flag for tooling consistency.
|
|
153
|
+
*
|
|
154
|
+
* @see {@link SchemaBuilder.readonly}
|
|
155
|
+
*/
|
|
156
|
+
readonly(): FunctionSchemaBuilder<TRequired, TNullable, Readonly<TResult>, THasDefault, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
157
|
+
/**
|
|
158
|
+
* @hidden
|
|
159
|
+
*/
|
|
160
|
+
nullable(): FunctionSchemaBuilder<TRequired, true, TExplicitType, THasDefault, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
161
|
+
/**
|
|
162
|
+
* Appends a positional parameter schema to the function schema.
|
|
163
|
+
*
|
|
164
|
+
* Each call extends the inferred function signature by one parameter.
|
|
165
|
+
* The full list of parameter schemas is available at runtime via
|
|
166
|
+
* `introspect().parameters`.
|
|
167
|
+
*
|
|
168
|
+
* Parameter order matches the call order — the first `addParameter()` call
|
|
169
|
+
* defines the type of the first argument, the second call defines the second
|
|
170
|
+
* argument, and so on.
|
|
171
|
+
*
|
|
172
|
+
* @param schema - The schema describing the parameter. Pass an optional schema
|
|
173
|
+
* (e.g. `string().optional()`) to make the corresponding argument optional.
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```ts
|
|
177
|
+
* const fn = func()
|
|
178
|
+
* .addParameter(string()) // (a: string, ...) => any
|
|
179
|
+
* .addParameter(number().optional()) // (..., b?: number) => any
|
|
180
|
+
* .addParameter(boolean()); // (..., c: boolean) => any
|
|
181
|
+
*
|
|
182
|
+
* type Fn = InferType<typeof fn>;
|
|
183
|
+
* // → (a: string, b: number | undefined, c: boolean) => any
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
addParameter<TSchema extends SchemaBuilder<any, any, any, any, any>>(schema: TSchema): FunctionSchemaBuilder<TRequired, TNullable, TExplicitType, THasDefault, TExtensions, [
|
|
187
|
+
...TParameters,
|
|
188
|
+
TSchema
|
|
189
|
+
], TReturnTypeSchema> & TExtensions;
|
|
190
|
+
/**
|
|
191
|
+
* Sets the return type schema for the function schema.
|
|
192
|
+
*
|
|
193
|
+
* Replaces any previously set return type. The inferred function signature
|
|
194
|
+
* gains a concrete return type instead of `any`. The schema is accessible at
|
|
195
|
+
* runtime via `introspect().returnType`.
|
|
196
|
+
*
|
|
197
|
+
* @param schema - The schema describing the return type of the function.
|
|
198
|
+
*
|
|
199
|
+
* @example
|
|
200
|
+
* ```ts
|
|
201
|
+
* const fn = func()
|
|
202
|
+
* .addParameter(string())
|
|
203
|
+
* .hasReturnType(number());
|
|
204
|
+
*
|
|
205
|
+
* type Fn = InferType<typeof fn>;
|
|
206
|
+
* // → (param0: string) => number
|
|
207
|
+
*
|
|
208
|
+
* fn.introspect().returnType; // NumberSchemaBuilder
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
hasReturnType<TSchema extends SchemaBuilder<any, any, any, any, any>>(schema: TSchema): FunctionSchemaBuilder<TRequired, TNullable, TExplicitType, THasDefault, TExtensions, TParameters, TSchema> & TExtensions;
|
|
212
|
+
/**
|
|
213
|
+
* @hidden
|
|
214
|
+
*/
|
|
215
|
+
notNullable(): FunctionSchemaBuilder<TRequired, false, TExplicitType, THasDefault, TExtensions, TParameters, TReturnTypeSchema> & TExtensions;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Creates a `function` schema that validates the value is a JavaScript function.
|
|
219
|
+
*
|
|
220
|
+
* The returned builder is immutable and fully chainable. Use
|
|
221
|
+
* {@link FunctionSchemaBuilder.addParameter} to annotate the expected parameter
|
|
222
|
+
* types and {@link FunctionSchemaBuilder.hasReturnType} to annotate the return
|
|
223
|
+
* type — the inferred TypeScript function signature is updated automatically.
|
|
224
|
+
*
|
|
225
|
+
* @returns A new {@link FunctionSchemaBuilder} with `isRequired` set to `true`.
|
|
226
|
+
*
|
|
227
|
+
* @example
|
|
228
|
+
* ```ts
|
|
229
|
+
* import { func, string, number, InferType } from '@cleverbrush/schema';
|
|
230
|
+
*
|
|
231
|
+
* const schema = func()
|
|
232
|
+
* .addParameter(string())
|
|
233
|
+
* .addParameter(number().optional())
|
|
234
|
+
* .hasReturnType(string());
|
|
235
|
+
*
|
|
236
|
+
* type Fn = InferType<typeof schema>;
|
|
237
|
+
* // → (param0: string, param1?: number) => string
|
|
238
|
+
*
|
|
239
|
+
* schema.validate(() => 'hello'); // { valid: true }
|
|
240
|
+
* schema.validate('not a fn'); // { valid: false }
|
|
241
|
+
* ```
|
|
242
|
+
*
|
|
243
|
+
* @see {@link FunctionSchemaBuilder}
|
|
244
|
+
*/
|
|
245
|
+
export declare const func: () => FunctionSchemaBuilder<true>;
|
|
246
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import { type BRAND, SchemaBuilder, type ValidationContext, type ValidationErrorMessageProvider, type ValidationResult } from './SchemaBuilder.js';
|
|
2
|
+
type LazySchemaBuilderCreateProps<R extends boolean = true> = Partial<ReturnType<LazySchemaBuilder<any, R>['introspect']>>;
|
|
3
|
+
/**
|
|
4
|
+
* Lazy schema builder class. Allows defining recursive/self-referential schemas
|
|
5
|
+
* by wrapping a getter function that returns the target schema. The getter is
|
|
6
|
+
* called once on first validation and the result is cached.
|
|
7
|
+
*
|
|
8
|
+
* This is the primary mechanism for building recursive data structures such as
|
|
9
|
+
* tree nodes, nested menus, and threaded comments.
|
|
10
|
+
*
|
|
11
|
+
* **NOTE** TypeScript cannot infer recursive types automatically, so you must
|
|
12
|
+
* provide an explicit type annotation on the variable holding the schema:
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* type TreeNode = { value: number; children: TreeNode[] };
|
|
17
|
+
*
|
|
18
|
+
* const treeNode: SchemaBuilder<TreeNode, true> = object({
|
|
19
|
+
* value: number(),
|
|
20
|
+
* children: array(lazy(() => treeNode))
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* treeNode.validate({ value: 1, children: [{ value: 2, children: [] }] });
|
|
24
|
+
* // { valid: true, object: { value: 1, children: [{ value: 2, children: [] }] } }
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* type Comment = { text: string; replies: Comment[] };
|
|
30
|
+
*
|
|
31
|
+
* const commentSchema: SchemaBuilder<Comment, true> = object({
|
|
32
|
+
* text: string(),
|
|
33
|
+
* replies: array(lazy(() => commentSchema))
|
|
34
|
+
* });
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare class LazySchemaBuilder<TResult = any, TRequired extends boolean = true, TNullable extends boolean = false, THasDefault extends boolean = false, TExtensions = {}> extends SchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> {
|
|
38
|
+
#private;
|
|
39
|
+
/**
|
|
40
|
+
* @hidden
|
|
41
|
+
*/
|
|
42
|
+
static create(props: LazySchemaBuilderCreateProps<any>): LazySchemaBuilder<any, true, false, false, {}>;
|
|
43
|
+
protected constructor(props: LazySchemaBuilderCreateProps<TRequired>);
|
|
44
|
+
/**
|
|
45
|
+
* Resolves the lazy schema by calling the getter (once; result is cached).
|
|
46
|
+
* After the first call subsequent calls return the cached schema instance.
|
|
47
|
+
*/
|
|
48
|
+
resolve(): SchemaBuilder<TResult, any, any>;
|
|
49
|
+
/**
|
|
50
|
+
* @inheritdoc
|
|
51
|
+
*/
|
|
52
|
+
introspect(): {
|
|
53
|
+
/**
|
|
54
|
+
* The getter function that returns the lazily-resolved schema.
|
|
55
|
+
* Call {@link LazySchemaBuilder.resolve} to obtain the schema instance.
|
|
56
|
+
*/
|
|
57
|
+
getter: () => SchemaBuilder<TResult, any, any>;
|
|
58
|
+
type: string;
|
|
59
|
+
isRequired: boolean;
|
|
60
|
+
isNullable: boolean;
|
|
61
|
+
isReadonly: boolean;
|
|
62
|
+
preprocessors: readonly import("./SchemaBuilder.js").PreprocessorEntry<TResult>[];
|
|
63
|
+
validators: readonly import("./SchemaBuilder.js").ValidatorEntry<TResult>[];
|
|
64
|
+
requiredValidationErrorMessageProvider: ValidationErrorMessageProvider<SchemaBuilder<any, any, any, any, any>>;
|
|
65
|
+
extensions: {
|
|
66
|
+
[x: string]: unknown;
|
|
67
|
+
};
|
|
68
|
+
hasDefault: boolean;
|
|
69
|
+
defaultValue: TResult | (() => TResult) | undefined;
|
|
70
|
+
description: string | undefined;
|
|
71
|
+
hasCatch: boolean;
|
|
72
|
+
catchValue: TResult | (() => TResult) | undefined;
|
|
73
|
+
};
|
|
74
|
+
/** {@inheritDoc SchemaBuilder.validate} */
|
|
75
|
+
validate(object: TResult, context?: ValidationContext): ValidationResult<TResult>;
|
|
76
|
+
/** {@inheritDoc SchemaBuilder.validateAsync} */
|
|
77
|
+
validateAsync(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
|
|
78
|
+
/**
|
|
79
|
+
* Performs synchronous validation of the schema over `object`.
|
|
80
|
+
* Throws if any preprocessor, validator, or error message provider returns a Promise.
|
|
81
|
+
* @param context Optional `ValidationContext` settings.
|
|
82
|
+
*/
|
|
83
|
+
protected _validate(object: TResult, context?: ValidationContext): ValidationResult<TResult>;
|
|
84
|
+
/**
|
|
85
|
+
* Performs async validation of the schema over `object`.
|
|
86
|
+
* Supports async preprocessors, validators, and error message providers.
|
|
87
|
+
* @param context Optional `ValidationContext` settings.
|
|
88
|
+
*/
|
|
89
|
+
protected _validateAsync(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
|
|
90
|
+
protected createFromProps<TReq extends boolean>(props: LazySchemaBuilderCreateProps<TReq>): this;
|
|
91
|
+
/**
|
|
92
|
+
* @inheritdoc
|
|
93
|
+
*/
|
|
94
|
+
hasType<T>(_notUsed?: T): LazySchemaBuilder<T, true, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
95
|
+
/**
|
|
96
|
+
* @inheritdoc
|
|
97
|
+
*/
|
|
98
|
+
clearHasType(): LazySchemaBuilder<TResult, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
99
|
+
/**
|
|
100
|
+
* @hidden
|
|
101
|
+
*/
|
|
102
|
+
required(errorMessage?: ValidationErrorMessageProvider): LazySchemaBuilder<TResult, true, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
103
|
+
/**
|
|
104
|
+
* @hidden
|
|
105
|
+
*/
|
|
106
|
+
optional(): LazySchemaBuilder<TResult, false, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
107
|
+
/**
|
|
108
|
+
* @hidden
|
|
109
|
+
*/
|
|
110
|
+
default(value: TResult | (() => TResult)): LazySchemaBuilder<TResult, true, TNullable, true, TExtensions> & TExtensions;
|
|
111
|
+
/**
|
|
112
|
+
* @hidden
|
|
113
|
+
*/
|
|
114
|
+
clearDefault(): LazySchemaBuilder<TResult, TRequired, TNullable, false, TExtensions> & TExtensions;
|
|
115
|
+
/**
|
|
116
|
+
* @hidden
|
|
117
|
+
*/
|
|
118
|
+
brand<TBrand extends string | symbol>(_name?: TBrand): LazySchemaBuilder<TResult & {
|
|
119
|
+
readonly [K in BRAND]: TBrand;
|
|
120
|
+
}, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
121
|
+
/**
|
|
122
|
+
* @hidden
|
|
123
|
+
*/
|
|
124
|
+
readonly(): LazySchemaBuilder<Readonly<TResult>, TRequired, TNullable, THasDefault, TExtensions> & TExtensions;
|
|
125
|
+
/**
|
|
126
|
+
* @hidden
|
|
127
|
+
*/
|
|
128
|
+
nullable(): LazySchemaBuilder<TResult, TRequired, true, THasDefault, TExtensions> & TExtensions;
|
|
129
|
+
/**
|
|
130
|
+
* @hidden
|
|
131
|
+
*/
|
|
132
|
+
notNullable(): LazySchemaBuilder<TResult, TRequired, false, THasDefault, TExtensions> & TExtensions;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Creates a lazy schema that defers the schema definition until first validation.
|
|
136
|
+
* Use this to define recursive/self-referential schemas.
|
|
137
|
+
*
|
|
138
|
+
* The getter function is called **once** on first use and the result is cached.
|
|
139
|
+
* You **must** provide an explicit TypeScript type annotation on the variable
|
|
140
|
+
* holding the outer schema — TypeScript cannot infer recursive types automatically.
|
|
141
|
+
*
|
|
142
|
+
* @param getter - A function that returns the schema to use for validation.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* // Tree structure
|
|
147
|
+
* type TreeNode = { value: number; children: TreeNode[] };
|
|
148
|
+
*
|
|
149
|
+
* const treeNode: SchemaBuilder<TreeNode, true> = object({
|
|
150
|
+
* value: number(),
|
|
151
|
+
* children: array(lazy(() => treeNode))
|
|
152
|
+
* });
|
|
153
|
+
* ```
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```ts
|
|
157
|
+
* // Optional recursive field (submenu)
|
|
158
|
+
* type MenuItem = { label: string; submenu?: MenuItem[] };
|
|
159
|
+
*
|
|
160
|
+
* const menuItem: SchemaBuilder<MenuItem, true> = object({
|
|
161
|
+
* label: string(),
|
|
162
|
+
* submenu: array(lazy(() => menuItem)).optional()
|
|
163
|
+
* });
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
export declare function lazy<TResult>(getter: () => SchemaBuilder<TResult, any, any>): LazySchemaBuilder<TResult, true, false, false, {}>;
|
|
167
|
+
export {};
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { type BRAND, SchemaBuilder, type ValidationContext, type ValidationErrorMessageProvider, type ValidationResult } from './SchemaBuilder.js';
|
|
2
|
+
type NullSchemaBuilderCreateProps<R extends boolean = true> = Partial<ReturnType<NullSchemaBuilder<R>['introspect']>>;
|
|
3
|
+
/**
|
|
4
|
+
* Schema builder for `null` values. Validates that the input is exactly `null`.
|
|
5
|
+
*
|
|
6
|
+
* When required (the default), only `null` is accepted. When optional (via
|
|
7
|
+
* `.optional()`), both `null` and `undefined` are accepted; any other value
|
|
8
|
+
* is rejected.
|
|
9
|
+
*
|
|
10
|
+
* This builder is useful when you need to represent an explicitly-null field
|
|
11
|
+
* in a typed schema, for example in discriminated-union branches or when
|
|
12
|
+
* modelling a JSON payload that may carry a JSON `null` value.
|
|
13
|
+
*
|
|
14
|
+
* **NOTE** this class is exported only to give opportunity to extend it
|
|
15
|
+
* by inheriting. It is not recommended to create an instance of this class
|
|
16
|
+
* directly. Use {@link nul | nul()} function instead.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* import { nul } from '@cleverbrush/schema';
|
|
21
|
+
*
|
|
22
|
+
* const schema = nul();
|
|
23
|
+
*
|
|
24
|
+
* schema.validate(null); // { valid: true, object: null }
|
|
25
|
+
* schema.validate(undefined); // { valid: false }
|
|
26
|
+
* schema.validate(0); // { valid: false }
|
|
27
|
+
* schema.validate(''); // { valid: false }
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* // Optional — accepts null or undefined
|
|
33
|
+
* const schema = nul().optional();
|
|
34
|
+
*
|
|
35
|
+
* schema.validate(null); // { valid: true, object: null }
|
|
36
|
+
* schema.validate(undefined); // { valid: true, object: undefined }
|
|
37
|
+
* schema.validate(false); // { valid: false }
|
|
38
|
+
* ```
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* // Use inside a union to model a nullable string field
|
|
43
|
+
* import { union, string, nul, InferType } from '@cleverbrush/schema';
|
|
44
|
+
*
|
|
45
|
+
* const NullableString = union(string()).or(nul());
|
|
46
|
+
* type NullableString = InferType<typeof NullableString>;
|
|
47
|
+
* // string | null
|
|
48
|
+
*
|
|
49
|
+
* NullableString.validate('hello'); // valid
|
|
50
|
+
* NullableString.validate(null); // valid
|
|
51
|
+
* NullableString.validate(42); // invalid
|
|
52
|
+
* ```
|
|
53
|
+
*
|
|
54
|
+
* @see {@link nul}
|
|
55
|
+
*/
|
|
56
|
+
export declare class NullSchemaBuilder<TRequired extends boolean = true, TNullable extends boolean = false, TExplicitType = undefined, THasDefault extends boolean = false, TExtensions = {}> extends SchemaBuilder<null, TRequired, TNullable, THasDefault, TExtensions> {
|
|
57
|
+
#private;
|
|
58
|
+
/**
|
|
59
|
+
* @hidden
|
|
60
|
+
*/
|
|
61
|
+
static create(props: NullSchemaBuilderCreateProps<any>): NullSchemaBuilder<true, false, undefined, false, {}>;
|
|
62
|
+
protected constructor(props: NullSchemaBuilderCreateProps<TRequired>);
|
|
63
|
+
/**
|
|
64
|
+
* @hidden
|
|
65
|
+
*/
|
|
66
|
+
hasType<T>(_notUsed?: T): NullSchemaBuilder<true, TNullable, T, THasDefault, TExtensions> & TExtensions;
|
|
67
|
+
/**
|
|
68
|
+
* @hidden
|
|
69
|
+
*/
|
|
70
|
+
clearHasType(): NullSchemaBuilder<TRequired, TNullable, undefined, THasDefault, TExtensions> & TExtensions;
|
|
71
|
+
/** {@inheritDoc SchemaBuilder.validate} */
|
|
72
|
+
validate(object: null, context?: ValidationContext): ValidationResult<null>;
|
|
73
|
+
/** {@inheritDoc SchemaBuilder.validateAsync} */
|
|
74
|
+
validateAsync(object: null, context?: ValidationContext): Promise<ValidationResult<null>>;
|
|
75
|
+
/**
|
|
76
|
+
* Performs synchronous validation of the schema over `object`.
|
|
77
|
+
* @param context Optional `ValidationContext` settings.
|
|
78
|
+
*/
|
|
79
|
+
protected _validate(object: null, _context?: ValidationContext): ValidationResult<null>;
|
|
80
|
+
/**
|
|
81
|
+
* Performs async validation of the schema over `object`.
|
|
82
|
+
* @param context Optional `ValidationContext` settings.
|
|
83
|
+
*/
|
|
84
|
+
protected _validateAsync(object: null, _context?: ValidationContext): Promise<ValidationResult<null>>;
|
|
85
|
+
protected createFromProps<TReq extends boolean>(props: NullSchemaBuilderCreateProps<TReq>): this;
|
|
86
|
+
/**
|
|
87
|
+
* @hidden
|
|
88
|
+
*/
|
|
89
|
+
required(errorMessage?: ValidationErrorMessageProvider): NullSchemaBuilder<true, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
90
|
+
/**
|
|
91
|
+
* @hidden
|
|
92
|
+
*/
|
|
93
|
+
optional(): NullSchemaBuilder<false, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
94
|
+
/**
|
|
95
|
+
* @hidden
|
|
96
|
+
*/
|
|
97
|
+
default(value: null | (() => null)): NullSchemaBuilder<true, TNullable, TExplicitType, true, TExtensions> & TExtensions;
|
|
98
|
+
/**
|
|
99
|
+
* @hidden
|
|
100
|
+
*/
|
|
101
|
+
clearDefault(): NullSchemaBuilder<TRequired, TNullable, TExplicitType, false, TExtensions> & TExtensions;
|
|
102
|
+
/**
|
|
103
|
+
* @hidden
|
|
104
|
+
*/
|
|
105
|
+
brand<TBrand extends string | symbol>(_name?: TBrand): NullSchemaBuilder<TRequired, TNullable, null & {
|
|
106
|
+
readonly [K in BRAND]: TBrand;
|
|
107
|
+
}, THasDefault, TExtensions> & TExtensions;
|
|
108
|
+
/**
|
|
109
|
+
* Marks the inferred type as `Readonly<null>`. Since `null` is already
|
|
110
|
+
* immutable this is an identity operation, but it sets the `isReadonly`
|
|
111
|
+
* introspection flag for tooling consistency.
|
|
112
|
+
*
|
|
113
|
+
* @see {@link SchemaBuilder.readonly}
|
|
114
|
+
*/
|
|
115
|
+
readonly(): NullSchemaBuilder<TRequired, TNullable, Readonly<null>, THasDefault, TExtensions> & TExtensions;
|
|
116
|
+
/**
|
|
117
|
+
* @hidden
|
|
118
|
+
*/
|
|
119
|
+
nullable(): NullSchemaBuilder<TRequired, true, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
120
|
+
/**
|
|
121
|
+
* @hidden
|
|
122
|
+
*/
|
|
123
|
+
notNullable(): NullSchemaBuilder<TRequired, false, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Creates a schema that validates the value is exactly `null`.
|
|
127
|
+
*
|
|
128
|
+
* By default the schema is **required** — only `null` is accepted.
|
|
129
|
+
* Call `.optional()` to also allow `undefined`.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts
|
|
133
|
+
* import { nul } from '@cleverbrush/schema';
|
|
134
|
+
*
|
|
135
|
+
* nul().validate(null); // { valid: true, object: null }
|
|
136
|
+
* nul().validate(undefined); // { valid: false }
|
|
137
|
+
* nul().validate(0); // { valid: false }
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* @example
|
|
141
|
+
* ```ts
|
|
142
|
+
* nul().optional().validate(null); // { valid: true, object: null }
|
|
143
|
+
* nul().optional().validate(undefined); // { valid: true, object: undefined }
|
|
144
|
+
* nul().optional().validate(false); // { valid: false }
|
|
145
|
+
* ```
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* // Nullable field in an object schema
|
|
150
|
+
* import { object, string, nul, union, InferType } from '@cleverbrush/schema';
|
|
151
|
+
*
|
|
152
|
+
* const Schema = object({
|
|
153
|
+
* name: string(),
|
|
154
|
+
* deleted: union(nul()).or(string()), // null | string
|
|
155
|
+
* });
|
|
156
|
+
*
|
|
157
|
+
* type T = InferType<typeof Schema>;
|
|
158
|
+
* // { name: string; deleted: null | string }
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
export declare const nul: () => NullSchemaBuilder<true>;
|
|
162
|
+
export {};
|