decoders 2.3.0-test4 → 2.4.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 +1 -1
- package/dist/index.cjs +387 -322
- package/dist/index.d.cts +267 -183
- package/dist/index.d.ts +267 -183
- package/dist/index.js +390 -325
- package/package.json +10 -9
- package/dist/index.cjs.map +0 -1
- package/dist/index.js.map +0 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
interface ObjectAnnotation {
|
|
2
2
|
readonly type: 'object';
|
|
3
|
-
readonly fields:
|
|
4
|
-
readonly [key: string]: Annotation;
|
|
5
|
-
};
|
|
3
|
+
readonly fields: ReadonlyMap<string, Annotation>;
|
|
6
4
|
readonly text?: string;
|
|
7
5
|
}
|
|
8
6
|
interface ArrayAnnotation {
|
|
@@ -55,9 +53,14 @@ declare function ok<T>(value: T): Ok<T>;
|
|
|
55
53
|
*/
|
|
56
54
|
declare function err<E>(error: E): Err<E>;
|
|
57
55
|
|
|
58
|
-
type Scalar = string | number | boolean | symbol | undefined | null;
|
|
59
56
|
type DecodeResult<T> = Result<T, Annotation>;
|
|
60
|
-
|
|
57
|
+
/**
|
|
58
|
+
* A function taking a untrusted input, and returning a DecodeResult<T>. The
|
|
59
|
+
* `ok()` and `err()` constructor functions are provided as the 2nd and 3rd
|
|
60
|
+
* param. One of these should be called and its value returned.
|
|
61
|
+
*/
|
|
62
|
+
type AcceptanceFn<O, I = unknown> = (blob: I, ok: (value: O) => DecodeResult<O>, err: (msg: string | Annotation) => DecodeResult<O>) => DecodeResult<O>;
|
|
63
|
+
type Next<O, I = unknown> = Decoder<O> | ((blob: I, ok: (value: O) => DecodeResult<O>, err: (msg: string | Annotation) => DecodeResult<O>) => DecodeResult<O> | Decoder<O>);
|
|
61
64
|
interface Decoder<T> {
|
|
62
65
|
/**
|
|
63
66
|
* Verifies untrusted input. Either returns a value, or throws a decoding
|
|
@@ -94,24 +97,45 @@ interface Decoder<T> {
|
|
|
94
97
|
*/
|
|
95
98
|
describe(message: string): Decoder<T>;
|
|
96
99
|
/**
|
|
97
|
-
*
|
|
100
|
+
* Send the output of the current decoder into another decoder or acceptance
|
|
101
|
+
* function. The given acceptance function will receive the output of the
|
|
102
|
+
* current decoder as its input.
|
|
103
|
+
*
|
|
104
|
+
* > _**NOTE:** This is an advanced, low-level, API. It's not recommended
|
|
105
|
+
* > to reach for this construct unless there is no other way. Most cases can
|
|
106
|
+
* > be covered more elegantly by `.transform()`, `.refine()`, or `.pipe()`
|
|
107
|
+
* > instead._
|
|
108
|
+
*/
|
|
109
|
+
then<V>(next: Next<V, T>): Decoder<V>;
|
|
110
|
+
/**
|
|
111
|
+
* Send the output of this decoder as input to another decoder.
|
|
112
|
+
*
|
|
113
|
+
* This can be useful to validate the results of a transform, i.e.:
|
|
114
|
+
*
|
|
115
|
+
* string
|
|
116
|
+
* .transform((s) => s.split(','))
|
|
117
|
+
* .pipe(array(nonEmptyString))
|
|
118
|
+
*
|
|
119
|
+
* You can also conditionally pipe:
|
|
120
|
+
*
|
|
121
|
+
* string.pipe((s) => s.startsWith('@') ? username : email)
|
|
98
122
|
*/
|
|
99
|
-
|
|
100
|
-
|
|
123
|
+
pipe<V, D extends Decoder<V>>(next: D): Decoder<DecoderType<D>>;
|
|
124
|
+
pipe<V, D extends Decoder<V>>(next: (blob: T) => D): Decoder<DecoderType<D>>;
|
|
101
125
|
}
|
|
102
126
|
/**
|
|
103
|
-
* Helper type to return the
|
|
127
|
+
* Helper type to return the output type of a Decoder.
|
|
128
|
+
* It’s the inverse of Decoder<T>.
|
|
104
129
|
*
|
|
105
|
-
* You can use it
|
|
130
|
+
* You can use it at the type level:
|
|
106
131
|
*
|
|
107
132
|
* DecoderType<Decoder<string>> // string
|
|
108
133
|
* DecoderType<Decoder<number[]>> // number[]
|
|
109
134
|
*
|
|
110
|
-
* Or on
|
|
135
|
+
* Or on decoder instances, by using the `typeof` keyword:
|
|
111
136
|
*
|
|
112
137
|
* DecoderType<typeof string> // string
|
|
113
138
|
* DecoderType<typeof truthy> // boolean
|
|
114
|
-
*
|
|
115
139
|
*/
|
|
116
140
|
type DecoderType<D extends Decoder<any>> = D extends Decoder<infer T> ? T : never;
|
|
117
141
|
/**
|
|
@@ -129,36 +153,34 @@ type DecoderType<D extends Decoder<any>> = D extends Decoder<infer T> ? T : neve
|
|
|
129
153
|
*/
|
|
130
154
|
declare function define<T>(fn: AcceptanceFn<T>): Decoder<T>;
|
|
131
155
|
|
|
132
|
-
type
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
type JSONArray = JSONValue[];
|
|
156
|
+
type Formatter = (err: Annotation) => string | Error;
|
|
157
|
+
declare function formatInline(ann: Annotation): string;
|
|
158
|
+
declare function formatShort(ann: Annotation): string;
|
|
159
|
+
|
|
137
160
|
/**
|
|
138
|
-
* Accepts
|
|
161
|
+
* Accepts any array, but doesn't validate its items further.
|
|
162
|
+
*
|
|
163
|
+
* "poja" means "plain old JavaScript array", a play on `pojo()`.
|
|
139
164
|
*/
|
|
140
|
-
declare const
|
|
165
|
+
declare const poja: Decoder<unknown[]>;
|
|
141
166
|
/**
|
|
142
|
-
* Accepts arrays
|
|
167
|
+
* Accepts arrays of whatever the given decoder accepts.
|
|
143
168
|
*/
|
|
144
|
-
declare
|
|
169
|
+
declare function array<T>(decoder: Decoder<T>): Decoder<T[]>;
|
|
145
170
|
/**
|
|
146
|
-
*
|
|
147
|
-
*
|
|
148
|
-
* In other words: any value returned by `JSON.parse()` should decode without
|
|
149
|
-
* failure.
|
|
150
|
-
*
|
|
151
|
-
* ```typescript
|
|
152
|
-
* type JSONValue =
|
|
153
|
-
* | null
|
|
154
|
-
* | string
|
|
155
|
-
* | number
|
|
156
|
-
* | boolean
|
|
157
|
-
* | { [string]: JSONValue }
|
|
158
|
-
* | JSONValue[]
|
|
159
|
-
* ```
|
|
171
|
+
* Like `array()`, but will reject arrays with 0 elements.
|
|
160
172
|
*/
|
|
161
|
-
declare
|
|
173
|
+
declare function nonEmptyArray<T>(decoder: Decoder<T>): Decoder<[T, ...T[]]>;
|
|
174
|
+
type TupleDecoderType<Ds extends readonly Decoder<unknown>[]> = {
|
|
175
|
+
[K in keyof Ds]: DecoderType<Ds[K]>;
|
|
176
|
+
};
|
|
177
|
+
/**
|
|
178
|
+
* Accepts a tuple (an array with exactly _n_ items) of values accepted by the
|
|
179
|
+
* _n_ given decoders.
|
|
180
|
+
*/
|
|
181
|
+
declare function tuple<Ds extends readonly [first: Decoder<unknown>, ...rest: readonly Decoder<unknown>[]]>(...decoders: Ds): Decoder<TupleDecoderType<Ds>>;
|
|
182
|
+
|
|
183
|
+
type Scalar = string | number | boolean | symbol | undefined | null;
|
|
162
184
|
|
|
163
185
|
/**
|
|
164
186
|
* Accepts and returns only the literal `null` value.
|
|
@@ -187,8 +209,9 @@ declare function nullable<T>(decoder: Decoder<T>): Decoder<T | null>;
|
|
|
187
209
|
declare function nullable<T, C extends Scalar>(decoder: Decoder<T>, defaultValue: (() => C) | C): Decoder<NonNullable<T> | C>;
|
|
188
210
|
declare function nullable<T, V>(decoder: Decoder<T>, defaultValue: (() => V) | V): Decoder<NonNullable<T> | V>;
|
|
189
211
|
/**
|
|
190
|
-
* @deprecated
|
|
191
|
-
*
|
|
212
|
+
* @deprecated Will get removed in a future version.
|
|
213
|
+
*
|
|
214
|
+
* Alias of `nullish()`.
|
|
192
215
|
*/
|
|
193
216
|
declare const maybe: typeof nullish;
|
|
194
217
|
/**
|
|
@@ -218,12 +241,13 @@ declare function always<T>(value: (() => T) | T): Decoder<T>;
|
|
|
218
241
|
*/
|
|
219
242
|
declare function never(msg: string): Decoder<never>;
|
|
220
243
|
/**
|
|
221
|
-
* Alias of never()
|
|
244
|
+
* Alias of `never()`.
|
|
222
245
|
*/
|
|
223
246
|
declare const fail: typeof never;
|
|
224
247
|
/**
|
|
225
|
-
*
|
|
226
|
-
*
|
|
248
|
+
* Alias of `always()`.
|
|
249
|
+
*
|
|
250
|
+
* @deprecated Will get removed in a future version.
|
|
227
251
|
*/
|
|
228
252
|
declare const hardcoded: typeof always;
|
|
229
253
|
/**
|
|
@@ -235,53 +259,56 @@ declare const hardcoded: typeof always;
|
|
|
235
259
|
*/
|
|
236
260
|
declare const unknown: Decoder<unknown>;
|
|
237
261
|
/**
|
|
238
|
-
*
|
|
239
|
-
*
|
|
262
|
+
* Alias of `unknown`.
|
|
263
|
+
*
|
|
264
|
+
* @deprecated Will get removed in a future version.
|
|
240
265
|
*/
|
|
241
266
|
declare const mixed: Decoder<unknown>;
|
|
242
267
|
|
|
243
268
|
/**
|
|
244
|
-
* Accepts
|
|
245
|
-
*
|
|
246
|
-
* "poja" means "plain old JavaScript array", a play on `pojo()`.
|
|
269
|
+
* Accepts and returns booleans.
|
|
247
270
|
*/
|
|
248
|
-
declare const
|
|
271
|
+
declare const boolean: Decoder<boolean>;
|
|
249
272
|
/**
|
|
250
|
-
* Accepts
|
|
273
|
+
* Accepts anything and will return its "truth" value. Will never reject.
|
|
251
274
|
*/
|
|
252
|
-
declare
|
|
275
|
+
declare const truthy: Decoder<boolean>;
|
|
276
|
+
|
|
253
277
|
/**
|
|
254
|
-
*
|
|
278
|
+
* Accepts objects where all values match the given decoder, and returns the
|
|
279
|
+
* result as a `Record<string, V>`.
|
|
255
280
|
*/
|
|
256
|
-
declare function
|
|
281
|
+
declare function record<V>(valueDecoder: Decoder<V>): Decoder<Record<string, V>>;
|
|
257
282
|
/**
|
|
258
|
-
*
|
|
259
|
-
*
|
|
283
|
+
* Accepts objects where all keys and values match the given decoders, and
|
|
284
|
+
* returns the result as a `Record<K, V>`. The given key decoder must return
|
|
285
|
+
* strings.
|
|
260
286
|
*/
|
|
261
|
-
declare function
|
|
287
|
+
declare function record<K extends string, V>(keyDecoder: Decoder<K>, valueDecoder: Decoder<V>): Decoder<Record<K, V>>;
|
|
262
288
|
/**
|
|
263
|
-
*
|
|
264
|
-
*
|
|
289
|
+
* @deprecated Will get removed in a future version.
|
|
290
|
+
*
|
|
291
|
+
* Alias of `record()`.
|
|
265
292
|
*/
|
|
266
|
-
declare
|
|
267
|
-
declare function tuple<A, B>(a: Decoder<A>, b: Decoder<B>): Decoder<[A, B]>;
|
|
268
|
-
declare function tuple<A, B, C>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>): Decoder<[A, B, C]>;
|
|
269
|
-
declare function tuple<A, B, C, D>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>): Decoder<[A, B, C, D]>;
|
|
270
|
-
declare function tuple<A, B, C, D, E>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>, e: Decoder<E>): Decoder<[A, B, C, D, E]>;
|
|
271
|
-
declare function tuple<A, B, C, D, E, F>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>, e: Decoder<E>, f: Decoder<F>): Decoder<[A, B, C, D, E, F]>;
|
|
272
|
-
|
|
293
|
+
declare const dict: typeof record;
|
|
273
294
|
/**
|
|
274
|
-
*
|
|
295
|
+
* Similar to `array()`, but returns the result as an [ES6
|
|
296
|
+
* Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set).
|
|
275
297
|
*/
|
|
276
|
-
declare
|
|
298
|
+
declare function setFromArray<T>(decoder: Decoder<T>): Decoder<Set<T>>;
|
|
277
299
|
/**
|
|
278
|
-
*
|
|
300
|
+
* Renamed to `setFromArray` to make room for a future `set()` decoder that
|
|
301
|
+
* works differently.
|
|
302
|
+
*
|
|
303
|
+
* @deprecated This decoder will change behavior in a future version.
|
|
279
304
|
*/
|
|
280
|
-
declare const
|
|
305
|
+
declare const set: typeof setFromArray;
|
|
281
306
|
/**
|
|
282
|
-
*
|
|
307
|
+
* Similar to `record()`, but returns the result as a `Map<string, T>` (an [ES6
|
|
308
|
+
* Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map))
|
|
309
|
+
* instead.
|
|
283
310
|
*/
|
|
284
|
-
declare
|
|
311
|
+
declare function mapping<T>(decoder: Decoder<T>): Decoder<Map<string, T>>;
|
|
285
312
|
|
|
286
313
|
/**
|
|
287
314
|
* Accepts and returns `Date` instances.
|
|
@@ -295,10 +322,107 @@ declare const date: Decoder<Date>;
|
|
|
295
322
|
* `.toISOString()` when sending, decode them with `iso8601` when receiving.
|
|
296
323
|
*/
|
|
297
324
|
declare const iso8601: Decoder<Date>;
|
|
325
|
+
/**
|
|
326
|
+
* Accepts either a Date, or an ISO date string, returns a Date instance.
|
|
327
|
+
* This is commonly useful to build decoders that can be reused to validate
|
|
328
|
+
* object with Date instances as well as objects coming from JSON payloads.
|
|
329
|
+
*/
|
|
330
|
+
declare const datelike: Decoder<Date>;
|
|
331
|
+
|
|
332
|
+
type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
|
|
333
|
+
type JSONObject = {
|
|
334
|
+
[key: string]: JSONValue | undefined;
|
|
335
|
+
};
|
|
336
|
+
type JSONArray = JSONValue[];
|
|
337
|
+
/**
|
|
338
|
+
* Accepts objects that contain only valid JSON values.
|
|
339
|
+
*/
|
|
340
|
+
declare const jsonObject: Decoder<JSONObject>;
|
|
341
|
+
/**
|
|
342
|
+
* Accepts arrays that contain only valid JSON values.
|
|
343
|
+
*/
|
|
344
|
+
declare const jsonArray: Decoder<JSONArray>;
|
|
345
|
+
/**
|
|
346
|
+
* Accepts any value that's a valid JSON value.
|
|
347
|
+
*
|
|
348
|
+
* In other words: any value returned by `JSON.parse()` should decode without
|
|
349
|
+
* failure.
|
|
350
|
+
*
|
|
351
|
+
* ```typescript
|
|
352
|
+
* type JSONValue =
|
|
353
|
+
* | null
|
|
354
|
+
* | string
|
|
355
|
+
* | number
|
|
356
|
+
* | boolean
|
|
357
|
+
* | { [string]: JSONValue }
|
|
358
|
+
* | JSONValue[]
|
|
359
|
+
* ```
|
|
360
|
+
*/
|
|
361
|
+
declare const json: Decoder<JSONValue>;
|
|
362
|
+
|
|
363
|
+
type SizeOptions = {
|
|
364
|
+
min?: number;
|
|
365
|
+
max?: number;
|
|
366
|
+
size?: number;
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
interface Klass<T> extends Function {
|
|
370
|
+
new (...args: readonly any[]): T;
|
|
371
|
+
}
|
|
372
|
+
type Instance<K> = K extends Klass<infer T> ? T : never;
|
|
373
|
+
/**
|
|
374
|
+
* Accepts any value that is an ``instanceof`` the given class.
|
|
375
|
+
*/
|
|
376
|
+
declare function instanceOf<K extends Klass<any>>(klass: K): Decoder<Instance<K>>;
|
|
377
|
+
/**
|
|
378
|
+
* Lazily evaluate the given decoder. This is useful to build self-referential
|
|
379
|
+
* types for recursive data structures.
|
|
380
|
+
*/
|
|
381
|
+
declare function lazy<T>(decoderFn: () => Decoder<T>): Decoder<T>;
|
|
382
|
+
/**
|
|
383
|
+
* Pre-process the data input before passing it into the decoder. This gives
|
|
384
|
+
* you the ability to arbitrarily customize the input on the fly before passing
|
|
385
|
+
* it to the decoder. Of course, the input value at that point is still of
|
|
386
|
+
* ``unknown`` type, so you will have to deal with that accordingly.
|
|
387
|
+
*/
|
|
388
|
+
declare function prep<T>(mapperFn: (blob: unknown) => unknown, decoder: Decoder<T>): Decoder<T>;
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* Accepts any valid ``number`` value.
|
|
392
|
+
*
|
|
393
|
+
* This also accepts special values like `NaN` and `Infinity`. Unless you
|
|
394
|
+
* want to deliberately accept those, you'll likely want to use the
|
|
395
|
+
* `number` decoder instead.
|
|
396
|
+
*/
|
|
397
|
+
declare const anyNumber: Decoder<number>;
|
|
398
|
+
/**
|
|
399
|
+
* Accepts finite numbers (can be integer or float values). Values `NaN`,
|
|
400
|
+
* or positive and negative `Infinity` will get rejected.
|
|
401
|
+
*/
|
|
402
|
+
declare const number: Decoder<number>;
|
|
403
|
+
/**
|
|
404
|
+
* Accepts only finite whole numbers.
|
|
405
|
+
*/
|
|
406
|
+
declare const integer: Decoder<number>;
|
|
407
|
+
/**
|
|
408
|
+
* Accepts only non-negative (zero or positive) finite numbers.
|
|
409
|
+
*/
|
|
410
|
+
declare const positiveNumber: Decoder<number>;
|
|
411
|
+
/**
|
|
412
|
+
* Accepts only non-negative (zero or positive) finite whole numbers.
|
|
413
|
+
*/
|
|
414
|
+
declare const positiveInteger: Decoder<number>;
|
|
415
|
+
/**
|
|
416
|
+
* Accepts any valid ``bigint`` value.
|
|
417
|
+
*/
|
|
418
|
+
declare const bigint: Decoder<bigint>;
|
|
298
419
|
|
|
299
420
|
type RequiredKeys<T extends object> = {
|
|
300
421
|
[K in keyof T]: undefined extends T[K] ? never : K;
|
|
301
422
|
}[keyof T];
|
|
423
|
+
type Resolve<T> = T extends (...args: readonly unknown[]) => unknown ? T : {
|
|
424
|
+
[K in keyof T]: T[K];
|
|
425
|
+
};
|
|
302
426
|
/**
|
|
303
427
|
* Transforms an object type, by marking all fields that contain "undefined"
|
|
304
428
|
* with a question mark, i.e. allowing implicit-undefineds when
|
|
@@ -321,12 +445,8 @@ type RequiredKeys<T extends object> = {
|
|
|
321
445
|
* }
|
|
322
446
|
*/
|
|
323
447
|
type UndefinedToOptional<T extends object> = Resolve<Pick<Required<T>, RequiredKeys<T>> & Partial<T>>;
|
|
324
|
-
type
|
|
325
|
-
[K in keyof
|
|
326
|
-
};
|
|
327
|
-
|
|
328
|
-
type ObjectDecoderType<T> = UndefinedToOptional<{
|
|
329
|
-
[K in keyof T]: T[K] extends Decoder<infer V> ? V : never;
|
|
448
|
+
type ObjectDecoderType<Ds extends Record<string, Decoder<unknown>>> = UndefinedToOptional<{
|
|
449
|
+
[K in keyof Ds]: DecoderType<Ds[K]>;
|
|
330
450
|
}>;
|
|
331
451
|
/**
|
|
332
452
|
* Accepts any "plain old JavaScript object", but doesn't validate its keys or
|
|
@@ -337,88 +457,20 @@ declare const pojo: Decoder<Record<string, unknown>>;
|
|
|
337
457
|
* Accepts objects with fields matching the given decoders. Extra fields that
|
|
338
458
|
* exist on the input object are ignored and will not be returned.
|
|
339
459
|
*/
|
|
340
|
-
declare function object(
|
|
341
|
-
declare function object<
|
|
460
|
+
declare function object(decoders: Record<any, never>): Decoder<Record<string, never>>;
|
|
461
|
+
declare function object<Ds extends Record<string, Decoder<unknown>>>(decoders: Ds): Decoder<ObjectDecoderType<Ds>>;
|
|
342
462
|
/**
|
|
343
463
|
* Like `object()`, but will reject inputs that contain extra fields that are
|
|
344
464
|
* not specified explicitly.
|
|
345
465
|
*/
|
|
346
|
-
declare function exact(
|
|
347
|
-
declare function exact<
|
|
348
|
-
[K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K];
|
|
349
|
-
}>;
|
|
466
|
+
declare function exact(decoders: Record<any, never>): Decoder<Record<string, never>>;
|
|
467
|
+
declare function exact<Ds extends Record<string, Decoder<unknown>>>(decoders: Ds): Decoder<ObjectDecoderType<Ds>>;
|
|
350
468
|
/**
|
|
351
469
|
* Like `object()`, but will pass through any extra fields on the input object
|
|
352
470
|
* unvalidated that will thus be of `unknown` type statically.
|
|
353
471
|
*/
|
|
354
|
-
declare function inexact(
|
|
355
|
-
declare function inexact<
|
|
356
|
-
[K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K];
|
|
357
|
-
} & Record<string, unknown>>;
|
|
358
|
-
/**
|
|
359
|
-
* Accepts objects where all values match the given decoder, and returns the
|
|
360
|
-
* result as a `Record<string, T>`.
|
|
361
|
-
*
|
|
362
|
-
* The main difference between `object()` and `dict()` is that you'd typically
|
|
363
|
-
* use `object()` if this is a record-like object, where all field names are
|
|
364
|
-
* known and the values are heterogeneous. Whereas with `dict()` the keys are
|
|
365
|
-
* typically dynamic and the values homogeneous, like in a dictionary,
|
|
366
|
-
* a lookup table, or a cache.
|
|
367
|
-
*/
|
|
368
|
-
declare function dict<T>(decoder: Decoder<T>): Decoder<Record<string, T>>;
|
|
369
|
-
/**
|
|
370
|
-
* Similar to `dict()`, but returns the result as a `Map<string, T>` (an [ES6
|
|
371
|
-
* Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map))
|
|
372
|
-
* instead.
|
|
373
|
-
*/
|
|
374
|
-
declare function mapping<T>(decoder: Decoder<T>): Decoder<Map<string, T>>;
|
|
375
|
-
|
|
376
|
-
type Values<T extends object> = T[keyof T];
|
|
377
|
-
type DecoderTypes<T> = T extends readonly Decoder<infer U>[] ? U : never;
|
|
378
|
-
/**
|
|
379
|
-
* Accepts values accepted by any of the given decoders.
|
|
380
|
-
*
|
|
381
|
-
* The decoders are tried on the input one by one, in the given order. The
|
|
382
|
-
* first one that accepts the input "wins". If all decoders reject the input,
|
|
383
|
-
* the input gets rejected.
|
|
384
|
-
*/
|
|
385
|
-
declare function either<T extends readonly Decoder<any>[]>(...decoders: T): Decoder<DecoderTypes<T>>;
|
|
386
|
-
/**
|
|
387
|
-
* Accepts any value that is strictly-equal (using `===`) to one of the
|
|
388
|
-
* specified values.
|
|
389
|
-
*/
|
|
390
|
-
declare function oneOf<C extends Scalar>(constants: readonly C[]): Decoder<C>;
|
|
391
|
-
/**
|
|
392
|
-
* If you are decoding tagged unions you may want to use the `taggedUnion()`
|
|
393
|
-
* decoder instead of the general purpose `either()` decoder to get better
|
|
394
|
-
* error messages and better performance.
|
|
395
|
-
*
|
|
396
|
-
* This decoder is optimized for [tagged
|
|
397
|
-
* unions](https://en.wikipedia.org/wiki/Tagged_union), i.e. a union of
|
|
398
|
-
* objects where one field is used as the discriminator.
|
|
399
|
-
*
|
|
400
|
-
* ```ts
|
|
401
|
-
* const A = object({ tag: constant('A'), foo: string });
|
|
402
|
-
* const B = object({ tag: constant('B'), bar: number });
|
|
403
|
-
*
|
|
404
|
-
* const AorB = taggedUnion('tag', { A, B });
|
|
405
|
-
* // ^^^
|
|
406
|
-
* ```
|
|
407
|
-
*
|
|
408
|
-
* Decoding now works in two steps:
|
|
409
|
-
*
|
|
410
|
-
* 1. Look at the `'tag'` field in the incoming object (this is the field
|
|
411
|
-
* that decides which decoder will be used)
|
|
412
|
-
* 2. If the value is `'A'`, then decoder `A` will be used. If it's `'B'`, then
|
|
413
|
-
* decoder `B` will be used. Otherwise, this will fail.
|
|
414
|
-
*
|
|
415
|
-
* This is effectively equivalent to `either(A, B)`, but will provide better
|
|
416
|
-
* error messages and is more performant at runtime because it doesn't have to
|
|
417
|
-
* try all decoders one by one.
|
|
418
|
-
*/
|
|
419
|
-
declare function taggedUnion<O extends Record<string, Decoder<any>>>(field: string, mapping: O): Decoder<Values<{
|
|
420
|
-
[key in keyof O]: DecoderType<O[key]>;
|
|
421
|
-
}>>;
|
|
472
|
+
declare function inexact(decoders: Record<any, never>): Decoder<Record<string, unknown>>;
|
|
473
|
+
declare function inexact<Ds extends Record<string, Decoder<unknown>>>(decoders: Ds): Decoder<ObjectDecoderType<Ds> & Record<string, unknown>>;
|
|
422
474
|
|
|
423
475
|
/**
|
|
424
476
|
* Accepts and returns strings.
|
|
@@ -446,6 +498,17 @@ declare const url: Decoder<URL>;
|
|
|
446
498
|
* as a URL instance.
|
|
447
499
|
*/
|
|
448
500
|
declare const httpsUrl: Decoder<URL>;
|
|
501
|
+
/**
|
|
502
|
+
* Accepts and returns strings that are valid identifiers in most programming
|
|
503
|
+
* languages.
|
|
504
|
+
*/
|
|
505
|
+
declare const identifier: Decoder<string>;
|
|
506
|
+
/**
|
|
507
|
+
* Accepts and returns [nanoid](https://zelark.github.io/nano-id-cc) string
|
|
508
|
+
* values. It assumes the default nanoid alphabet. If you're using a custom
|
|
509
|
+
* alphabet, use `regex()` instead.
|
|
510
|
+
*/
|
|
511
|
+
declare function nanoid(options?: SizeOptions): Decoder<string>;
|
|
449
512
|
/**
|
|
450
513
|
* Accepts strings that are valid
|
|
451
514
|
* [UUIDs](https://en.wikipedia.org/wiki/universally_unique_identifier)
|
|
@@ -464,56 +527,77 @@ declare const uuidv1: Decoder<string>;
|
|
|
464
527
|
* strings.
|
|
465
528
|
*/
|
|
466
529
|
declare const uuidv4: Decoder<string>;
|
|
467
|
-
|
|
468
|
-
interface Klass<T> extends Function {
|
|
469
|
-
new (...args: readonly any[]): T;
|
|
470
|
-
}
|
|
471
|
-
type Instance<K> = K extends Klass<infer T> ? T : never;
|
|
472
530
|
/**
|
|
473
|
-
* Accepts
|
|
531
|
+
* Accepts and returns strings with decimal digits only (base-10).
|
|
532
|
+
* To convert these to numbers, use the `numeric` decoder.
|
|
474
533
|
*/
|
|
475
|
-
declare
|
|
534
|
+
declare const decimal: Decoder<string>;
|
|
476
535
|
/**
|
|
477
|
-
*
|
|
478
|
-
* types for recursive data structures.
|
|
536
|
+
* Accepts and returns strings with hexadecimal digits only (base-16).
|
|
479
537
|
*/
|
|
480
|
-
declare
|
|
538
|
+
declare const hexadecimal: Decoder<string>;
|
|
481
539
|
/**
|
|
482
|
-
*
|
|
483
|
-
*
|
|
484
|
-
*
|
|
485
|
-
* ``unknown`` type, so you will have to deal with that accordingly.
|
|
540
|
+
* Accepts valid numerical strings (in base-10) and returns them as a number.
|
|
541
|
+
* To only accept numerical strings and keep them as string values, use the
|
|
542
|
+
* `decimal` decoder.
|
|
486
543
|
*/
|
|
487
|
-
declare
|
|
544
|
+
declare const numeric: Decoder<number>;
|
|
488
545
|
|
|
489
546
|
/**
|
|
490
|
-
* Accepts any
|
|
547
|
+
* Accepts values accepted by any of the given decoders.
|
|
491
548
|
*
|
|
492
|
-
*
|
|
493
|
-
*
|
|
494
|
-
*
|
|
549
|
+
* The decoders are tried on the input one by one, in the given order. The
|
|
550
|
+
* first one that accepts the input "wins". If all decoders reject the input,
|
|
551
|
+
* the input gets rejected.
|
|
495
552
|
*/
|
|
496
|
-
declare
|
|
553
|
+
declare function either<TDecoders extends readonly Decoder<unknown>[]>(...decoders: TDecoders): Decoder<DecoderType<TDecoders[number]>>;
|
|
497
554
|
/**
|
|
498
|
-
* Accepts
|
|
499
|
-
*
|
|
555
|
+
* Accepts any value that is strictly-equal (using `===`) to one of the
|
|
556
|
+
* specified values.
|
|
500
557
|
*/
|
|
501
|
-
declare
|
|
558
|
+
declare function oneOf<C extends Scalar>(constants: readonly C[]): Decoder<C>;
|
|
502
559
|
/**
|
|
503
|
-
* Accepts
|
|
560
|
+
* Accepts and return an enum value.
|
|
504
561
|
*/
|
|
505
|
-
declare
|
|
562
|
+
declare function enum_<TEnum extends Record<string, string | number>>(enumObj: TEnum): Decoder<TEnum[keyof TEnum]>;
|
|
506
563
|
/**
|
|
507
|
-
*
|
|
564
|
+
* If you are decoding tagged unions you may want to use the `taggedUnion()`
|
|
565
|
+
* decoder instead of the general purpose `either()` decoder to get better
|
|
566
|
+
* error messages and better performance.
|
|
567
|
+
*
|
|
568
|
+
* This decoder is optimized for [tagged
|
|
569
|
+
* unions](https://en.wikipedia.org/wiki/Tagged_union), i.e. a union of
|
|
570
|
+
* objects where one field is used as the discriminator.
|
|
571
|
+
*
|
|
572
|
+
* ```ts
|
|
573
|
+
* const A = object({ tag: constant('A'), foo: string });
|
|
574
|
+
* const B = object({ tag: constant('B'), bar: number });
|
|
575
|
+
*
|
|
576
|
+
* const AorB = taggedUnion('tag', { A, B });
|
|
577
|
+
* // ^^^
|
|
578
|
+
* ```
|
|
579
|
+
*
|
|
580
|
+
* Decoding now works in two steps:
|
|
581
|
+
*
|
|
582
|
+
* 1. Look at the `'tag'` field in the incoming object (this is the field
|
|
583
|
+
* that decides which decoder will be used)
|
|
584
|
+
* 2. If the value is `'A'`, then decoder `A` will be used. If it's `'B'`, then
|
|
585
|
+
* decoder `B` will be used. Otherwise, this will fail.
|
|
586
|
+
*
|
|
587
|
+
* This is effectively equivalent to `either(A, B)`, but will provide better
|
|
588
|
+
* error messages and is more performant at runtime because it doesn't have to
|
|
589
|
+
* try all decoders one by one.
|
|
508
590
|
*/
|
|
509
|
-
declare
|
|
591
|
+
declare function taggedUnion<O extends Record<string, Decoder<unknown>>, T = DecoderType<O[keyof O]>>(field: string, mapping: O): Decoder<T>;
|
|
510
592
|
/**
|
|
511
|
-
*
|
|
593
|
+
* Briefly peek at a runtime input using a "scout" decoder first, then decide
|
|
594
|
+
* which decoder to run on the (original) input, based on the information that
|
|
595
|
+
* the "scout" extracted.
|
|
596
|
+
*
|
|
597
|
+
* It serves a similar purpose as `taggedUnion()`, but is a generalization that
|
|
598
|
+
* works even if there isn't a single discriminator, or the discriminator isn't
|
|
599
|
+
* a string.
|
|
512
600
|
*/
|
|
513
|
-
declare
|
|
514
|
-
|
|
515
|
-
type Formatter = (err: Annotation) => string | Error;
|
|
516
|
-
declare function formatInline(ann: Annotation): string;
|
|
517
|
-
declare function formatShort(ann: Annotation): string;
|
|
601
|
+
declare function select<T, D extends Decoder<unknown>>(scout: Decoder<T>, selectFn: (result: T) => D): Decoder<DecoderType<D>>;
|
|
518
602
|
|
|
519
|
-
export { type DecodeResult, type Decoder, type DecoderType, type Err, type Formatter, type JSONArray, type JSONObject, type JSONValue, type Ok, type Result, type Scalar, always, anyNumber, array, boolean, constant, date, define, dict, either, email, err, exact, fail, formatInline, formatShort, hardcoded, httpsUrl, inexact, instanceOf, integer, iso8601, json, jsonArray, jsonObject, lazy, mapping, maybe, mixed, never, nonEmptyArray, nonEmptyString, null_, nullable, nullish, number,
|
|
603
|
+
export { type DecodeResult, type Decoder, type DecoderType, type Err, type Formatter, type JSONArray, type JSONObject, type JSONValue, type Ok, type Result, type Scalar, type SizeOptions, always, anyNumber, array, bigint, boolean, constant, date, datelike, decimal, define, dict, either, email, enum_, err, exact, fail, formatInline, formatShort, hardcoded, hexadecimal, httpsUrl, identifier, inexact, instanceOf, integer, iso8601, json, jsonArray, jsonObject, lazy, mapping, maybe, mixed, nanoid, never, nonEmptyArray, nonEmptyString, null_, nullable, nullish, number, numeric, object, ok, oneOf, optional, poja, pojo, positiveInteger, positiveNumber, prep, record, regex, select, set, setFromArray, string, taggedUnion, truthy, tuple, undefined_, unknown, url, uuid, uuidv1, uuidv4 };
|