atchara 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/release/index.cjs +2119 -0
- package/dist/release/index.d.cts +563 -0
- package/dist/release/index.d.ts +563 -0
- package/dist/release/index.js +2060 -0
- package/package.json +60 -0
|
@@ -0,0 +1,563 @@
|
|
|
1
|
+
import { Schema, DeferredPrimitive, Parser, InferDeferred, LargeParseResult, LazyContext, SerializedSchema, DeferredObject, DeferredNullable, DeferredValue, DeferredTuple, DeferredRecord, DeferredUnion, DeferredArray } from '@atcharajs/core';
|
|
2
|
+
export { AtcharaError, AtcharaErrorCode, AtcharaPosition, DeferredArray, DeferredNullable, DeferredObject, DeferredPrimitive, DeferredRecord, DeferredTuple, DeferredUnion, InferDeferred, InferInput, InferValue, InvalidSchema, InvalidUtf8, LargeParseResult, MissingRequired, Parser, Schema, SerializedSchema, UnexpectedCharacter, UnexpectedEof, UnexpectedField, UnionNoMatch, Unwrap, ValidationError, isAtcharaError, toBytes, wrapDeferred } from '@atcharajs/core';
|
|
3
|
+
import { Readable } from 'stream';
|
|
4
|
+
|
|
5
|
+
interface StringConstraints {
|
|
6
|
+
min?: number;
|
|
7
|
+
max?: number;
|
|
8
|
+
pattern?: string;
|
|
9
|
+
}
|
|
10
|
+
declare class StringSchema implements Schema<string, void, DeferredPrimitive<string>>, Parser<StringSchema> {
|
|
11
|
+
readonly _output: string;
|
|
12
|
+
readonly _input: void;
|
|
13
|
+
readonly _deferred: DeferredPrimitive<string>;
|
|
14
|
+
private _constraints;
|
|
15
|
+
private _parser?;
|
|
16
|
+
get schema(): StringSchema;
|
|
17
|
+
get constraints(): StringConstraints | undefined;
|
|
18
|
+
min(n: number): StringSchema;
|
|
19
|
+
max(n: number): StringSchema;
|
|
20
|
+
pattern(regex: RegExp | string): StringSchema;
|
|
21
|
+
parse(input: Uint8Array): InferDeferred<StringSchema>;
|
|
22
|
+
parseLarge(stream: Readable): Promise<LargeParseResult<StringSchema>>;
|
|
23
|
+
private getParser;
|
|
24
|
+
serializeSchema(counter: {
|
|
25
|
+
value: number;
|
|
26
|
+
} | null, _lazy?: LazyContext): SerializedSchema;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
type ObjectOutput<T extends Record<string, Parser<Schema>>> = {
|
|
30
|
+
[K in keyof T]: T[K]['schema']['_output'];
|
|
31
|
+
};
|
|
32
|
+
type ObjectInput<T extends Record<string, Parser<Schema>>> = {
|
|
33
|
+
[K in keyof T]: T[K]['schema']['_input'];
|
|
34
|
+
};
|
|
35
|
+
type ObjectDeferred<T extends Record<string, Parser<Schema>>> = {
|
|
36
|
+
[K in keyof T]: T[K]['schema']['_deferred'];
|
|
37
|
+
};
|
|
38
|
+
declare class ObjectSchema<T extends Record<string, Parser<Schema>>> implements Schema<ObjectOutput<T>, ObjectInput<T>, DeferredObject<ObjectOutput<T>, ObjectDeferred<T>>> {
|
|
39
|
+
readonly _shape: T;
|
|
40
|
+
readonly _output: ObjectOutput<T>;
|
|
41
|
+
readonly _input: ObjectInput<T>;
|
|
42
|
+
readonly _deferred: DeferredObject<ObjectOutput<T>, ObjectDeferred<T>>;
|
|
43
|
+
constructor(_shape: T);
|
|
44
|
+
serializeSchema(counter: {
|
|
45
|
+
value: number;
|
|
46
|
+
} | null, lazy?: LazyContext): SerializedSchema;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
interface NumberConstraints {
|
|
50
|
+
min?: number;
|
|
51
|
+
max?: number;
|
|
52
|
+
gt?: number;
|
|
53
|
+
lt?: number;
|
|
54
|
+
multipleOf?: number;
|
|
55
|
+
}
|
|
56
|
+
declare class NumberSchema implements Schema<number, void, DeferredPrimitive<number>>, Parser<NumberSchema> {
|
|
57
|
+
readonly _output: number;
|
|
58
|
+
readonly _input: void;
|
|
59
|
+
readonly _deferred: DeferredPrimitive<number>;
|
|
60
|
+
private _constraints;
|
|
61
|
+
private _parser?;
|
|
62
|
+
get schema(): NumberSchema;
|
|
63
|
+
get constraints(): NumberConstraints | undefined;
|
|
64
|
+
min(n: number): NumberSchema;
|
|
65
|
+
max(n: number): NumberSchema;
|
|
66
|
+
gt(n: number): NumberSchema;
|
|
67
|
+
lt(n: number): NumberSchema;
|
|
68
|
+
multipleOf(n: number): NumberSchema;
|
|
69
|
+
parse(input: Uint8Array): InferDeferred<NumberSchema>;
|
|
70
|
+
parseLarge(stream: Readable): Promise<LargeParseResult<NumberSchema>>;
|
|
71
|
+
private getParser;
|
|
72
|
+
serializeSchema(counter: {
|
|
73
|
+
value: number;
|
|
74
|
+
} | null, _lazy?: LazyContext): SerializedSchema;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
interface IntegerConstraints {
|
|
78
|
+
min?: number;
|
|
79
|
+
max?: number;
|
|
80
|
+
gt?: number;
|
|
81
|
+
lt?: number;
|
|
82
|
+
multipleOf?: number;
|
|
83
|
+
}
|
|
84
|
+
declare class IntegerSchema implements Schema<number, void, DeferredPrimitive<number>>, Parser<IntegerSchema> {
|
|
85
|
+
readonly _output: number;
|
|
86
|
+
readonly _input: void;
|
|
87
|
+
readonly _deferred: DeferredPrimitive<number>;
|
|
88
|
+
private _constraints;
|
|
89
|
+
private _parser?;
|
|
90
|
+
get schema(): IntegerSchema;
|
|
91
|
+
get constraints(): IntegerConstraints | undefined;
|
|
92
|
+
min(n: number): IntegerSchema;
|
|
93
|
+
max(n: number): IntegerSchema;
|
|
94
|
+
gt(n: number): IntegerSchema;
|
|
95
|
+
lt(n: number): IntegerSchema;
|
|
96
|
+
multipleOf(n: number): IntegerSchema;
|
|
97
|
+
parse(input: Uint8Array): InferDeferred<IntegerSchema>;
|
|
98
|
+
parseLarge(stream: Readable): Promise<LargeParseResult<IntegerSchema>>;
|
|
99
|
+
private getParser;
|
|
100
|
+
serializeSchema(counter: {
|
|
101
|
+
value: number;
|
|
102
|
+
} | null, _lazy?: LazyContext): SerializedSchema;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
declare class BooleanSchema implements Schema<boolean, void, DeferredPrimitive<boolean>> {
|
|
106
|
+
readonly _output: boolean;
|
|
107
|
+
readonly _input: void;
|
|
108
|
+
readonly _deferred: DeferredPrimitive<boolean>;
|
|
109
|
+
serializeSchema(counter: {
|
|
110
|
+
value: number;
|
|
111
|
+
} | null, _lazy?: LazyContext): SerializedSchema;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
declare class NullableSchema<T extends Parser<Schema>> implements Schema<T['schema']['_output'] | null, T['schema']['_input'] | null, DeferredNullable<T['schema']['_output']>> {
|
|
115
|
+
readonly _valueParser: T;
|
|
116
|
+
readonly _output: T['schema']['_output'] | null;
|
|
117
|
+
readonly _input: T['schema']['_input'] | null;
|
|
118
|
+
readonly _deferred: DeferredNullable<T['schema']['_output']>;
|
|
119
|
+
readonly _brand: "nullable";
|
|
120
|
+
constructor(_valueParser: T);
|
|
121
|
+
serializeSchema(counter: {
|
|
122
|
+
value: number;
|
|
123
|
+
} | null, lazy?: LazyContext): SerializedSchema;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
declare class OptionalSchema<T extends Parser<Schema>> implements Schema<T['schema']['_output'] | undefined, T['schema']['_input'] | undefined, T['schema']['_deferred'] | DeferredValue<undefined>> {
|
|
127
|
+
readonly _valueParser: T;
|
|
128
|
+
readonly _output: T['schema']['_output'] | undefined;
|
|
129
|
+
readonly _input: T['schema']['_input'] | undefined;
|
|
130
|
+
readonly _deferred: T['schema']['_deferred'] | DeferredValue<undefined>;
|
|
131
|
+
readonly _brand: "optional";
|
|
132
|
+
constructor(_valueParser: T);
|
|
133
|
+
serializeSchema(counter: {
|
|
134
|
+
value: number;
|
|
135
|
+
} | null, lazy?: LazyContext): SerializedSchema;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
declare class LiteralSchema<T extends string | number | boolean | null> implements Schema<T, T, DeferredPrimitive<T>> {
|
|
139
|
+
readonly _value: T;
|
|
140
|
+
readonly _output: T;
|
|
141
|
+
readonly _input: T;
|
|
142
|
+
readonly _deferred: DeferredPrimitive<T>;
|
|
143
|
+
constructor(_value: T);
|
|
144
|
+
serializeSchema(counter: {
|
|
145
|
+
value: number;
|
|
146
|
+
} | null, _lazy?: LazyContext): SerializedSchema;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
type TupleOutput<T extends readonly Parser<Schema>[]> = {
|
|
150
|
+
[K in keyof T]: T[K] extends Parser<Schema> ? T[K]['schema']['_output'] : never;
|
|
151
|
+
};
|
|
152
|
+
type TupleInput<T extends readonly Parser<Schema>[]> = {
|
|
153
|
+
[K in keyof T]: T[K] extends Parser<Schema> ? T[K]['schema']['_input'] : never;
|
|
154
|
+
};
|
|
155
|
+
type TupleDeferred<T extends readonly Parser<Schema>[]> = {
|
|
156
|
+
[K in keyof T]: T[K] extends Parser<Schema> ? T[K]['schema']['_deferred'] : never;
|
|
157
|
+
};
|
|
158
|
+
declare class TupleSchema<T extends readonly Parser<Schema>[]> implements Schema<TupleOutput<T>, TupleInput<T>, DeferredTuple<TupleOutput<T>, TupleDeferred<T>>> {
|
|
159
|
+
readonly _elements: T;
|
|
160
|
+
readonly _output: TupleOutput<T>;
|
|
161
|
+
readonly _input: TupleInput<T>;
|
|
162
|
+
readonly _deferred: DeferredTuple<TupleOutput<T>, TupleDeferred<T>>;
|
|
163
|
+
constructor(_elements: T);
|
|
164
|
+
serializeSchema(counter: {
|
|
165
|
+
value: number;
|
|
166
|
+
} | null, lazy?: LazyContext): SerializedSchema;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
interface RecordConstraints {
|
|
170
|
+
min?: number;
|
|
171
|
+
max?: number;
|
|
172
|
+
}
|
|
173
|
+
declare class RecordSchema<T extends Parser<Schema>> implements Schema<Record<string, T['schema']['_output']>, Record<string, T['schema']['_input']>, DeferredRecord<T['schema']['_output'], T['schema']['_deferred']>>, Parser<RecordSchema<T>> {
|
|
174
|
+
readonly _valueSchema: T;
|
|
175
|
+
readonly _output: Record<string, T['schema']['_output']>;
|
|
176
|
+
readonly _input: Record<string, T['schema']['_input']>;
|
|
177
|
+
readonly _deferred: DeferredRecord<T['schema']['_output'], T['schema']['_deferred']>;
|
|
178
|
+
private _constraints;
|
|
179
|
+
private _parser?;
|
|
180
|
+
constructor(_valueSchema: T);
|
|
181
|
+
get schema(): RecordSchema<T>;
|
|
182
|
+
get constraints(): RecordConstraints | undefined;
|
|
183
|
+
min(n: number): RecordSchema<T>;
|
|
184
|
+
max(n: number): RecordSchema<T>;
|
|
185
|
+
parse(input: Uint8Array): InferDeferred<RecordSchema<T>>;
|
|
186
|
+
parseLarge(stream: Readable): Promise<LargeParseResult<RecordSchema<T>>>;
|
|
187
|
+
private getParser;
|
|
188
|
+
serializeSchema(counter: {
|
|
189
|
+
value: number;
|
|
190
|
+
} | null, lazy?: LazyContext): SerializedSchema;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
type UnionOutput<T extends readonly Parser<Schema>[]> = T[number]['schema']['_output'];
|
|
194
|
+
type UnionInput<T extends readonly Parser<Schema>[]> = T[number]['schema']['_input'];
|
|
195
|
+
declare class UnionSchema<T extends readonly [Parser<Schema>, ...Parser<Schema>[]]> implements Schema<UnionOutput<T>, UnionInput<T>, DeferredUnion<UnionOutput<T>>> {
|
|
196
|
+
readonly _variants: T;
|
|
197
|
+
readonly _output: UnionOutput<T>;
|
|
198
|
+
readonly _input: UnionInput<T>;
|
|
199
|
+
readonly _deferred: DeferredUnion<UnionOutput<T>>;
|
|
200
|
+
constructor(_variants: T);
|
|
201
|
+
serializeSchema(counter: {
|
|
202
|
+
value: number;
|
|
203
|
+
} | null, lazy?: LazyContext): SerializedSchema;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
interface ArrayConstraints {
|
|
207
|
+
min?: number;
|
|
208
|
+
max?: number;
|
|
209
|
+
}
|
|
210
|
+
declare class ArraySchema<T extends Parser<Schema>> implements Schema<T['schema']['_output'][], T['schema']['_input'][], DeferredArray<T['schema']['_output'], T['schema']['_deferred']>>, Parser<ArraySchema<T>> {
|
|
211
|
+
readonly _elements: T;
|
|
212
|
+
readonly _output: T['schema']['_output'][];
|
|
213
|
+
readonly _input: T['schema']['_input'][];
|
|
214
|
+
readonly _deferred: DeferredArray<T['schema']['_output'], T['schema']['_deferred']>;
|
|
215
|
+
private _constraints;
|
|
216
|
+
private _parser?;
|
|
217
|
+
constructor(_elements: T);
|
|
218
|
+
get schema(): ArraySchema<T>;
|
|
219
|
+
get constraints(): ArrayConstraints | undefined;
|
|
220
|
+
min(n: number): ArraySchema<T>;
|
|
221
|
+
max(n: number): ArraySchema<T>;
|
|
222
|
+
parse(input: Uint8Array): InferDeferred<ArraySchema<T>>;
|
|
223
|
+
parseLarge(stream: Readable): Promise<LargeParseResult<ArraySchema<T>>>;
|
|
224
|
+
/**
|
|
225
|
+
* Parse a JSON array from a stream, yielding each element's deferred wrapper as it is parsed.
|
|
226
|
+
* Yielded deferreds become invalid after the iterator completes.
|
|
227
|
+
*/
|
|
228
|
+
parseEach(stream: Readable): AsyncIterableIterator<T['schema']['_deferred']>;
|
|
229
|
+
private getParser;
|
|
230
|
+
serializeSchema(counter: {
|
|
231
|
+
value: number;
|
|
232
|
+
} | null, lazy?: LazyContext): SerializedSchema;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
declare class LazySchema<T extends Parser<Schema>> implements Schema<T['schema']['_output'], T['schema']['_input'], T['schema']['_deferred']>, Parser<LazySchema<T>> {
|
|
236
|
+
private _thunk;
|
|
237
|
+
readonly _output: T['schema']['_output'];
|
|
238
|
+
readonly _input: T['schema']['_input'];
|
|
239
|
+
readonly _deferred: T['schema']['_deferred'];
|
|
240
|
+
private _resolved?;
|
|
241
|
+
private _parser?;
|
|
242
|
+
private _resolving;
|
|
243
|
+
constructor(_thunk: () => T);
|
|
244
|
+
get schema(): LazySchema<T>;
|
|
245
|
+
parse(input: Uint8Array): InferDeferred<LazySchema<T>>;
|
|
246
|
+
parseLarge(stream: Readable): Promise<LargeParseResult<LazySchema<T>>>;
|
|
247
|
+
private getParser;
|
|
248
|
+
private resolve;
|
|
249
|
+
serializeSchema(counter: {
|
|
250
|
+
value: number;
|
|
251
|
+
} | null, lazy?: LazyContext): SerializedSchema;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Native API implementation - provides tree-shakeable functional API for creating parsers
|
|
256
|
+
*/
|
|
257
|
+
|
|
258
|
+
type RejectOptional<T> = T extends Parser<OptionalSchema<Parser<Schema>>> ? never : T;
|
|
259
|
+
/**
|
|
260
|
+
* Initialize the parser module.
|
|
261
|
+
* With native bindings, initialization is automatic. This function is kept for backwards compatibility.
|
|
262
|
+
*
|
|
263
|
+
* @returns A promise that resolves immediately
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* ```ts
|
|
267
|
+
* import { initialize, object, string, number, toBytes as b } from 'atchara'
|
|
268
|
+
*
|
|
269
|
+
* await initialize()
|
|
270
|
+
*
|
|
271
|
+
* const User = object({
|
|
272
|
+
* name: string(),
|
|
273
|
+
* age: number()
|
|
274
|
+
* })
|
|
275
|
+
*
|
|
276
|
+
* const user = User.parse(b`{"name": "Alice", "age": 30}`)
|
|
277
|
+
* ```
|
|
278
|
+
*/
|
|
279
|
+
declare function initialize(): Promise<void>;
|
|
280
|
+
/**
|
|
281
|
+
* Creates a parser that validates and parses JSON strings.
|
|
282
|
+
*
|
|
283
|
+
* @returns A parser for string values
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* ```ts
|
|
287
|
+
* import { string, toBytes as b } from 'atchara'
|
|
288
|
+
*
|
|
289
|
+
* const parser = string()
|
|
290
|
+
* parser.parse(b`"hello"`) // "hello"
|
|
291
|
+
*
|
|
292
|
+
* // With constraints
|
|
293
|
+
* const bounded = string().min(1).max(255)
|
|
294
|
+
* ```
|
|
295
|
+
*/
|
|
296
|
+
declare function string(): StringSchema;
|
|
297
|
+
/**
|
|
298
|
+
* Creates a parser that validates and parses JSON numbers (integers and floats).
|
|
299
|
+
*
|
|
300
|
+
* @returns A parser for number values
|
|
301
|
+
*
|
|
302
|
+
* @example
|
|
303
|
+
* ```ts
|
|
304
|
+
* import { number, toBytes as b } from 'atchara'
|
|
305
|
+
*
|
|
306
|
+
* const parser = number()
|
|
307
|
+
* parser.parse(b`42`) // 42
|
|
308
|
+
* parser.parse(b`3.14`) // 3.14
|
|
309
|
+
* parser.parse(b`-1.5e10`) // -1.5e10
|
|
310
|
+
*
|
|
311
|
+
* // With constraints
|
|
312
|
+
* const positive = number().min(0)
|
|
313
|
+
* const percentage = number().min(0).max(100)
|
|
314
|
+
* ```
|
|
315
|
+
*/
|
|
316
|
+
declare function number(): NumberSchema;
|
|
317
|
+
/**
|
|
318
|
+
* Creates a parser that validates and parses JSON integers.
|
|
319
|
+
* Unlike `number()`, this rejects floating-point values.
|
|
320
|
+
*
|
|
321
|
+
* @returns A parser for integer values
|
|
322
|
+
*
|
|
323
|
+
* @example
|
|
324
|
+
* ```ts
|
|
325
|
+
* import { integer, toBytes as b } from 'atchara'
|
|
326
|
+
*
|
|
327
|
+
* const parser = integer()
|
|
328
|
+
* parser.parse(b`42`) // 42
|
|
329
|
+
* parser.parse(b`-100`) // -100
|
|
330
|
+
* parser.parse(b`3.14`) // throws - not an integer
|
|
331
|
+
*
|
|
332
|
+
* // With constraints
|
|
333
|
+
* const natural = integer().min(0)
|
|
334
|
+
* ```
|
|
335
|
+
*/
|
|
336
|
+
declare function integer(): IntegerSchema;
|
|
337
|
+
/**
|
|
338
|
+
* Creates a parser that validates and parses JSON booleans.
|
|
339
|
+
*
|
|
340
|
+
* @returns A parser for boolean values
|
|
341
|
+
*
|
|
342
|
+
* @example
|
|
343
|
+
* ```ts
|
|
344
|
+
* import { boolean, toBytes as b } from 'atchara'
|
|
345
|
+
*
|
|
346
|
+
* const parser = boolean()
|
|
347
|
+
* parser.parse(b`true`) // true
|
|
348
|
+
* parser.parse(b`false`) // false
|
|
349
|
+
* ```
|
|
350
|
+
*/
|
|
351
|
+
declare function boolean(): Parser<BooleanSchema>;
|
|
352
|
+
/**
|
|
353
|
+
* Creates a parser that accepts either the wrapped type or `null`.
|
|
354
|
+
*
|
|
355
|
+
* @param valueParser - The parser for the non-null value
|
|
356
|
+
* @returns A parser that accepts the value type or null
|
|
357
|
+
*
|
|
358
|
+
* @example
|
|
359
|
+
* ```ts
|
|
360
|
+
* import { nullable, string, toBytes as b } from 'atchara'
|
|
361
|
+
*
|
|
362
|
+
* const parser = nullable(string())
|
|
363
|
+
* parser.parse(b`"hello"`) // "hello"
|
|
364
|
+
* parser.parse(b`null`) // null
|
|
365
|
+
*
|
|
366
|
+
* // Type is inferred as: string | null
|
|
367
|
+
* ```
|
|
368
|
+
*/
|
|
369
|
+
declare function nullable<T extends Parser<Schema>>(valueParser: T): Parser<NullableSchema<T>>;
|
|
370
|
+
/**
|
|
371
|
+
* Marks an object field as optional, allowing it to be omitted.
|
|
372
|
+
* This should only be used within `object()` shape definitions.
|
|
373
|
+
*
|
|
374
|
+
* @param valueParser - The parser for the field value when present
|
|
375
|
+
* @returns A parser that marks the field as optional
|
|
376
|
+
*
|
|
377
|
+
* @example
|
|
378
|
+
* ```ts
|
|
379
|
+
* import { object, string, number, optional, toBytes as b } from 'atchara'
|
|
380
|
+
*
|
|
381
|
+
* const parser = object({
|
|
382
|
+
* name: string(),
|
|
383
|
+
* age: optional(number())
|
|
384
|
+
* })
|
|
385
|
+
*
|
|
386
|
+
* parser.parse(b`{"name":"Alice"}`) // { name: "Alice", age: undefined }
|
|
387
|
+
* parser.parse(b`{"name":"Bob","age":30}`) // { name: "Bob", age: 30 }
|
|
388
|
+
*
|
|
389
|
+
* // Type is inferred as: { name: string; age?: number }
|
|
390
|
+
* ```
|
|
391
|
+
*/
|
|
392
|
+
declare function optional<T extends Parser<Schema>>(valueParser: T): Parser<OptionalSchema<T>>;
|
|
393
|
+
/**
|
|
394
|
+
* Creates a parser for JSON objects with a defined shape.
|
|
395
|
+
* Each key in the shape corresponds to an expected field in the JSON object.
|
|
396
|
+
*
|
|
397
|
+
* @param shape - An object mapping field names to their parsers
|
|
398
|
+
* @returns A parser for objects matching the shape
|
|
399
|
+
*
|
|
400
|
+
* @example
|
|
401
|
+
* ```ts
|
|
402
|
+
* import { object, number, string, optional, literal, toBytes as b } from 'atchara'
|
|
403
|
+
*
|
|
404
|
+
* const userParser = object({
|
|
405
|
+
* id: number(),
|
|
406
|
+
* name: string(),
|
|
407
|
+
* email: optional(string()),
|
|
408
|
+
* role: literal('admin')
|
|
409
|
+
* })
|
|
410
|
+
*
|
|
411
|
+
* const user = userParser.parse(b`{"id":1,"name":"Alice","role":"admin"}`)
|
|
412
|
+
* // user: { id: number; name: string; email?: string; role: "admin" }
|
|
413
|
+
* ```
|
|
414
|
+
*/
|
|
415
|
+
declare function object<T extends Record<string, Parser<Schema>>>(shape: T): Parser<ObjectSchema<T>>;
|
|
416
|
+
/**
|
|
417
|
+
* Creates a parser for JSON arrays where all elements share the same type.
|
|
418
|
+
*
|
|
419
|
+
* @param elements - The parser for each element in the array
|
|
420
|
+
* @returns A parser for arrays of the element type
|
|
421
|
+
*
|
|
422
|
+
* @example
|
|
423
|
+
* ```ts
|
|
424
|
+
* import { array, number, toBytes as b } from 'atchara'
|
|
425
|
+
*
|
|
426
|
+
* const parser = array(number())
|
|
427
|
+
* parser.parse(b`[1,2,3]`) // [1, 2, 3]
|
|
428
|
+
*
|
|
429
|
+
* // With constraints
|
|
430
|
+
* const bounded = array(number()).min(1).max(10)
|
|
431
|
+
* ```
|
|
432
|
+
*/
|
|
433
|
+
declare function array<T extends Parser<Schema>>(elements: T): ArraySchema<T>;
|
|
434
|
+
/**
|
|
435
|
+
* Creates a parser that only accepts a specific literal value.
|
|
436
|
+
* Useful for discriminated unions, constants, and exact value matching.
|
|
437
|
+
*
|
|
438
|
+
* @param value - The exact value to match (string, number, boolean, or null)
|
|
439
|
+
* @returns A parser that only accepts the literal value
|
|
440
|
+
*
|
|
441
|
+
* @example
|
|
442
|
+
* ```ts
|
|
443
|
+
* import { literal, union, object, number, string, toBytes as b } from 'atchara'
|
|
444
|
+
*
|
|
445
|
+
* const parser = literal('success')
|
|
446
|
+
* parser.parse(b`"success"`) // "success"
|
|
447
|
+
* parser.parse(b`"failure"`) // throws - not the expected literal
|
|
448
|
+
*
|
|
449
|
+
* // Discriminated unions
|
|
450
|
+
* const eventParser = union([
|
|
451
|
+
* object({ type: literal('click'), x: number(), y: number() }),
|
|
452
|
+
* object({ type: literal('keypress'), key: string() })
|
|
453
|
+
* ])
|
|
454
|
+
* ```
|
|
455
|
+
*/
|
|
456
|
+
declare function literal<T extends string | number | boolean | null>(value: T): Parser<LiteralSchema<T>>;
|
|
457
|
+
/**
|
|
458
|
+
* Creates a parser for fixed-length arrays where each position has a specific type.
|
|
459
|
+
* Unlike `array()`, tuples have a known length and can have different types per position.
|
|
460
|
+
*
|
|
461
|
+
* @param elements - An array of parsers, one for each position in the tuple
|
|
462
|
+
* @returns A parser for tuples matching the element types
|
|
463
|
+
*
|
|
464
|
+
* @example
|
|
465
|
+
* ```ts
|
|
466
|
+
* import { tuple, number, string, boolean, toBytes as b } from 'atchara'
|
|
467
|
+
*
|
|
468
|
+
* // Coordinate pair
|
|
469
|
+
* const point = tuple([number(), number()])
|
|
470
|
+
* point.parse(b`[10,20]`) // [10, 20] as [number, number]
|
|
471
|
+
*
|
|
472
|
+
* // Mixed types
|
|
473
|
+
* const entry = tuple([string(), number(), boolean()])
|
|
474
|
+
* entry.parse(b`["key",42,true]`) // ["key", 42, true] as [string, number, boolean]
|
|
475
|
+
*
|
|
476
|
+
* // Empty tuple
|
|
477
|
+
* const empty = tuple([])
|
|
478
|
+
* empty.parse(b`[]`) // [] as []
|
|
479
|
+
* ```
|
|
480
|
+
*/
|
|
481
|
+
declare function tuple<T extends readonly [] | readonly [Parser<Schema>, ...Parser<Schema>[]]>(elements: T): Parser<TupleSchema<T>>;
|
|
482
|
+
/**
|
|
483
|
+
* Creates a parser for objects with string keys and uniform value types.
|
|
484
|
+
* Unlike `object()`, record keys are not known at compile time.
|
|
485
|
+
*
|
|
486
|
+
* @param valueSchema - The parser for each value in the record
|
|
487
|
+
* @returns A parser for records with the value type
|
|
488
|
+
*
|
|
489
|
+
* @example
|
|
490
|
+
* ```ts
|
|
491
|
+
* import { record, number, string, toBytes as b } from 'atchara'
|
|
492
|
+
*
|
|
493
|
+
* // String-to-number mapping
|
|
494
|
+
* const scores = record(number())
|
|
495
|
+
* scores.parse(b`{"alice":100,"bob":85}`)
|
|
496
|
+
* // { alice: 100, bob: 85 } as Record<string, number>
|
|
497
|
+
*
|
|
498
|
+
* // Nested records
|
|
499
|
+
* const config = record(record(string()))
|
|
500
|
+
* config.parse(b`{"db":{"host":"localhost"}}`)
|
|
501
|
+
* ```
|
|
502
|
+
*/
|
|
503
|
+
declare function record<T extends Parser<Schema>>(valueSchema: T): RecordSchema<T>;
|
|
504
|
+
/**
|
|
505
|
+
* Creates a parser that accepts any of the provided variant types.
|
|
506
|
+
* Variants are tried in order until one succeeds.
|
|
507
|
+
*
|
|
508
|
+
* @param variants - An array of parsers representing possible types
|
|
509
|
+
* @returns A parser that accepts any of the variant types
|
|
510
|
+
*
|
|
511
|
+
* @example
|
|
512
|
+
* ```ts
|
|
513
|
+
* import { union, string, number, object, literal, toBytes as b } from 'atchara'
|
|
514
|
+
*
|
|
515
|
+
* // Simple union
|
|
516
|
+
* const stringOrNumber = union([string(), number()])
|
|
517
|
+
* stringOrNumber.parse(b`"hello"`) // "hello"
|
|
518
|
+
* stringOrNumber.parse(b`42`) // 42
|
|
519
|
+
*
|
|
520
|
+
* // Discriminated union (recommended for objects)
|
|
521
|
+
* const shape = union([
|
|
522
|
+
* object({ kind: literal('circle'), radius: number() }),
|
|
523
|
+
* object({ kind: literal('rect'), width: number(), height: number() })
|
|
524
|
+
* ])
|
|
525
|
+
*
|
|
526
|
+
* shape.parse(b`{"kind":"circle","radius":5}`)
|
|
527
|
+
* // { kind: "circle", radius: 5 }
|
|
528
|
+
* ```
|
|
529
|
+
*/
|
|
530
|
+
declare function union<T extends readonly [Parser<Schema>, ...Parser<Schema>[]]>(variants: {
|
|
531
|
+
[K in keyof T]: RejectOptional<T[K]>;
|
|
532
|
+
} & T): Parser<UnionSchema<{ [K in keyof T]: RejectOptional<T[K]>; } & T>>;
|
|
533
|
+
/**
|
|
534
|
+
* Creates a lazily-evaluated schema for recursive data structures.
|
|
535
|
+
* The schema is resolved on first use, enabling self-referential definitions.
|
|
536
|
+
*
|
|
537
|
+
* Provide the output type as a type parameter to enable recursive self-reference
|
|
538
|
+
* without a variable annotation. TypeScript resolves the return type from the
|
|
539
|
+
* type parameter alone, breaking the circular inference.
|
|
540
|
+
*
|
|
541
|
+
* @param fn - A function that returns the parser to defer to
|
|
542
|
+
* @returns A parser wrapping the lazily-resolved schema
|
|
543
|
+
*
|
|
544
|
+
* @example
|
|
545
|
+
* ```ts
|
|
546
|
+
* import { lazy, object, number, array, toBytes as b } from 'atchara'
|
|
547
|
+
*
|
|
548
|
+
* type TreeNode = { value: number; children: TreeNode[] }
|
|
549
|
+
*
|
|
550
|
+
* const Node = lazy<TreeNode>(() =>
|
|
551
|
+
* object({
|
|
552
|
+
* value: number(),
|
|
553
|
+
* children: array(Node),
|
|
554
|
+
* })
|
|
555
|
+
* )
|
|
556
|
+
*
|
|
557
|
+
* Node.parse(b`{"value": 1, "children": []}`)
|
|
558
|
+
* ```
|
|
559
|
+
*/
|
|
560
|
+
declare function lazy<TOutput>(fn: () => any): LazySchema<Parser<Schema<TOutput>>>;
|
|
561
|
+
declare function lazy<T extends Parser<Schema>>(fn: () => T): LazySchema<T>;
|
|
562
|
+
|
|
563
|
+
export { ArraySchema, IntegerSchema, LazySchema, NumberSchema, StringSchema, array, boolean, initialize, integer, lazy, literal, nullable, number, object, optional, record, string, tuple, union };
|