@cleverbrush/schema 1.1.10 → 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 +55 -15
- 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 +95 -20
- package/dist/builders/BooleanSchemaBuilder.js +2 -150
- package/dist/builders/BooleanSchemaBuilder.js.map +1 -0
- package/dist/builders/DateSchemaBuilder.d.ts +135 -37
- 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 +58 -15
- 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
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Built-in enum extension for `@cleverbrush/schema`.
|
|
3
|
+
*
|
|
4
|
+
* Provides the `.oneOf()` method on `StringSchemaBuilder` and
|
|
5
|
+
* `NumberSchemaBuilder`, constraining the value to a fixed set of
|
|
6
|
+
* allowed literals and narrowing the inferred type accordingly.
|
|
7
|
+
*
|
|
8
|
+
* Also exports the top-level {@link enumOf} convenience factory, which is
|
|
9
|
+
* sugar for `string().oneOf(...)`.
|
|
10
|
+
*
|
|
11
|
+
* These are pre-applied in the default `@cleverbrush/schema` import.
|
|
12
|
+
* Import from `@cleverbrush/schema/core` to get bare builders without
|
|
13
|
+
* these extensions.
|
|
14
|
+
*
|
|
15
|
+
* @module
|
|
16
|
+
*/
|
|
17
|
+
import type { NumberSchemaBuilder } from '../builders/NumberSchemaBuilder.js';
|
|
18
|
+
import type { ValidationErrorMessageProvider } from '../builders/SchemaBuilder.js';
|
|
19
|
+
import type { StringSchemaBuilder } from '../builders/StringSchemaBuilder.js';
|
|
20
|
+
import type { HiddenExtensionMethods } from '../extension.js';
|
|
21
|
+
/** Return type for `.oneOf()` on `StringSchemaBuilder`. */
|
|
22
|
+
type StringOneOfReturn<T extends string = string> = StringSchemaBuilder<T, true, false, false, StringOneOfExtension> & StringOneOfExtension & HiddenExtensionMethods;
|
|
23
|
+
/** Return type for `.oneOf()` on `NumberSchemaBuilder`. */
|
|
24
|
+
type NumberOneOfReturn<T extends number = number> = NumberSchemaBuilder<T, true, false, false, NumberOneOfExtension> & NumberOneOfExtension & HiddenExtensionMethods;
|
|
25
|
+
/**
|
|
26
|
+
* `.oneOf()` method added to `StringSchemaBuilder` by the built-in enum
|
|
27
|
+
* extension pack.
|
|
28
|
+
*
|
|
29
|
+
* **WORKAROUND:** This interface duplicates the method signature from
|
|
30
|
+
* `enumExtension` so that JSDoc survives into the published `.d.ts`
|
|
31
|
+
* files. TypeScript strips JSDoc when method signatures are reconstructed
|
|
32
|
+
* through the `FixedMethods` mapped type (conditional `infer` loses
|
|
33
|
+
* comments). Remove this interface once TypeScript preserves JSDoc
|
|
34
|
+
* through mapped types / conditional type inference.
|
|
35
|
+
*
|
|
36
|
+
* @see https://github.com/microsoft/TypeScript/issues/50715
|
|
37
|
+
*/
|
|
38
|
+
export interface StringOneOfExtension {
|
|
39
|
+
/**
|
|
40
|
+
* Constrains the string to one of the specified literal values.
|
|
41
|
+
*
|
|
42
|
+
* Narrows the inferred type from `string` to the union of the
|
|
43
|
+
* provided literals.
|
|
44
|
+
*
|
|
45
|
+
* @param values - the allowed string literals
|
|
46
|
+
* @returns a new schema builder restricted to the given values
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* import { string, InferType } from '@cleverbrush/schema';
|
|
51
|
+
*
|
|
52
|
+
* const role = string().oneOf('admin', 'user', 'guest');
|
|
53
|
+
* type Role = InferType<typeof role>; // 'admin' | 'user' | 'guest'
|
|
54
|
+
*
|
|
55
|
+
* role.validate('admin'); // valid
|
|
56
|
+
* role.validate('other'); // invalid — "must be one of: admin, user, guest"
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
oneOf<V extends string>(...values: [V, ...V[]]): StringOneOfReturn<V>;
|
|
60
|
+
/**
|
|
61
|
+
* Constrains the string to one of the specified literal values,
|
|
62
|
+
* with a custom error message or factory as the last argument.
|
|
63
|
+
*
|
|
64
|
+
* A trailing `string` or function argument is unambiguously the error
|
|
65
|
+
* message when using rest-param form.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* const role = string().oneOf('admin', 'user', (val) => `"${val}" is not allowed`);
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
oneOf<V extends string>(...args: [
|
|
73
|
+
...[V, ...V[]],
|
|
74
|
+
ValidationErrorMessageProvider<StringSchemaBuilder>
|
|
75
|
+
]): StringOneOfReturn<V>;
|
|
76
|
+
/**
|
|
77
|
+
* Constrains the string to one of the specified literal values,
|
|
78
|
+
* with an optional custom error message or factory.
|
|
79
|
+
*
|
|
80
|
+
* Pass the allowed values as an array (first argument) to provide a
|
|
81
|
+
* custom error message as the second argument.
|
|
82
|
+
*
|
|
83
|
+
* @param values - the allowed string literals as an array
|
|
84
|
+
* @param errorMessage - optional custom error message or factory function
|
|
85
|
+
* @returns a new schema builder restricted to the given values
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* import { string, InferType } from '@cleverbrush/schema';
|
|
90
|
+
*
|
|
91
|
+
* const role = string().oneOf(['admin', 'user', 'guest'], 'Invalid role');
|
|
92
|
+
* role.validate('other'); // invalid — "Invalid role"
|
|
93
|
+
*
|
|
94
|
+
* const role2 = string().oneOf(['admin', 'user'], (val) => `"${val}" is not a valid role`);
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
oneOf<V extends string>(values: readonly [V, ...V[]], errorMessage?: ValidationErrorMessageProvider<StringSchemaBuilder>): StringOneOfReturn<V>;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* `.oneOf()` method added to `NumberSchemaBuilder` by the built-in enum
|
|
101
|
+
* extension pack.
|
|
102
|
+
*
|
|
103
|
+
* @see StringOneOfExtension for full JSDoc rationale.
|
|
104
|
+
*/
|
|
105
|
+
export interface NumberOneOfExtension {
|
|
106
|
+
/**
|
|
107
|
+
* Constrains the number to one of the specified literal values.
|
|
108
|
+
*
|
|
109
|
+
* Narrows the inferred type from `number` to the union of the
|
|
110
|
+
* provided literals.
|
|
111
|
+
*
|
|
112
|
+
* @param values - the allowed number literals
|
|
113
|
+
* @returns a new schema builder restricted to the given values
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* import { number, InferType } from '@cleverbrush/schema';
|
|
118
|
+
*
|
|
119
|
+
* const priority = number().oneOf(1, 2, 3);
|
|
120
|
+
* type Priority = InferType<typeof priority>; // 1 | 2 | 3
|
|
121
|
+
*
|
|
122
|
+
* priority.validate(1); // valid
|
|
123
|
+
* priority.validate(4); // invalid — "must be one of: 1, 2, 3"
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
oneOf<V extends number>(...values: [V, ...V[]]): NumberOneOfReturn<V>;
|
|
127
|
+
/**
|
|
128
|
+
* Constrains the number to one of the specified literal values,
|
|
129
|
+
* with a custom error message or factory as the last argument.
|
|
130
|
+
*
|
|
131
|
+
* Because number values are always numbers, a trailing `string` or
|
|
132
|
+
* function argument is unambiguously the error message.
|
|
133
|
+
*
|
|
134
|
+
* @param args - the allowed number literals followed by an error message
|
|
135
|
+
* @returns a new schema builder restricted to the given values
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```ts
|
|
139
|
+
* import { number } from '@cleverbrush/schema';
|
|
140
|
+
*
|
|
141
|
+
* const priority = number().oneOf(1, 2, 3, 'Priority must be 1, 2, or 3');
|
|
142
|
+
* const priority2 = number().oneOf(1, 2, 3, (val) => `${val} is not a valid priority`);
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
oneOf<V extends number>(...args: [
|
|
146
|
+
...[V, ...V[]],
|
|
147
|
+
ValidationErrorMessageProvider<NumberSchemaBuilder>
|
|
148
|
+
]): NumberOneOfReturn<V>;
|
|
149
|
+
/**
|
|
150
|
+
* Constrains the number to one of the specified literal values,
|
|
151
|
+
* with an optional custom error message or factory.
|
|
152
|
+
*
|
|
153
|
+
* Pass the allowed values as an array (first argument) to provide a
|
|
154
|
+
* custom error message as the second argument.
|
|
155
|
+
*
|
|
156
|
+
* @param values - the allowed number literals as an array
|
|
157
|
+
* @param errorMessage - optional custom error message or factory function
|
|
158
|
+
* @returns a new schema builder restricted to the given values
|
|
159
|
+
*
|
|
160
|
+
* @example
|
|
161
|
+
* ```ts
|
|
162
|
+
* import { number } from '@cleverbrush/schema';
|
|
163
|
+
*
|
|
164
|
+
* const priority = number().oneOf([1, 2, 3], 'Must be 1, 2, or 3');
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
oneOf<V extends number>(values: readonly [V, ...V[]], errorMessage?: ValidationErrorMessageProvider<NumberSchemaBuilder>): NumberOneOfReturn<V>;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Extension descriptor that adds `.oneOf()` to `StringSchemaBuilder`
|
|
171
|
+
* and `NumberSchemaBuilder`.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* import { withExtensions } from '@cleverbrush/schema/core';
|
|
176
|
+
* import { enumExtension } from '@cleverbrush/schema';
|
|
177
|
+
*
|
|
178
|
+
* const s = withExtensions(enumExtension);
|
|
179
|
+
* const role = s.string().oneOf('admin', 'user', 'guest');
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
export declare const enumExtension: import("../extension.js").ExtensionDescriptor<{
|
|
183
|
+
string: {
|
|
184
|
+
oneOf(this: StringSchemaBuilder, ...args: any[]): StringSchemaBuilder<string, true, false, false, {}>;
|
|
185
|
+
};
|
|
186
|
+
number: {
|
|
187
|
+
oneOf(this: NumberSchemaBuilder, ...args: any[]): NumberSchemaBuilder<number, true, false, false, {}>;
|
|
188
|
+
};
|
|
189
|
+
}>;
|
|
190
|
+
export {};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pre‑wired extension pack for `@cleverbrush/schema`.
|
|
3
|
+
*
|
|
4
|
+
* Combines {@link stringExtensions}, {@link numberExtensions},
|
|
5
|
+
* and {@link arrayExtensions} via
|
|
6
|
+
* `withExtensions()` and re‑exports the augmented factory functions.
|
|
7
|
+
*
|
|
8
|
+
* The default `@cleverbrush/schema` entry point re‑exports these
|
|
9
|
+
* augmented factories so that `email()`, `positive()`, `nonempty()`,
|
|
10
|
+
* `.nullable()`, etc. are available without any setup.
|
|
11
|
+
*
|
|
12
|
+
* @module
|
|
13
|
+
*/
|
|
14
|
+
import type { AnySchemaBuilder } from '../builders/AnySchemaBuilder.js';
|
|
15
|
+
import type { ArraySchemaBuilder } from '../builders/ArraySchemaBuilder.js';
|
|
16
|
+
import type { BooleanSchemaBuilder } from '../builders/BooleanSchemaBuilder.js';
|
|
17
|
+
import type { DateSchemaBuilder } from '../builders/DateSchemaBuilder.js';
|
|
18
|
+
import type { FunctionSchemaBuilder } from '../builders/FunctionSchemaBuilder.js';
|
|
19
|
+
import type { NumberSchemaBuilder } from '../builders/NumberSchemaBuilder.js';
|
|
20
|
+
import type { ObjectSchemaBuilder } from '../builders/ObjectSchemaBuilder.js';
|
|
21
|
+
import type { RecordSchemaBuilder } from '../builders/RecordSchemaBuilder.js';
|
|
22
|
+
import type { SchemaBuilder, ValidationErrorMessageProvider } from '../builders/SchemaBuilder.js';
|
|
23
|
+
import type { StringSchemaBuilder } from '../builders/StringSchemaBuilder.js';
|
|
24
|
+
import type { TupleSchemaBuilder } from '../builders/TupleSchemaBuilder.js';
|
|
25
|
+
import type { UnionSchemaBuilder } from '../builders/UnionSchemaBuilder.js';
|
|
26
|
+
import type { HiddenExtensionMethods } from '../extension.js';
|
|
27
|
+
import type { ArrayBuiltinExtensions } from './array.js';
|
|
28
|
+
import type { NumberBuiltinExtensions } from './number.js';
|
|
29
|
+
import type { StringBuiltinExtensions } from './string.js';
|
|
30
|
+
export { type ArrayBuiltinExtensions, arrayExtensions } from './array.js';
|
|
31
|
+
export { type NumberBuiltinExtensions, type NumberOneOfExtension, numberExtensions } from './number.js';
|
|
32
|
+
export { type StringBuiltinExtensions, type StringOneOfExtension, stringExtensions } from './string.js';
|
|
33
|
+
/** A `StringSchemaBuilder` with built-in extension methods. */
|
|
34
|
+
export type ExtendedString<T extends string = string> = StringSchemaBuilder<T, true, false, false, StringBuiltinExtensions<T>> & StringBuiltinExtensions<T> & HiddenExtensionMethods;
|
|
35
|
+
/** A `NumberSchemaBuilder` with built-in extension methods. */
|
|
36
|
+
export type ExtendedNumber<T extends number = number> = NumberSchemaBuilder<T, true, false, false, NumberBuiltinExtensions<T>> & NumberBuiltinExtensions<T> & HiddenExtensionMethods;
|
|
37
|
+
/** An `ArraySchemaBuilder` with built-in extension methods. */
|
|
38
|
+
export type ExtendedArray<TElementSchema extends SchemaBuilder<any, any, any, any, any> = SchemaBuilder<any, any, any>> = ArraySchemaBuilder<TElementSchema, true, false, undefined, false, ArrayBuiltinExtensions<TElementSchema>> & ArrayBuiltinExtensions<TElementSchema> & HiddenExtensionMethods;
|
|
39
|
+
/** A `BooleanSchemaBuilder` with built-in extension methods. */
|
|
40
|
+
export type ExtendedBoolean = BooleanSchemaBuilder<boolean, true, false, undefined, false, {}> & HiddenExtensionMethods;
|
|
41
|
+
/** A `DateSchemaBuilder` with built-in extension methods. */
|
|
42
|
+
export type ExtendedDate = DateSchemaBuilder<Date, true, false, false, {}> & HiddenExtensionMethods;
|
|
43
|
+
/** An `ObjectSchemaBuilder` with built-in extension methods. */
|
|
44
|
+
export type ExtendedObject<TProps extends Record<string, SchemaBuilder<any, any, any, any, any>> = {}> = ObjectSchemaBuilder<TProps, true, false, undefined, false, {}> & HiddenExtensionMethods;
|
|
45
|
+
/** A `UnionSchemaBuilder` with built-in extension methods. */
|
|
46
|
+
export type ExtendedUnion<TOptions extends readonly SchemaBuilder<any, any, any, any, any>[]> = UnionSchemaBuilder<TOptions, true, false, undefined, false, {}> & HiddenExtensionMethods;
|
|
47
|
+
/** A `FunctionSchemaBuilder` with built-in extension methods. */
|
|
48
|
+
export type ExtendedFunc = FunctionSchemaBuilder<true, false, undefined, false, {}> & HiddenExtensionMethods;
|
|
49
|
+
/** An `AnySchemaBuilder` with built-in extension methods. */
|
|
50
|
+
export type ExtendedAny = AnySchemaBuilder<true, false, undefined, false, {}> & HiddenExtensionMethods;
|
|
51
|
+
/** A `TupleSchemaBuilder` with built-in extension methods. */
|
|
52
|
+
export type ExtendedTuple<TElements extends readonly SchemaBuilder<any, any, any>[] = readonly SchemaBuilder<any, any, any, any, any>[]> = TupleSchemaBuilder<TElements, true, false, undefined, false, {}> & HiddenExtensionMethods;
|
|
53
|
+
/** A `RecordSchemaBuilder` with built-in extension methods. */
|
|
54
|
+
export type ExtendedRecord<TKeySchema extends StringSchemaBuilder<any, any, any, any> = StringSchemaBuilder<any, any, any, any>, TValueSchema extends SchemaBuilder<any, any, any, any, any> = SchemaBuilder<any, any, any>> = RecordSchemaBuilder<TKeySchema, TValueSchema, true, false, undefined, false, {}> & HiddenExtensionMethods;
|
|
55
|
+
export declare const string: {
|
|
56
|
+
(): ExtendedString;
|
|
57
|
+
<T extends string>(equals: T): ExtendedString<T>;
|
|
58
|
+
};
|
|
59
|
+
export declare const number: {
|
|
60
|
+
(): ExtendedNumber;
|
|
61
|
+
<T extends number>(equals: T): ExtendedNumber<T>;
|
|
62
|
+
};
|
|
63
|
+
export declare const array: <TElementSchema extends SchemaBuilder<any, any, any, any, any>>(elementSchema?: TElementSchema) => ExtendedArray<TElementSchema>;
|
|
64
|
+
export declare const boolean: () => ExtendedBoolean;
|
|
65
|
+
export declare const date: () => ExtendedDate;
|
|
66
|
+
export declare const object: {
|
|
67
|
+
(): ExtendedObject<{}>;
|
|
68
|
+
<TProps extends Record<string, SchemaBuilder<any, any, any, any, any>>>(props: TProps): ExtendedObject<TProps>;
|
|
69
|
+
<TProps extends Record<string, SchemaBuilder<any, any, any, any, any>>>(props?: TProps): ExtendedObject<TProps>;
|
|
70
|
+
};
|
|
71
|
+
export declare const union: <TOptions extends SchemaBuilder<any, any, any, any, any>>(schema: TOptions) => ExtendedUnion<[TOptions]>;
|
|
72
|
+
export declare const func: () => ExtendedFunc;
|
|
73
|
+
export declare const any: () => ExtendedAny;
|
|
74
|
+
export declare const tuple: <const TElements extends readonly SchemaBuilder<any, any, any, any, any>[]>(elements: [...TElements]) => ExtendedTuple<TElements>;
|
|
75
|
+
export declare const record: <TKeySchema extends StringSchemaBuilder<any, any, any, any>, TValueSchema extends SchemaBuilder<any, any, any, any, any>>(keySchema: TKeySchema, valueSchema: TValueSchema) => ExtendedRecord<TKeySchema, TValueSchema>;
|
|
76
|
+
/**
|
|
77
|
+
* Creates a string schema constrained to the given literal values.
|
|
78
|
+
*
|
|
79
|
+
* Convenience factory equivalent to `string().oneOf(...values)`.
|
|
80
|
+
* Mirrors Zod's `z.enum(['admin', 'user', 'guest'])` API.
|
|
81
|
+
*
|
|
82
|
+
* **Rest-params form** (no custom error message):
|
|
83
|
+
* ```ts
|
|
84
|
+
* const Role = enumOf('admin', 'user', 'guest');
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* **Array form** (with optional custom error message):
|
|
88
|
+
* ```ts
|
|
89
|
+
* const Role = enumOf(['admin', 'user', 'guest'], 'Invalid role');
|
|
90
|
+
* const Role2 = enumOf(['admin', 'user'], (val) => `"${val}" is not a valid role`);
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
* @param values - the allowed string literals (at least one required)
|
|
94
|
+
* @returns a typed `StringSchemaBuilder` that only accepts the given values
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* ```ts
|
|
98
|
+
* import { enumOf, InferType } from '@cleverbrush/schema';
|
|
99
|
+
*
|
|
100
|
+
* const Role = enumOf('admin', 'user', 'guest');
|
|
101
|
+
* type Role = InferType<typeof Role>; // 'admin' | 'user' | 'guest'
|
|
102
|
+
*
|
|
103
|
+
* Role.validate('admin'); // valid
|
|
104
|
+
* Role.validate('other'); // invalid
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export declare function enumOf<const T extends string>(...values: [T, ...T[]]): ExtendedString<T>;
|
|
108
|
+
export declare function enumOf<const T extends string>(values: readonly [T, ...T[]], errorMessage?: ValidationErrorMessageProvider<StringSchemaBuilder>): ExtendedString<T>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deprecated nullable extension types for `@cleverbrush/schema`.
|
|
3
|
+
*
|
|
4
|
+
* `nullable()` and `notNullable()` are now first-class methods on
|
|
5
|
+
* `SchemaBuilder`. These types are kept for backward compatibility
|
|
6
|
+
* but will be removed in a future major version.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
* @deprecated Use the first-class `.nullable()` / `.notNullable()` methods
|
|
10
|
+
* on `SchemaBuilder` instead.
|
|
11
|
+
*/
|
|
12
|
+
import type { NullSchemaBuilder } from '../builders/NullSchemaBuilder.js';
|
|
13
|
+
import type { SchemaBuilder } from '../builders/SchemaBuilder.js';
|
|
14
|
+
import type { UnionSchemaBuilder } from '../builders/UnionSchemaBuilder.js';
|
|
15
|
+
/**
|
|
16
|
+
* @deprecated `nullable()` is now a first-class method on `SchemaBuilder`.
|
|
17
|
+
* This type is kept for backward compatibility only.
|
|
18
|
+
*/
|
|
19
|
+
export type NullableReturn<TBuilder extends SchemaBuilder<any, any, any>> = UnionSchemaBuilder<[TBuilder, NullSchemaBuilder<true>]>;
|
|
20
|
+
/**
|
|
21
|
+
* @deprecated `nullable()` is now a first-class method on `SchemaBuilder`.
|
|
22
|
+
* This type is kept for backward compatibility only.
|
|
23
|
+
*/
|
|
24
|
+
export interface NullableMethod<TBuilder extends SchemaBuilder<any, any, any>> {
|
|
25
|
+
nullable(): NullableReturn<TBuilder>;
|
|
26
|
+
}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Built-in number extensions for `@cleverbrush/schema`.
|
|
3
|
+
*
|
|
4
|
+
* Provides common number validators: {@link numberExtensions | positive},
|
|
5
|
+
* {@link numberExtensions | negative}, {@link numberExtensions | finite},
|
|
6
|
+
* {@link numberExtensions | multipleOf}, and {@link numberExtensions | oneOf}.
|
|
7
|
+
*
|
|
8
|
+
* These are pre-applied in the default `@cleverbrush/schema` import.
|
|
9
|
+
* Import from `@cleverbrush/schema/core` to get bare builders without these extensions.
|
|
10
|
+
*
|
|
11
|
+
* @module
|
|
12
|
+
*/
|
|
13
|
+
import type { NumberSchemaBuilder } from '../builders/NumberSchemaBuilder.js';
|
|
14
|
+
import type { ValidationErrorMessageProvider } from '../builders/SchemaBuilder.js';
|
|
15
|
+
import type { HiddenExtensionMethods } from '../extension.js';
|
|
16
|
+
/** Return type shared by every method on {@link NumberBuiltinExtensions}. */
|
|
17
|
+
type NumberExtReturn<T extends number = number> = NumberSchemaBuilder<T, true, false, false, NumberBuiltinExtensions<T>> & NumberBuiltinExtensions<T> & HiddenExtensionMethods;
|
|
18
|
+
/**
|
|
19
|
+
* Methods added to `NumberSchemaBuilder` by the built-in number extension pack.
|
|
20
|
+
*
|
|
21
|
+
* **WORKAROUND:** This interface duplicates the method signatures from
|
|
22
|
+
* `numberExtensions` so that JSDoc survives into the published `.d.ts`
|
|
23
|
+
* files. TypeScript strips JSDoc when method signatures are reconstructed
|
|
24
|
+
* through the `FixedMethods` mapped type (conditional `infer` loses
|
|
25
|
+
* comments). Remove this interface once TypeScript preserves JSDoc
|
|
26
|
+
* through mapped types / conditional type inference.
|
|
27
|
+
*
|
|
28
|
+
* @see https://github.com/microsoft/TypeScript/issues/50715
|
|
29
|
+
*/
|
|
30
|
+
export interface NumberBuiltinExtensions<T extends number = number> {
|
|
31
|
+
/**
|
|
32
|
+
* Validates that the number is strictly greater than zero.
|
|
33
|
+
*
|
|
34
|
+
* @param errorMessage - custom error message or function to generate one
|
|
35
|
+
* @returns a new schema builder with the positive validator applied
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* number().positive();
|
|
40
|
+
* number().positive('Must be greater than zero');
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
positive(errorMessage?: ValidationErrorMessageProvider<NumberSchemaBuilder>): NumberExtReturn<T>;
|
|
44
|
+
/**
|
|
45
|
+
* Validates that the number is strictly less than zero.
|
|
46
|
+
*
|
|
47
|
+
* @param errorMessage - custom error message or function to generate one
|
|
48
|
+
* @returns a new schema builder with the negative validator applied
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* number().negative();
|
|
53
|
+
* number().negative('Must be below zero');
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
negative(errorMessage?: ValidationErrorMessageProvider<NumberSchemaBuilder>): NumberExtReturn<T>;
|
|
57
|
+
/**
|
|
58
|
+
* Validates that the number is finite (rejects `Infinity` and `-Infinity`).
|
|
59
|
+
*
|
|
60
|
+
* @param errorMessage - custom error message or function to generate one
|
|
61
|
+
* @returns a new schema builder with the finite validator applied
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* number().finite();
|
|
66
|
+
* number().finite('No infinities allowed');
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
finite(errorMessage?: ValidationErrorMessageProvider<NumberSchemaBuilder>): NumberExtReturn<T>;
|
|
70
|
+
/**
|
|
71
|
+
* Validates that the number is an exact multiple of `n`.
|
|
72
|
+
*
|
|
73
|
+
* Uses a relative tolerance of `1e-10` for float-safe comparison.
|
|
74
|
+
*
|
|
75
|
+
* @param n - the divisor to check against
|
|
76
|
+
* @param errorMessage - custom error message or function to generate one
|
|
77
|
+
* @returns a new schema builder with the multipleOf validator applied
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* ```ts
|
|
81
|
+
* number().multipleOf(5);
|
|
82
|
+
* number().multipleOf(0.1, 'Must be a multiple of 0.1');
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
multipleOf(n: number, errorMessage?: ValidationErrorMessageProvider<NumberSchemaBuilder>): NumberExtReturn<T>;
|
|
86
|
+
/**
|
|
87
|
+
* Constrains the number to one of the specified literal values.
|
|
88
|
+
*
|
|
89
|
+
* Narrows the inferred type from `number` to the union of the
|
|
90
|
+
* provided literals.
|
|
91
|
+
*
|
|
92
|
+
* @param values - the allowed number literals
|
|
93
|
+
* @returns a new schema builder restricted to the given values
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* import { number, InferType } from '@cleverbrush/schema';
|
|
98
|
+
*
|
|
99
|
+
* const priority = number().oneOf(1, 2, 3);
|
|
100
|
+
* type Priority = InferType<typeof priority>; // 1 | 2 | 3
|
|
101
|
+
*
|
|
102
|
+
* priority.validate(1); // valid
|
|
103
|
+
* priority.validate(4); // invalid — "must be one of: 1, 2, 3"
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
106
|
+
oneOf<V extends number>(...values: [V, ...V[]]): NumberExtReturn<V>;
|
|
107
|
+
/**
|
|
108
|
+
* Constrains the number to one of the specified literal values,
|
|
109
|
+
* with a custom error message or factory as the last argument.
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* ```ts
|
|
113
|
+
* const priority = number().oneOf(1, 2, 3, 'Priority must be 1, 2, or 3');
|
|
114
|
+
* const priority2 = number().oneOf(1, 2, 3, (val) => `${val} is not a valid priority`);
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
oneOf<V extends number>(...args: [
|
|
118
|
+
...[V, ...V[]],
|
|
119
|
+
ValidationErrorMessageProvider<NumberSchemaBuilder>
|
|
120
|
+
]): NumberExtReturn<V>;
|
|
121
|
+
/**
|
|
122
|
+
* Constrains the number to one of the specified literal values,
|
|
123
|
+
* with an optional custom error message or factory.
|
|
124
|
+
*
|
|
125
|
+
* @param values - the allowed number literals as an array
|
|
126
|
+
* @param errorMessage - optional custom error message or factory function
|
|
127
|
+
* @returns a new schema builder restricted to the given values
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* const priority = number().oneOf([1, 2, 3], 'Must be 1, 2, or 3');
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
oneOf<V extends number>(values: readonly [V, ...V[]], errorMessage?: ValidationErrorMessageProvider<NumberSchemaBuilder>): NumberExtReturn<V>;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Subset of {@link NumberBuiltinExtensions} containing only the `.oneOf()` overloads.
|
|
138
|
+
* Exported for backward compatibility.
|
|
139
|
+
*/
|
|
140
|
+
export type NumberOneOfExtension = Pick<NumberBuiltinExtensions, 'oneOf'>;
|
|
141
|
+
/**
|
|
142
|
+
* Extension descriptor that adds common number validators
|
|
143
|
+
* to `NumberSchemaBuilder`.
|
|
144
|
+
*
|
|
145
|
+
* Included methods: `positive`, `negative`, `finite`, `multipleOf`, `oneOf`.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```ts
|
|
149
|
+
* import { withExtensions } from '@cleverbrush/schema/core';
|
|
150
|
+
* import { numberExtensions } from '@cleverbrush/schema';
|
|
151
|
+
*
|
|
152
|
+
* const s = withExtensions(numberExtensions);
|
|
153
|
+
* const schema = s.number().positive().multipleOf(5);
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
export declare const numberExtensions: import("../extension.js").ExtensionDescriptor<{
|
|
157
|
+
number: {
|
|
158
|
+
/**
|
|
159
|
+
* Validates that the number is strictly greater than zero.
|
|
160
|
+
*
|
|
161
|
+
* @param errorMessage - custom error message or function to generate one
|
|
162
|
+
* @returns a new schema builder with the positive validator applied
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* ```ts
|
|
166
|
+
* number().positive();
|
|
167
|
+
* number().positive('Must be greater than zero');
|
|
168
|
+
* ```
|
|
169
|
+
*/
|
|
170
|
+
positive(this: NumberSchemaBuilder, errorMessage?: ValidationErrorMessageProvider<NumberSchemaBuilder>): NumberSchemaBuilder<number, true, false, false, {}>;
|
|
171
|
+
/**
|
|
172
|
+
* Validates that the number is strictly less than zero.
|
|
173
|
+
*
|
|
174
|
+
* @param errorMessage - custom error message or function to generate one
|
|
175
|
+
* @returns a new schema builder with the negative validator applied
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```ts
|
|
179
|
+
* number().negative();
|
|
180
|
+
* number().negative('Must be below zero');
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
negative(this: NumberSchemaBuilder, errorMessage?: ValidationErrorMessageProvider<NumberSchemaBuilder>): NumberSchemaBuilder<number, true, false, false, {}>;
|
|
184
|
+
/**
|
|
185
|
+
* Validates that the number is finite (rejects `Infinity` and `-Infinity`).
|
|
186
|
+
*
|
|
187
|
+
* @param errorMessage - custom error message or function to generate one
|
|
188
|
+
* @returns a new schema builder with the finite validator applied
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* ```ts
|
|
192
|
+
* number().finite();
|
|
193
|
+
* number().finite('No infinities allowed');
|
|
194
|
+
* ```
|
|
195
|
+
*/
|
|
196
|
+
finite(this: NumberSchemaBuilder, errorMessage?: ValidationErrorMessageProvider<NumberSchemaBuilder>): NumberSchemaBuilder<number, true, false, false, {}>;
|
|
197
|
+
/**
|
|
198
|
+
* Validates that the number is an exact multiple of `n`.
|
|
199
|
+
*
|
|
200
|
+
* Uses a relative tolerance of `1e-10` for float-safe comparison.
|
|
201
|
+
*
|
|
202
|
+
* @param n - the divisor to check against
|
|
203
|
+
* @param errorMessage - custom error message or function to generate one
|
|
204
|
+
* @returns a new schema builder with the multipleOf validator applied
|
|
205
|
+
*
|
|
206
|
+
* @example
|
|
207
|
+
* ```ts
|
|
208
|
+
* number().multipleOf(5);
|
|
209
|
+
* number().multipleOf(0.1, 'Must be a multiple of 0.1');
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
212
|
+
multipleOf(this: NumberSchemaBuilder, n: number, errorMessage?: ValidationErrorMessageProvider<NumberSchemaBuilder>): NumberSchemaBuilder<number, true, false, false, {}>;
|
|
213
|
+
/**
|
|
214
|
+
* Constrains the number to one of the specified literal values.
|
|
215
|
+
*
|
|
216
|
+
* @param args - the allowed number literals, optionally followed by an error message
|
|
217
|
+
* @returns a new schema builder restricted to the given values
|
|
218
|
+
*
|
|
219
|
+
* @example
|
|
220
|
+
* ```ts
|
|
221
|
+
* number().oneOf(1, 2, 3);
|
|
222
|
+
* number().oneOf([1, 2, 3], 'Must be 1, 2, or 3');
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
225
|
+
oneOf(this: NumberSchemaBuilder, ...args: any[]): NumberSchemaBuilder<number, true, false, false, {}>;
|
|
226
|
+
};
|
|
227
|
+
}>;
|
|
228
|
+
export {};
|