valrs 0.1.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 +197 -0
- package/dist/compiler.d.ts +195 -0
- package/dist/compiler.d.ts.map +1 -0
- package/dist/compiler.js +349 -0
- package/dist/compiler.js.map +1 -0
- package/dist/error.d.ts +415 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.js +619 -0
- package/dist/error.js.map +1 -0
- package/dist/factory.d.ts +107 -0
- package/dist/factory.d.ts.map +1 -0
- package/dist/factory.js +135 -0
- package/dist/factory.js.map +1 -0
- package/dist/index.d.ts +79 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +104 -0
- package/dist/index.js.map +1 -0
- package/dist/primitives.d.ts +99 -0
- package/dist/primitives.d.ts.map +1 -0
- package/dist/primitives.js +315 -0
- package/dist/primitives.js.map +1 -0
- package/dist/schema.d.ts +710 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +1179 -0
- package/dist/schema.js.map +1 -0
- package/dist/streaming.d.ts +159 -0
- package/dist/streaming.d.ts.map +1 -0
- package/dist/streaming.js +692 -0
- package/dist/streaming.js.map +1 -0
- package/dist/types.d.ts +107 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +19 -0
- package/dist/types.js.map +1 -0
- package/dist/v.d.ts +1382 -0
- package/dist/v.d.ts.map +1 -0
- package/dist/v.js +2396 -0
- package/dist/v.js.map +1 -0
- package/dist/wasm.d.ts +86 -0
- package/dist/wasm.d.ts.map +1 -0
- package/dist/wasm.js +87 -0
- package/dist/wasm.js.map +1 -0
- package/package.json +89 -0
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,710 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base schema class providing the fluent API wrapper around Standard Schema.
|
|
3
|
+
*
|
|
4
|
+
* This module provides the `ValSchema` base class that all schema types extend.
|
|
5
|
+
* It wraps the internal Standard Schema implementation and exposes Zod-like methods.
|
|
6
|
+
*/
|
|
7
|
+
import type { StandardSchemaV1, StandardJSONSchemaV1, ValidationResult, JsonSchemaOptions, PathSegment } from './types';
|
|
8
|
+
import { ValError } from './error';
|
|
9
|
+
/**
|
|
10
|
+
* Context object passed to superRefine callbacks.
|
|
11
|
+
* Allows adding multiple validation issues.
|
|
12
|
+
*/
|
|
13
|
+
export interface RefinementContext {
|
|
14
|
+
/**
|
|
15
|
+
* Adds a validation issue.
|
|
16
|
+
*/
|
|
17
|
+
addIssue(issue: {
|
|
18
|
+
code: string;
|
|
19
|
+
message: string;
|
|
20
|
+
path?: ReadonlyArray<PathSegment>;
|
|
21
|
+
fatal?: boolean;
|
|
22
|
+
}): void;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Options for refine method.
|
|
26
|
+
*/
|
|
27
|
+
export interface RefinementOptions {
|
|
28
|
+
message?: string;
|
|
29
|
+
path?: ReadonlyArray<PathSegment>;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Transform function type - can be sync or async.
|
|
33
|
+
*/
|
|
34
|
+
export type TransformFn<Input, Output> = (value: Input) => Output | Promise<Output>;
|
|
35
|
+
/**
|
|
36
|
+
* Refinement predicate function type - can be sync or async.
|
|
37
|
+
*/
|
|
38
|
+
export type RefinePredicate<T> = (value: T) => boolean | Promise<boolean>;
|
|
39
|
+
/**
|
|
40
|
+
* SuperRefine function type - can be sync or async.
|
|
41
|
+
*/
|
|
42
|
+
export type SuperRefineFn<T> = (value: T, ctx: RefinementContext) => void | Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* The result of a successful `safeParse()` call.
|
|
45
|
+
*/
|
|
46
|
+
export interface SafeParseSuccess<T> {
|
|
47
|
+
readonly success: true;
|
|
48
|
+
readonly data: T;
|
|
49
|
+
readonly error?: undefined;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The result of a failed `safeParse()` call.
|
|
53
|
+
*/
|
|
54
|
+
export interface SafeParseError {
|
|
55
|
+
readonly success: false;
|
|
56
|
+
readonly error: ValError;
|
|
57
|
+
readonly data?: undefined;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The result of a `safeParse()` call.
|
|
61
|
+
*/
|
|
62
|
+
export type SafeParseResult<T> = SafeParseSuccess<T> | SafeParseError;
|
|
63
|
+
/**
|
|
64
|
+
* Type guard to check if a SafeParseResult is successful.
|
|
65
|
+
*/
|
|
66
|
+
export declare function isSafeParseSuccess<T>(result: SafeParseResult<T>): result is SafeParseSuccess<T>;
|
|
67
|
+
/**
|
|
68
|
+
* Type guard to check if a SafeParseResult is a failure.
|
|
69
|
+
*/
|
|
70
|
+
export declare function isSafeParseError<T>(result: SafeParseResult<T>): result is SafeParseError;
|
|
71
|
+
/**
|
|
72
|
+
* Internal validation function type.
|
|
73
|
+
*/
|
|
74
|
+
type ValidateFn<T> = (value: unknown) => ValidationResult<T>;
|
|
75
|
+
/**
|
|
76
|
+
* Internal JSON Schema function type.
|
|
77
|
+
*/
|
|
78
|
+
type JsonSchemaFn = (target: string) => Record<string, unknown>;
|
|
79
|
+
/**
|
|
80
|
+
* Base schema class that provides the Zod-compatible API.
|
|
81
|
+
*
|
|
82
|
+
* All schema types extend this class, inheriting methods like `parse()`,
|
|
83
|
+
* `safeParse()`, and maintaining Standard Schema compliance via `~standard`.
|
|
84
|
+
*
|
|
85
|
+
* @template Input - The expected input type
|
|
86
|
+
* @template Output - The validated output type (defaults to Input)
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```typescript
|
|
90
|
+
* const schema = v.string();
|
|
91
|
+
*
|
|
92
|
+
* // Throws ValError on failure
|
|
93
|
+
* const value = schema.parse('hello');
|
|
94
|
+
*
|
|
95
|
+
* // Returns { success, data } or { success, error }
|
|
96
|
+
* const result = schema.safeParse('hello');
|
|
97
|
+
* if (result.success) {
|
|
98
|
+
* console.log(result.data);
|
|
99
|
+
* }
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
export declare class ValSchema<Input = unknown, Output = Input> implements StandardJSONSchemaV1<Input, Output> {
|
|
103
|
+
/**
|
|
104
|
+
* Standard Schema interface for interoperability.
|
|
105
|
+
*
|
|
106
|
+
* This property makes ValSchema compatible with any library that
|
|
107
|
+
* supports the Standard Schema specification.
|
|
108
|
+
*/
|
|
109
|
+
readonly '~standard': {
|
|
110
|
+
readonly version: 1;
|
|
111
|
+
readonly vendor: 'valrs';
|
|
112
|
+
readonly validate: (value: unknown) => ValidationResult<Output>;
|
|
113
|
+
readonly jsonSchema: {
|
|
114
|
+
readonly input: (options: JsonSchemaOptions) => Record<string, unknown>;
|
|
115
|
+
readonly output: (options: JsonSchemaOptions) => Record<string, unknown>;
|
|
116
|
+
};
|
|
117
|
+
};
|
|
118
|
+
/**
|
|
119
|
+
* Compiled validator for fast boolean checks.
|
|
120
|
+
* Lazily initialized on first check() call.
|
|
121
|
+
*/
|
|
122
|
+
private _compiledCheck?;
|
|
123
|
+
/**
|
|
124
|
+
* Whether this schema or any of its children contain transforms.
|
|
125
|
+
* When true, validation must go through WASM to apply transforms.
|
|
126
|
+
*/
|
|
127
|
+
protected _hasTransforms: boolean;
|
|
128
|
+
/**
|
|
129
|
+
* Returns whether this schema contains transforms.
|
|
130
|
+
* Used internally to determine validation path.
|
|
131
|
+
*/
|
|
132
|
+
hasTransforms(): boolean;
|
|
133
|
+
/**
|
|
134
|
+
* Creates a new ValSchema wrapping a validation function.
|
|
135
|
+
*
|
|
136
|
+
* @param validateFn - Function that validates input values
|
|
137
|
+
* @param inputJsonSchemaFn - Function that generates JSON Schema for input type
|
|
138
|
+
* @param outputJsonSchemaFn - Function that generates JSON Schema for output type
|
|
139
|
+
*/
|
|
140
|
+
constructor(validateFn: ValidateFn<Output>, inputJsonSchemaFn: JsonSchemaFn, outputJsonSchemaFn?: JsonSchemaFn);
|
|
141
|
+
/**
|
|
142
|
+
* Parses the input and returns the validated value.
|
|
143
|
+
*
|
|
144
|
+
* Throws a `ValError` if validation fails.
|
|
145
|
+
*
|
|
146
|
+
* @param data - The value to validate
|
|
147
|
+
* @returns The validated value with the correct type
|
|
148
|
+
* @throws {ValError} If validation fails
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```typescript
|
|
152
|
+
* const name = v.string().parse('Alice'); // 'Alice'
|
|
153
|
+
* const age = v.number().parse('42'); // throws ValError
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
156
|
+
parse(data: unknown): Output;
|
|
157
|
+
/**
|
|
158
|
+
* Parses the input and returns a result object.
|
|
159
|
+
*
|
|
160
|
+
* Never throws. Returns `{ success: true, data }` on success,
|
|
161
|
+
* or `{ success: false, error }` on failure.
|
|
162
|
+
*
|
|
163
|
+
* @param data - The value to validate
|
|
164
|
+
* @returns A result object indicating success or failure
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```typescript
|
|
168
|
+
* const result = v.string().safeParse(input);
|
|
169
|
+
*
|
|
170
|
+
* if (result.success) {
|
|
171
|
+
* console.log(result.data); // The validated string
|
|
172
|
+
* } else {
|
|
173
|
+
* console.log(result.error.issues); // Array of validation issues
|
|
174
|
+
* }
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
safeParse(data: unknown): SafeParseResult<Output>;
|
|
178
|
+
/**
|
|
179
|
+
* Alias for `safeParse()` that matches Standard Schema naming.
|
|
180
|
+
*/
|
|
181
|
+
validate(data: unknown): SafeParseResult<Output>;
|
|
182
|
+
/**
|
|
183
|
+
* Returns the JSON Schema for this schema's input type.
|
|
184
|
+
*
|
|
185
|
+
* @param target - The JSON Schema version (e.g., 'draft-2020-12', 'draft-07', 'openapi-3.0')
|
|
186
|
+
* @returns A JSON Schema object
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* ```typescript
|
|
190
|
+
* const schema = v.string();
|
|
191
|
+
* const jsonSchema = schema.toJsonSchema('draft-2020-12');
|
|
192
|
+
* // { type: 'string' }
|
|
193
|
+
* ```
|
|
194
|
+
*/
|
|
195
|
+
toJsonSchema(target?: string): Record<string, unknown>;
|
|
196
|
+
/**
|
|
197
|
+
* Fast boolean check using JS-compiled validator.
|
|
198
|
+
*
|
|
199
|
+
* Returns true if the data passes validation, false otherwise.
|
|
200
|
+
* Does NOT apply transforms - use safeParse() for that.
|
|
201
|
+
*
|
|
202
|
+
* @param data - The value to check
|
|
203
|
+
* @returns true if valid, false otherwise
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* ```typescript
|
|
207
|
+
* const schema = v.string();
|
|
208
|
+
* schema.check('hello'); // true
|
|
209
|
+
* schema.check(42); // false
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
212
|
+
check(data: unknown): boolean;
|
|
213
|
+
/**
|
|
214
|
+
* Checks if the schema is optional.
|
|
215
|
+
*
|
|
216
|
+
* Override in subclasses that support optionality.
|
|
217
|
+
*/
|
|
218
|
+
isOptional(): boolean;
|
|
219
|
+
/**
|
|
220
|
+
* Checks if the schema is nullable.
|
|
221
|
+
*
|
|
222
|
+
* Override in subclasses that support nullability.
|
|
223
|
+
*/
|
|
224
|
+
isNullable(): boolean;
|
|
225
|
+
/**
|
|
226
|
+
* Makes the schema accept undefined values.
|
|
227
|
+
*
|
|
228
|
+
* @returns A new schema that accepts T | undefined
|
|
229
|
+
*
|
|
230
|
+
* @example
|
|
231
|
+
* ```typescript
|
|
232
|
+
* const schema = v.string().optional();
|
|
233
|
+
* schema.parse(undefined); // undefined
|
|
234
|
+
* schema.parse('hello'); // 'hello'
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
237
|
+
optional(): ValOptional<this>;
|
|
238
|
+
/**
|
|
239
|
+
* Makes the schema accept null values.
|
|
240
|
+
*
|
|
241
|
+
* @returns A new schema that accepts T | null
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```typescript
|
|
245
|
+
* const schema = v.string().nullable();
|
|
246
|
+
* schema.parse(null); // null
|
|
247
|
+
* schema.parse('hello'); // 'hello'
|
|
248
|
+
* ```
|
|
249
|
+
*/
|
|
250
|
+
nullable(): ValNullable<this>;
|
|
251
|
+
/**
|
|
252
|
+
* Makes the schema accept null or undefined values.
|
|
253
|
+
*
|
|
254
|
+
* @returns A new schema that accepts T | null | undefined
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* ```typescript
|
|
258
|
+
* const schema = v.string().nullish();
|
|
259
|
+
* schema.parse(null); // null
|
|
260
|
+
* schema.parse(undefined); // undefined
|
|
261
|
+
* schema.parse('hello'); // 'hello'
|
|
262
|
+
* ```
|
|
263
|
+
*/
|
|
264
|
+
nullish(): ValNullish<this>;
|
|
265
|
+
/**
|
|
266
|
+
* Provides a default value when the input is undefined.
|
|
267
|
+
*
|
|
268
|
+
* @param defaultValue - The default value or a function that returns it
|
|
269
|
+
* @returns A new schema that uses the default when input is undefined
|
|
270
|
+
*
|
|
271
|
+
* @example
|
|
272
|
+
* ```typescript
|
|
273
|
+
* const schema = v.string().default('anonymous');
|
|
274
|
+
* schema.parse(undefined); // 'anonymous'
|
|
275
|
+
* schema.parse('hello'); // 'hello'
|
|
276
|
+
* ```
|
|
277
|
+
*/
|
|
278
|
+
default(defaultValue: Output | (() => Output)): ValDefault<this, Output>;
|
|
279
|
+
/**
|
|
280
|
+
* Provides a fallback value when parsing fails.
|
|
281
|
+
*
|
|
282
|
+
* @param catchValue - The fallback value or a function that returns it
|
|
283
|
+
* @returns A new schema that uses the fallback on parse error
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* ```typescript
|
|
287
|
+
* const schema = v.number().catch(0);
|
|
288
|
+
* schema.parse('not a number'); // 0
|
|
289
|
+
* schema.parse(42); // 42
|
|
290
|
+
* ```
|
|
291
|
+
*/
|
|
292
|
+
catch(catchValue: Output | (() => Output)): ValCatch<this, Output>;
|
|
293
|
+
/**
|
|
294
|
+
* Creates an array schema with this schema as the element type.
|
|
295
|
+
*
|
|
296
|
+
* This is an alternative syntax for `v.array(schema)`.
|
|
297
|
+
*
|
|
298
|
+
* @returns A new array schema
|
|
299
|
+
*
|
|
300
|
+
* @example
|
|
301
|
+
* ```typescript
|
|
302
|
+
* const schema = v.string().array();
|
|
303
|
+
* schema.parse(['a', 'b', 'c']); // ['a', 'b', 'c']
|
|
304
|
+
*
|
|
305
|
+
* type Arr = v.infer<typeof schema>; // string[]
|
|
306
|
+
* ```
|
|
307
|
+
*/
|
|
308
|
+
array(): ValArray<this>;
|
|
309
|
+
/**
|
|
310
|
+
* Creates a union of this schema with another schema.
|
|
311
|
+
*
|
|
312
|
+
* @param other - The other schema to union with
|
|
313
|
+
* @returns A new union schema
|
|
314
|
+
*
|
|
315
|
+
* @example
|
|
316
|
+
* ```typescript
|
|
317
|
+
* const schema = v.string().or(v.number());
|
|
318
|
+
* schema.parse('hello'); // 'hello'
|
|
319
|
+
* schema.parse(42); // 42
|
|
320
|
+
*
|
|
321
|
+
* type T = v.infer<typeof schema>; // string | number
|
|
322
|
+
* ```
|
|
323
|
+
*/
|
|
324
|
+
or<T extends ValSchema<unknown, unknown>>(other: T): ValUnion<readonly [this, T]>;
|
|
325
|
+
/**
|
|
326
|
+
* Creates an intersection of this schema with another schema.
|
|
327
|
+
*
|
|
328
|
+
* @param other - The other schema to intersect with
|
|
329
|
+
* @returns A new intersection schema
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* ```typescript
|
|
333
|
+
* const A = v.object({ a: v.string() });
|
|
334
|
+
* const B = v.object({ b: v.number() });
|
|
335
|
+
* const AB = A.and(B);
|
|
336
|
+
*
|
|
337
|
+
* type AB = v.infer<typeof AB>; // { a: string } & { b: number }
|
|
338
|
+
* ```
|
|
339
|
+
*/
|
|
340
|
+
and<T extends ValSchema<unknown, unknown>>(other: T): ValIntersection<this, T>;
|
|
341
|
+
/**
|
|
342
|
+
* Transforms the output value after validation.
|
|
343
|
+
*
|
|
344
|
+
* Changes the output type of the schema.
|
|
345
|
+
*
|
|
346
|
+
* @param fn - Transform function that receives validated value
|
|
347
|
+
* @returns A new schema with transformed output type
|
|
348
|
+
*
|
|
349
|
+
* @example
|
|
350
|
+
* ```typescript
|
|
351
|
+
* const schema = v.string().transform(s => parseInt(s, 10));
|
|
352
|
+
* schema.parse('42'); // 42 (number)
|
|
353
|
+
*
|
|
354
|
+
* type Input = v.input<typeof schema>; // string
|
|
355
|
+
* type Output = v.output<typeof schema>; // number
|
|
356
|
+
* ```
|
|
357
|
+
*/
|
|
358
|
+
transform<NewOutput>(fn: TransformFn<Output, NewOutput>): ValTransformed<Input, Output, NewOutput>;
|
|
359
|
+
/**
|
|
360
|
+
* Adds a custom validation refinement.
|
|
361
|
+
*
|
|
362
|
+
* Does not change the output type.
|
|
363
|
+
*
|
|
364
|
+
* @param predicate - Function that returns true if valid, false otherwise
|
|
365
|
+
* @param messageOrOptions - Error message or options object
|
|
366
|
+
* @returns A new schema with the refinement
|
|
367
|
+
*
|
|
368
|
+
* @example
|
|
369
|
+
* ```typescript
|
|
370
|
+
* const schema = v.string().refine(
|
|
371
|
+
* s => s.length > 0,
|
|
372
|
+
* 'Required'
|
|
373
|
+
* );
|
|
374
|
+
*
|
|
375
|
+
* const schemaWithPath = v.string().refine(
|
|
376
|
+
* s => s.includes('@'),
|
|
377
|
+
* { message: 'Must contain @', path: ['email'] }
|
|
378
|
+
* );
|
|
379
|
+
* ```
|
|
380
|
+
*/
|
|
381
|
+
refine(predicate: RefinePredicate<Output>, messageOrOptions?: string | RefinementOptions): ValRefined<Input, Output>;
|
|
382
|
+
/**
|
|
383
|
+
* Adds advanced refinement that can add multiple issues.
|
|
384
|
+
*
|
|
385
|
+
* Provides a context object for adding validation issues.
|
|
386
|
+
*
|
|
387
|
+
* @param fn - Refinement function with context
|
|
388
|
+
* @returns A new schema with the refinement
|
|
389
|
+
*
|
|
390
|
+
* @example
|
|
391
|
+
* ```typescript
|
|
392
|
+
* const schema = v.string().superRefine((val, ctx) => {
|
|
393
|
+
* if (val.length < 5) {
|
|
394
|
+
* ctx.addIssue({
|
|
395
|
+
* code: 'custom',
|
|
396
|
+
* message: 'Too short',
|
|
397
|
+
* path: [],
|
|
398
|
+
* });
|
|
399
|
+
* }
|
|
400
|
+
* if (val.length > 100) {
|
|
401
|
+
* ctx.addIssue({
|
|
402
|
+
* code: 'custom',
|
|
403
|
+
* message: 'Too long',
|
|
404
|
+
* path: [],
|
|
405
|
+
* });
|
|
406
|
+
* }
|
|
407
|
+
* });
|
|
408
|
+
* ```
|
|
409
|
+
*/
|
|
410
|
+
superRefine(fn: SuperRefineFn<Output>): ValSuperRefined<Input, Output>;
|
|
411
|
+
/**
|
|
412
|
+
* Pipes the output of this schema into another schema.
|
|
413
|
+
*
|
|
414
|
+
* Useful for chaining transforms with additional validation.
|
|
415
|
+
*
|
|
416
|
+
* @param schema - The schema to pipe into
|
|
417
|
+
* @returns A new schema that validates with both schemas
|
|
418
|
+
*
|
|
419
|
+
* @example
|
|
420
|
+
* ```typescript
|
|
421
|
+
* const schema = v.string()
|
|
422
|
+
* .transform(s => parseInt(s, 10))
|
|
423
|
+
* .pipe(v.number().positive());
|
|
424
|
+
*
|
|
425
|
+
* schema.parse('42'); // 42
|
|
426
|
+
* schema.parse('-5'); // throws (not positive)
|
|
427
|
+
* ```
|
|
428
|
+
*/
|
|
429
|
+
pipe<NextOutput>(schema: ValSchema<Output, NextOutput>): ValPiped<Input, Output, NextOutput>;
|
|
430
|
+
/**
|
|
431
|
+
* Asynchronously parses the input value.
|
|
432
|
+
*
|
|
433
|
+
* Required when using async transforms or refinements.
|
|
434
|
+
*
|
|
435
|
+
* @param data - The value to validate
|
|
436
|
+
* @returns Promise resolving to the validated value
|
|
437
|
+
* @throws {ValError} If validation fails
|
|
438
|
+
*
|
|
439
|
+
* @example
|
|
440
|
+
* ```typescript
|
|
441
|
+
* const schema = v.string().refine(async (s) => {
|
|
442
|
+
* return await checkIfExists(s);
|
|
443
|
+
* }, 'Not found');
|
|
444
|
+
*
|
|
445
|
+
* const value = await schema.parseAsync('test');
|
|
446
|
+
* ```
|
|
447
|
+
*/
|
|
448
|
+
parseAsync(data: unknown): Promise<Output>;
|
|
449
|
+
/**
|
|
450
|
+
* Asynchronously parses the input and returns a result object.
|
|
451
|
+
*
|
|
452
|
+
* Never throws. Required when using async transforms or refinements.
|
|
453
|
+
*
|
|
454
|
+
* @param data - The value to validate
|
|
455
|
+
* @returns Promise resolving to result object
|
|
456
|
+
*
|
|
457
|
+
* @example
|
|
458
|
+
* ```typescript
|
|
459
|
+
* const schema = v.string().transform(async (s) => {
|
|
460
|
+
* return await lookupValue(s);
|
|
461
|
+
* });
|
|
462
|
+
*
|
|
463
|
+
* const result = await schema.safeParseAsync('key');
|
|
464
|
+
* if (result.success) {
|
|
465
|
+
* console.log(result.data);
|
|
466
|
+
* }
|
|
467
|
+
* ```
|
|
468
|
+
*/
|
|
469
|
+
safeParseAsync(data: unknown): Promise<SafeParseResult<Output>>;
|
|
470
|
+
}
|
|
471
|
+
/**
|
|
472
|
+
* Extracts the output type from a ValSchema.
|
|
473
|
+
*/
|
|
474
|
+
type InferOutput<T extends ValSchema<unknown, unknown>> = T extends ValSchema<unknown, infer O> ? O : never;
|
|
475
|
+
/**
|
|
476
|
+
* Extracts the input type from a ValSchema.
|
|
477
|
+
*/
|
|
478
|
+
type InferInput<T extends ValSchema<unknown, unknown>> = T extends ValSchema<infer I, unknown> ? I : never;
|
|
479
|
+
/**
|
|
480
|
+
* Validator type for arrays.
|
|
481
|
+
*/
|
|
482
|
+
type ArrayValidator<T> = (value: ReadonlyArray<T>) => {
|
|
483
|
+
issues: Array<{
|
|
484
|
+
message: string;
|
|
485
|
+
}>;
|
|
486
|
+
} | null;
|
|
487
|
+
/**
|
|
488
|
+
* Schema for array values with validation methods.
|
|
489
|
+
*
|
|
490
|
+
* Supports chainable methods like `.min()`, `.max()`, `.length()`, `.nonempty()`.
|
|
491
|
+
* Each method returns a new schema instance (immutable).
|
|
492
|
+
*
|
|
493
|
+
* @template S - The element schema type
|
|
494
|
+
*
|
|
495
|
+
* @example
|
|
496
|
+
* ```typescript
|
|
497
|
+
* const schema = v.array(v.string());
|
|
498
|
+
* schema.parse(['a', 'b', 'c']); // ['a', 'b', 'c']
|
|
499
|
+
*
|
|
500
|
+
* const nonEmpty = v.array(v.number()).nonempty();
|
|
501
|
+
* nonEmpty.parse([1, 2]); // [1, 2]
|
|
502
|
+
* nonEmpty.parse([]); // throws
|
|
503
|
+
* ```
|
|
504
|
+
*/
|
|
505
|
+
export declare class ValArray<S extends ValSchema<unknown, unknown>> extends ValSchema<ReadonlyArray<InferInput<S>>, Array<InferOutput<S>>> {
|
|
506
|
+
readonly element: S;
|
|
507
|
+
private readonly validators;
|
|
508
|
+
constructor(elementSchema: S, validators?: ReadonlyArray<ArrayValidator<InferOutput<S>>>);
|
|
509
|
+
/**
|
|
510
|
+
* Creates a copy of this schema with additional validators.
|
|
511
|
+
*/
|
|
512
|
+
private clone;
|
|
513
|
+
/**
|
|
514
|
+
* Requires array to have at least `length` elements.
|
|
515
|
+
*/
|
|
516
|
+
min(length: number, message?: string): ValArray<S>;
|
|
517
|
+
/**
|
|
518
|
+
* Requires array to have at most `length` elements.
|
|
519
|
+
*/
|
|
520
|
+
max(length: number, message?: string): ValArray<S>;
|
|
521
|
+
/**
|
|
522
|
+
* Requires array to have exactly `len` elements.
|
|
523
|
+
*/
|
|
524
|
+
length(len: number, message?: string): ValArray<S>;
|
|
525
|
+
/**
|
|
526
|
+
* Requires array to be non-empty (at least 1 element).
|
|
527
|
+
*/
|
|
528
|
+
nonempty(message?: string): ValArray<S>;
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Infers union input type from an array of schemas.
|
|
532
|
+
*/
|
|
533
|
+
type InferUnionInput<T extends ReadonlyArray<ValSchema<unknown, unknown>>> = T[number] extends ValSchema<infer I, unknown> ? I : never;
|
|
534
|
+
/**
|
|
535
|
+
* Infers union output type from an array of schemas.
|
|
536
|
+
*/
|
|
537
|
+
type InferUnionOutput<T extends ReadonlyArray<ValSchema<unknown, unknown>>> = T[number] extends ValSchema<unknown, infer O> ? O : never;
|
|
538
|
+
/**
|
|
539
|
+
* Schema for union types (one of multiple schemas).
|
|
540
|
+
*
|
|
541
|
+
* @template T - Array of member schemas
|
|
542
|
+
*
|
|
543
|
+
* @example
|
|
544
|
+
* ```typescript
|
|
545
|
+
* const schema = v.union([v.string(), v.number()]);
|
|
546
|
+
* schema.parse('hello'); // 'hello'
|
|
547
|
+
* schema.parse(42); // 42
|
|
548
|
+
*
|
|
549
|
+
* type U = v.infer<typeof schema>; // string | number
|
|
550
|
+
* ```
|
|
551
|
+
*/
|
|
552
|
+
export declare class ValUnion<T extends ReadonlyArray<ValSchema<unknown, unknown>>> extends ValSchema<InferUnionInput<T>, InferUnionOutput<T>> {
|
|
553
|
+
readonly options: T;
|
|
554
|
+
constructor(options: T);
|
|
555
|
+
}
|
|
556
|
+
/**
|
|
557
|
+
* Schema for intersection types (all of multiple schemas).
|
|
558
|
+
*
|
|
559
|
+
* @template L - Left schema
|
|
560
|
+
* @template R - Right schema
|
|
561
|
+
*
|
|
562
|
+
* @example
|
|
563
|
+
* ```typescript
|
|
564
|
+
* const A = v.object({ a: v.string() });
|
|
565
|
+
* const B = v.object({ b: v.number() });
|
|
566
|
+
* const AB = v.intersection(A, B);
|
|
567
|
+
*
|
|
568
|
+
* type AB = v.infer<typeof AB>; // { a: string } & { b: number }
|
|
569
|
+
* ```
|
|
570
|
+
*/
|
|
571
|
+
export declare class ValIntersection<L extends ValSchema<unknown, unknown>, R extends ValSchema<unknown, unknown>> extends ValSchema<InferInput<L> & InferInput<R>, InferOutput<L> & InferOutput<R>> {
|
|
572
|
+
readonly left: L;
|
|
573
|
+
readonly right: R;
|
|
574
|
+
constructor(left: L, right: R);
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Schema wrapper that makes the inner schema optional (accepts undefined).
|
|
578
|
+
*/
|
|
579
|
+
export declare class ValOptional<T extends ValSchema<unknown, unknown>> extends ValSchema<InferInput<T> | undefined, InferOutput<T> | undefined> {
|
|
580
|
+
private readonly innerSchema;
|
|
581
|
+
constructor(schema: T);
|
|
582
|
+
isOptional(): boolean;
|
|
583
|
+
/**
|
|
584
|
+
* Returns the inner schema (unwrapped).
|
|
585
|
+
*/
|
|
586
|
+
unwrap(): T;
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Schema wrapper that makes the inner schema nullable (accepts null).
|
|
590
|
+
*/
|
|
591
|
+
export declare class ValNullable<T extends ValSchema<unknown, unknown>> extends ValSchema<InferInput<T> | null, InferOutput<T> | null> {
|
|
592
|
+
private readonly innerSchema;
|
|
593
|
+
constructor(schema: T);
|
|
594
|
+
isNullable(): boolean;
|
|
595
|
+
/**
|
|
596
|
+
* Returns the inner schema (unwrapped).
|
|
597
|
+
*/
|
|
598
|
+
unwrap(): T;
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* Schema wrapper that makes the inner schema nullish (accepts null or undefined).
|
|
602
|
+
*/
|
|
603
|
+
export declare class ValNullish<T extends ValSchema<unknown, unknown>> extends ValSchema<InferInput<T> | null | undefined, InferOutput<T> | null | undefined> {
|
|
604
|
+
private readonly innerSchema;
|
|
605
|
+
constructor(schema: T);
|
|
606
|
+
isOptional(): boolean;
|
|
607
|
+
isNullable(): boolean;
|
|
608
|
+
/**
|
|
609
|
+
* Returns the inner schema (unwrapped).
|
|
610
|
+
*/
|
|
611
|
+
unwrap(): T;
|
|
612
|
+
}
|
|
613
|
+
/**
|
|
614
|
+
* Schema wrapper that provides a default value when input is undefined.
|
|
615
|
+
*/
|
|
616
|
+
export declare class ValDefault<T extends ValSchema<unknown, unknown>, D> extends ValSchema<InferInput<T> | undefined, InferOutput<T>> {
|
|
617
|
+
private readonly innerSchema;
|
|
618
|
+
constructor(schema: T, defaultValue: D | (() => D));
|
|
619
|
+
/**
|
|
620
|
+
* Removes the default wrapper.
|
|
621
|
+
*/
|
|
622
|
+
removeDefault(): T;
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Schema wrapper that provides a catch value when parsing fails.
|
|
626
|
+
*/
|
|
627
|
+
export declare class ValCatch<T extends ValSchema<unknown, unknown>, C> extends ValSchema<unknown, InferOutput<T>> {
|
|
628
|
+
private readonly innerSchema;
|
|
629
|
+
constructor(schema: T, catchValue: C | (() => C));
|
|
630
|
+
/**
|
|
631
|
+
* Removes the catch wrapper.
|
|
632
|
+
*/
|
|
633
|
+
removeCatch(): T;
|
|
634
|
+
}
|
|
635
|
+
/**
|
|
636
|
+
* Schema that applies a transform function after validation.
|
|
637
|
+
*
|
|
638
|
+
* Changes the output type of the schema.
|
|
639
|
+
*
|
|
640
|
+
* @template Input - The original input type
|
|
641
|
+
* @template Middle - The intermediate validated type
|
|
642
|
+
* @template Output - The final transformed output type
|
|
643
|
+
*/
|
|
644
|
+
export declare class ValTransformed<Input, Middle, Output> extends ValSchema<Input, Output> {
|
|
645
|
+
constructor(schema: ValSchema<Input, Middle>, fn: TransformFn<Middle, Output>);
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* Schema that adds a refinement predicate.
|
|
649
|
+
*
|
|
650
|
+
* Does not change the output type.
|
|
651
|
+
*
|
|
652
|
+
* @template Input - The input type
|
|
653
|
+
* @template Output - The output type
|
|
654
|
+
*/
|
|
655
|
+
export declare class ValRefined<Input, Output> extends ValSchema<Input, Output> {
|
|
656
|
+
constructor(schema: ValSchema<Input, Output>, predicate: RefinePredicate<Output>, options: RefinementOptions);
|
|
657
|
+
}
|
|
658
|
+
/**
|
|
659
|
+
* Schema that applies a superRefine function for advanced validation.
|
|
660
|
+
*
|
|
661
|
+
* Allows adding multiple issues via the context.
|
|
662
|
+
*
|
|
663
|
+
* @template Input - The input type
|
|
664
|
+
* @template Output - The output type
|
|
665
|
+
*/
|
|
666
|
+
export declare class ValSuperRefined<Input, Output> extends ValSchema<Input, Output> {
|
|
667
|
+
constructor(schema: ValSchema<Input, Output>, fn: SuperRefineFn<Output>);
|
|
668
|
+
}
|
|
669
|
+
/**
|
|
670
|
+
* Schema that pipes the output of one schema into another.
|
|
671
|
+
*
|
|
672
|
+
* Useful for chaining transforms with additional validation.
|
|
673
|
+
*
|
|
674
|
+
* @template Input - The original input type
|
|
675
|
+
* @template Middle - The intermediate validated type
|
|
676
|
+
* @template Output - The final output type from the second schema
|
|
677
|
+
*/
|
|
678
|
+
export declare class ValPiped<Input, Middle, Output> extends ValSchema<Input, Output> {
|
|
679
|
+
constructor(first: ValSchema<Input, Middle>, second: ValSchema<Middle, Output>);
|
|
680
|
+
}
|
|
681
|
+
/**
|
|
682
|
+
* Schema that preprocesses input before validation.
|
|
683
|
+
*
|
|
684
|
+
* Useful for coercing input types before validation.
|
|
685
|
+
*
|
|
686
|
+
* @template Input - The preprocessed input type
|
|
687
|
+
* @template Output - The validated output type
|
|
688
|
+
*/
|
|
689
|
+
export declare class ValPreprocessed<Input, Output> extends ValSchema<unknown, Output> {
|
|
690
|
+
constructor(preprocessFn: (value: unknown) => Input, schema: ValSchema<Input, Output>);
|
|
691
|
+
}
|
|
692
|
+
/**
|
|
693
|
+
* Creates a ValSchema from an existing Standard Schema object.
|
|
694
|
+
*
|
|
695
|
+
* Useful for wrapping schemas created with the factory functions.
|
|
696
|
+
*
|
|
697
|
+
* @param standardSchema - A Standard Schema compliant object
|
|
698
|
+
* @returns A ValSchema instance
|
|
699
|
+
*/
|
|
700
|
+
export declare function fromStandardSchema<Input, Output>(standardSchema: StandardJSONSchemaV1<Input, Output>): ValSchema<Input, Output>;
|
|
701
|
+
/**
|
|
702
|
+
* Creates a ValSchema from a simple Standard Schema (without JSON Schema support).
|
|
703
|
+
*
|
|
704
|
+
* @param standardSchema - A Standard Schema compliant object
|
|
705
|
+
* @param jsonSchemaFn - Function to generate JSON Schema
|
|
706
|
+
* @returns A ValSchema instance
|
|
707
|
+
*/
|
|
708
|
+
export declare function fromSimpleStandardSchema<T>(standardSchema: StandardSchemaV1<T, T>, jsonSchemaFn: JsonSchemaFn): ValSchema<T, T>;
|
|
709
|
+
export {};
|
|
710
|
+
//# sourceMappingURL=schema.d.ts.map
|