@cleverbrush/schema 1.1.11 → 2.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 +1314 -101
- 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 +124 -23
- package/dist/builders/ArraySchemaBuilder.js +2 -284
- package/dist/builders/ArraySchemaBuilder.js.map +1 -0
- package/dist/builders/BooleanSchemaBuilder.d.ts +94 -19
- package/dist/builders/BooleanSchemaBuilder.js +2 -150
- package/dist/builders/BooleanSchemaBuilder.js.map +1 -0
- package/dist/builders/DateSchemaBuilder.d.ts +134 -34
- package/dist/builders/DateSchemaBuilder.js +2 -433
- 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 +57 -14
- package/dist/builders/FunctionSchemaBuilder.js +2 -113
- 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 +143 -31
- package/dist/builders/NumberSchemaBuilder.js +2 -386
- package/dist/builders/NumberSchemaBuilder.js.map +1 -0
- package/dist/builders/ObjectSchemaBuilder.d.ts +266 -61
- package/dist/builders/ObjectSchemaBuilder.js +2 -589
- 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 +818 -30
- package/dist/builders/StringSchemaBuilder.d.ts +152 -37
- package/dist/builders/StringSchemaBuilder.js +2 -414
- 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 +133 -39
- package/dist/builders/UnionSchemaBuilder.js +2 -216
- package/dist/builders/UnionSchemaBuilder.js.map +1 -0
- package/dist/chunk-BFCMDDTX.js +2 -0
- package/dist/chunk-BFCMDDTX.js.map +1 -0
- package/dist/chunk-CTP4RHDG.js +2 -0
- package/dist/chunk-CTP4RHDG.js.map +1 -0
- package/dist/chunk-EUQ5AE3H.js +2 -0
- package/dist/chunk-EUQ5AE3H.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-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-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 -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 +74 -6
- package/dist/builders/SchemaBuilder.js +0 -275
- package/dist/utils/transaction.js +0 -178
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type BRAND, SchemaBuilder, type ValidationContext, type ValidationErrorMessageProvider, type ValidationResult } from './SchemaBuilder.js';
|
|
2
2
|
type FunctionSchemaBuilderCreateProps<R extends boolean = true> = Partial<ReturnType<FunctionSchemaBuilder<R>['introspect']>>;
|
|
3
3
|
/**
|
|
4
4
|
* Schema builder for functions. Allows to define a schema for a function.
|
|
@@ -11,7 +11,7 @@ type FunctionSchemaBuilderCreateProps<R extends boolean = true> = Partial<Return
|
|
|
11
11
|
* @example
|
|
12
12
|
* ```ts
|
|
13
13
|
* const schema = func();
|
|
14
|
-
* const result =
|
|
14
|
+
* const result = schema.validate(() => {});
|
|
15
15
|
* // result.valid === true
|
|
16
16
|
* // result.object === () => {}
|
|
17
17
|
* ```
|
|
@@ -19,43 +19,86 @@ type FunctionSchemaBuilderCreateProps<R extends boolean = true> = Partial<Return
|
|
|
19
19
|
* @example
|
|
20
20
|
* ```ts
|
|
21
21
|
* const schema = func().optional();
|
|
22
|
-
* const result =
|
|
22
|
+
* const result = schema.validate(undefined);
|
|
23
23
|
* // result.valid === true
|
|
24
24
|
* // result.object === undefined
|
|
25
25
|
* ```
|
|
26
26
|
*
|
|
27
27
|
* @see {@link func}
|
|
28
28
|
*/
|
|
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
|
-
private constructor();
|
|
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> {
|
|
30
|
+
#private;
|
|
32
31
|
/**
|
|
33
32
|
* @hidden
|
|
34
33
|
*/
|
|
35
|
-
|
|
34
|
+
static create(props: FunctionSchemaBuilderCreateProps<any>): FunctionSchemaBuilder<true, false, undefined, false, {}, (...args: any[]) => any>;
|
|
35
|
+
protected constructor(props: FunctionSchemaBuilderCreateProps<TRequired>);
|
|
36
36
|
/**
|
|
37
37
|
* @hidden
|
|
38
38
|
*/
|
|
39
|
-
|
|
39
|
+
hasType<T>(_notUsed?: T): FunctionSchemaBuilder<true, TNullable, T, THasDefault, TExtensions> & TExtensions;
|
|
40
40
|
/**
|
|
41
|
-
*
|
|
42
|
-
|
|
41
|
+
* @hidden
|
|
42
|
+
*/
|
|
43
|
+
clearHasType(): FunctionSchemaBuilder<TRequired, TNullable, undefined, THasDefault, TExtensions> & TExtensions;
|
|
44
|
+
/** {@inheritDoc SchemaBuilder.validate} */
|
|
45
|
+
validate(object: TResult, context?: ValidationContext): ValidationResult<TResult>;
|
|
46
|
+
/** {@inheritDoc SchemaBuilder.validateAsync} */
|
|
47
|
+
validateAsync(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
|
|
48
|
+
/**
|
|
49
|
+
* Performs synchronous validation of the schema over `object`.
|
|
50
|
+
* Throws if any preprocessor, validator, or error message provider returns a Promise.
|
|
51
|
+
* @param context Optional `ValidationContext` settings.
|
|
52
|
+
*/
|
|
53
|
+
protected _validate(object: TResult, context?: ValidationContext): ValidationResult<TResult>;
|
|
54
|
+
/**
|
|
55
|
+
* Performs async validation of the schema over `object`.
|
|
56
|
+
* Supports async preprocessors, validators, and error message providers.
|
|
43
57
|
* @param context Optional `ValidationContext` settings.
|
|
44
58
|
*/
|
|
45
|
-
|
|
59
|
+
protected _validateAsync(object: TResult, context?: ValidationContext): Promise<ValidationResult<TResult>>;
|
|
46
60
|
protected createFromProps<TReq extends boolean>(props: FunctionSchemaBuilderCreateProps<TReq>): this;
|
|
47
61
|
/**
|
|
48
62
|
* @hidden
|
|
49
63
|
*/
|
|
50
|
-
required(): FunctionSchemaBuilder<true, TExplicitType
|
|
64
|
+
required(errorMessage?: ValidationErrorMessageProvider): FunctionSchemaBuilder<true, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
65
|
+
/**
|
|
66
|
+
* @hidden
|
|
67
|
+
*/
|
|
68
|
+
optional(): FunctionSchemaBuilder<false, TNullable, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
69
|
+
/**
|
|
70
|
+
* @hidden
|
|
71
|
+
*/
|
|
72
|
+
default(value: TResult | (() => TResult)): FunctionSchemaBuilder<true, TNullable, TExplicitType, true, TExtensions> & TExtensions;
|
|
73
|
+
/**
|
|
74
|
+
* @hidden
|
|
75
|
+
*/
|
|
76
|
+
clearDefault(): FunctionSchemaBuilder<TRequired, TNullable, TExplicitType, false, TExtensions> & TExtensions;
|
|
77
|
+
/**
|
|
78
|
+
* @hidden
|
|
79
|
+
*/
|
|
80
|
+
brand<TBrand extends string | symbol>(_name?: TBrand): FunctionSchemaBuilder<TRequired, TNullable, TResult & {
|
|
81
|
+
readonly [K in BRAND]: TBrand;
|
|
82
|
+
}, THasDefault, TExtensions> & TExtensions;
|
|
83
|
+
/**
|
|
84
|
+
* Marks the inferred type as `Readonly<Function>`. Sets the
|
|
85
|
+
* `isReadonly` introspection flag for tooling consistency.
|
|
86
|
+
*
|
|
87
|
+
* @see {@link SchemaBuilder.readonly}
|
|
88
|
+
*/
|
|
89
|
+
readonly(): FunctionSchemaBuilder<TRequired, TNullable, Readonly<TResult>, THasDefault, TExtensions> & TExtensions;
|
|
90
|
+
/**
|
|
91
|
+
* @hidden
|
|
92
|
+
*/
|
|
93
|
+
nullable(): FunctionSchemaBuilder<TRequired, true, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
51
94
|
/**
|
|
52
95
|
* @hidden
|
|
53
96
|
*/
|
|
54
|
-
|
|
97
|
+
notNullable(): FunctionSchemaBuilder<TRequired, false, TExplicitType, THasDefault, TExtensions> & TExtensions;
|
|
55
98
|
}
|
|
56
99
|
/**
|
|
57
100
|
* Creates a `function` schema.
|
|
58
|
-
* @
|
|
101
|
+
* @returns {@link FunctionSchemaBuilder}
|
|
59
102
|
*/
|
|
60
103
|
export declare const func: () => FunctionSchemaBuilder<true>;
|
|
61
104
|
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-EUQ5AE3H.js";import"../chunk-CTP4RHDG.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,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 {};
|