decoders 2.0.5 → 2.2.0-test
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 +12 -12
- package/dist/annotate-0PUmWHxH.d.cts +33 -0
- package/dist/annotate-0PUmWHxH.d.ts +33 -0
- package/dist/chunk-2C72BP5L.cjs +179 -0
- package/dist/chunk-2C72BP5L.cjs.map +1 -0
- package/dist/chunk-BPSZE2VX.js +13 -0
- package/dist/chunk-BPSZE2VX.js.map +1 -0
- package/dist/chunk-Q3YXBCTD.cjs +13 -0
- package/dist/chunk-Q3YXBCTD.cjs.map +1 -0
- package/dist/chunk-RUMDX66L.js +179 -0
- package/dist/chunk-RUMDX66L.js.map +1 -0
- package/dist/format.cjs +13 -0
- package/dist/format.cjs.map +1 -0
- package/dist/format.d.cts +9 -0
- package/dist/format.d.ts +9 -0
- package/dist/format.js +13 -0
- package/dist/format.js.map +1 -0
- package/dist/index.cjs +659 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +454 -0
- package/dist/index.d.ts +454 -0
- package/dist/index.js +659 -0
- package/dist/index.js.map +1 -0
- package/dist/result.cjs +9 -0
- package/dist/result.cjs.map +1 -0
- package/dist/result.d.cts +26 -0
- package/dist/result.d.ts +26 -0
- package/dist/result.js +9 -0
- package/dist/result.js.map +1 -0
- package/format.cjs +13 -0
- package/package.json +163 -100
- package/result.cjs +9 -0
- package/CHANGELOG.md +0 -665
- package/Decoder.d.ts +0 -94
- package/Decoder.js +0 -105
- package/Decoder.js.flow +0 -286
- package/Decoder.mjs +0 -101
- package/NotSupportedTSVersion.d.ts +0 -1
- package/_utils.d.ts +0 -9
- package/_utils.js +0 -80
- package/_utils.js.flow +0 -107
- package/_utils.mjs +0 -70
- package/annotate.d.ts +0 -62
- package/annotate.js +0 -145
- package/annotate.js.flow +0 -218
- package/annotate.mjs +0 -131
- package/format.d.ts +0 -6
- package/format.js +0 -117
- package/format.js.flow +0 -128
- package/format.mjs +0 -110
- package/index.d.ts +0 -40
- package/index.js +0 -67
- package/index.js.flow +0 -44
- package/index.mjs +0 -11
- package/lib/_helpers.d.ts +0 -79
- package/lib/arrays.d.ts +0 -59
- package/lib/arrays.js +0 -85
- package/lib/arrays.js.flow +0 -138
- package/lib/arrays.mjs +0 -75
- package/lib/basics.d.ts +0 -93
- package/lib/basics.js +0 -74
- package/lib/basics.js.flow +0 -124
- package/lib/basics.mjs +0 -60
- package/lib/booleans.d.ts +0 -16
- package/lib/booleans.js +0 -21
- package/lib/booleans.js.flow +0 -22
- package/lib/booleans.mjs +0 -15
- package/lib/dates.d.ts +0 -15
- package/lib/dates.js +0 -23
- package/lib/dates.js.flow +0 -40
- package/lib/dates.mjs +0 -17
- package/lib/json.d.ts +0 -35
- package/lib/json.js +0 -25
- package/lib/json.js.flow +0 -50
- package/lib/json.mjs +0 -18
- package/lib/numbers.d.ts +0 -31
- package/lib/numbers.js +0 -34
- package/lib/numbers.js.flow +0 -46
- package/lib/numbers.mjs +0 -25
- package/lib/objects.d.ts +0 -76
- package/lib/objects.js +0 -138
- package/lib/objects.js.flow +0 -238
- package/lib/objects.mjs +0 -128
- package/lib/strings.d.ts +0 -56
- package/lib/strings.js +0 -54
- package/lib/strings.js.flow +0 -90
- package/lib/strings.mjs +0 -40
- package/lib/unions.d.ts +0 -55
- package/lib/unions.js +0 -82
- package/lib/unions.js.flow +0 -155
- package/lib/unions.mjs +0 -75
- package/lib/utilities.d.ts +0 -40
- package/lib/utilities.js +0 -44
- package/lib/utilities.js.flow +0 -65
- package/lib/utilities.mjs +0 -35
- package/result.d.ts +0 -16
- package/result.js +0 -21
- package/result.js.flow +0 -26
- package/result.mjs +0 -15
package/lib/strings.js.flow
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { define } from '../Decoder';
|
|
4
|
-
import { either } from './unions';
|
|
5
|
-
import { instanceOf } from './utilities';
|
|
6
|
-
import type { Decoder } from '../Decoder';
|
|
7
|
-
|
|
8
|
-
/** Match groups in this regex:
|
|
9
|
-
* \1 - the scheme
|
|
10
|
-
* \2 - the username/password (optional)
|
|
11
|
-
* \3 - the host
|
|
12
|
-
* \4 - the port (optional)
|
|
13
|
-
* \5 - the path (optional)
|
|
14
|
-
*/
|
|
15
|
-
const url_re =
|
|
16
|
-
/^([A-Za-z]{3,9}(?:[+][A-Za-z]{3,9})?):\/\/(?:([-;:&=+$,\w]+)@)?(?:([A-Za-z0-9.-]+)(?::([0-9]{2,5}))?)(\/(?:[-+~%/.,\w]*)?(?:\?[-+=&;%@.,/\w]*)?(?:#[.,!/\w]*)?)?$/;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Accepts and returns strings.
|
|
20
|
-
*/
|
|
21
|
-
export const string: Decoder<string> = define((blob, ok, err) =>
|
|
22
|
-
typeof blob === 'string' ? ok(blob) : err('Must be string'),
|
|
23
|
-
);
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Like `string`, but will reject the empty string or strings containing only whitespace.
|
|
27
|
-
*/
|
|
28
|
-
export const nonEmptyString: Decoder<string> = regex(/\S/, 'Must be non-empty string');
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Accepts and returns strings that match the given regular expression.
|
|
32
|
-
*/
|
|
33
|
-
export function regex(regex: RegExp, msg: string): Decoder<string> {
|
|
34
|
-
return string.refine((s) => regex.test(s), msg);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Accepts and returns strings that are syntactically valid email addresses.
|
|
39
|
-
* (This will not mean that the email address actually exist.)
|
|
40
|
-
*/
|
|
41
|
-
export const email: Decoder<string> = regex(
|
|
42
|
-
// The almost perfect email regex, taken from https://emailregex.com/
|
|
43
|
-
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
|
|
44
|
-
'Must be email',
|
|
45
|
-
);
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Accepts strings that are valid URLs, returns the value as a URL instance.
|
|
49
|
-
*/
|
|
50
|
-
export const url: Decoder<URL> = either(
|
|
51
|
-
regex(url_re, 'Must be URL').transform((value) => new URL(value)),
|
|
52
|
-
instanceOf(URL),
|
|
53
|
-
);
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Accepts strings that are valid URLs, but only HTTPS ones. Returns the value
|
|
57
|
-
* as a URL instance.
|
|
58
|
-
*/
|
|
59
|
-
export const httpsUrl: Decoder<URL> = url.refine(
|
|
60
|
-
(value) => value.protocol === 'https:',
|
|
61
|
-
'Must be an HTTPS URL',
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Accepts strings that are valid
|
|
66
|
-
* [UUIDs](https://en.wikipedia.org/wiki/universally_unique_identifier)
|
|
67
|
-
* (universally unique identifier).
|
|
68
|
-
*/
|
|
69
|
-
export const uuid: Decoder<string> = regex(
|
|
70
|
-
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i,
|
|
71
|
-
'Must be uuid',
|
|
72
|
-
);
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Like `uuid`, but only accepts
|
|
76
|
-
* [UUIDv1](https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_1_%28date-time_and_MAC_address%29)
|
|
77
|
-
* strings.
|
|
78
|
-
*/
|
|
79
|
-
export const uuidv1: Decoder<string> =
|
|
80
|
-
// https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_1_(date-time_and_MAC_address)
|
|
81
|
-
uuid.refine((value) => value[14] === '1', 'Must be uuidv1');
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Like `uuid`, but only accepts
|
|
85
|
-
* [UUIDv4](https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_%28random%29)
|
|
86
|
-
* strings.
|
|
87
|
-
*/
|
|
88
|
-
export const uuidv4: Decoder<string> =
|
|
89
|
-
// https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)
|
|
90
|
-
uuid.refine((value) => value[14] === '4', 'Must be uuidv4');
|
package/lib/strings.mjs
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { define } from '../Decoder.mjs'
|
|
2
|
-
import { either } from './unions.mjs'
|
|
3
|
-
import { instanceOf } from './utilities.mjs'
|
|
4
|
-
|
|
5
|
-
var url_re = /^([A-Za-z]{3,9}(?:[+][A-Za-z]{3,9})?):\/\/(?:([-;:&=+$,\w]+)@)?(?:([A-Za-z0-9.-]+)(?::([0-9]{2,5}))?)(\/(?:[-+~%/.,\w]*)?(?:\?[-+=&;%@.,/\w]*)?(?:#[.,!/\w]*)?)?$/
|
|
6
|
-
|
|
7
|
-
export var string = define(function (blob, ok, err) {
|
|
8
|
-
return typeof blob === 'string' ? ok(blob) : err('Must be string')
|
|
9
|
-
})
|
|
10
|
-
|
|
11
|
-
export var nonEmptyString = regex(/\S/, 'Must be non-empty string')
|
|
12
|
-
|
|
13
|
-
export function regex(regex, msg) {
|
|
14
|
-
return string.refine(function (s) {
|
|
15
|
-
return regex.test(s)
|
|
16
|
-
}, msg)
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export var email = regex(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, 'Must be email')
|
|
20
|
-
|
|
21
|
-
export var url = either(
|
|
22
|
-
regex(url_re, 'Must be URL').transform(function (value) {
|
|
23
|
-
return new URL(value)
|
|
24
|
-
}),
|
|
25
|
-
instanceOf(URL)
|
|
26
|
-
)
|
|
27
|
-
|
|
28
|
-
export var httpsUrl = url.refine(function (value) {
|
|
29
|
-
return value.protocol === 'https:'
|
|
30
|
-
}, 'Must be an HTTPS URL')
|
|
31
|
-
|
|
32
|
-
export var uuid = regex(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i, 'Must be uuid')
|
|
33
|
-
|
|
34
|
-
export var uuidv1 = uuid.refine(function (value) {
|
|
35
|
-
return value[14] === '1'
|
|
36
|
-
}, 'Must be uuidv1')
|
|
37
|
-
|
|
38
|
-
export var uuidv4 = uuid.refine(function (value) {
|
|
39
|
-
return value[14] === '4'
|
|
40
|
-
}, 'Must be uuidv4')
|
package/lib/unions.d.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { Decoder, DecoderType, Scalar } from '../Decoder';
|
|
2
|
-
|
|
3
|
-
export type Values<T extends object> = T[keyof T];
|
|
4
|
-
|
|
5
|
-
export type DecoderTypes<T> = T extends ReadonlyArray<Decoder<infer U>> ? U : never;
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Accepts values accepted by any of the given decoders.
|
|
9
|
-
*
|
|
10
|
-
* The decoders are tried on the input one by one, in the given order. The
|
|
11
|
-
* first one that accepts the input "wins". If all decoders reject the input,
|
|
12
|
-
* the input gets rejected.
|
|
13
|
-
*/
|
|
14
|
-
export function either<T extends ReadonlyArray<Decoder<any>>>(
|
|
15
|
-
...args: T
|
|
16
|
-
): Decoder<DecoderTypes<T>>;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Accepts any value that is strictly-equal (using `===`) to one of the
|
|
20
|
-
* specified values.
|
|
21
|
-
*/
|
|
22
|
-
export function oneOf<T extends Scalar>(constants: readonly T[]): Decoder<T>;
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* If you are decoding tagged unions you may want to use the `taggedUnion()`
|
|
26
|
-
* decoder instead of the general purpose `either()` decoder to get better
|
|
27
|
-
* error messages and better performance.
|
|
28
|
-
*
|
|
29
|
-
* This decoder is optimized for [tagged
|
|
30
|
-
* unions](https://en.wikipedia.org/wiki/Tagged_union), i.e. a union of
|
|
31
|
-
* objects where one field is used as the discriminator.
|
|
32
|
-
*
|
|
33
|
-
* ```ts
|
|
34
|
-
* const A = object({ tag: constant('A'), foo: string });
|
|
35
|
-
* const B = object({ tag: constant('B'), bar: number });
|
|
36
|
-
*
|
|
37
|
-
* const AorB = taggedUnion('tag', { A, B });
|
|
38
|
-
* // ^^^
|
|
39
|
-
* ```
|
|
40
|
-
*
|
|
41
|
-
* Decoding now works in two steps:
|
|
42
|
-
*
|
|
43
|
-
* 1. Look at the `'tag'` field in the incoming object (this is the field
|
|
44
|
-
* that decides which decoder will be used)
|
|
45
|
-
* 2. If the value is `'A'`, then decoder `A` will be used. If it's `'B'`, then
|
|
46
|
-
* decoder `B` will be used. Otherwise, this will fail.
|
|
47
|
-
*
|
|
48
|
-
* This is effectively equivalent to `either(A, B)`, but will provide better
|
|
49
|
-
* error messages and is more performant at runtime because it doesn't have to
|
|
50
|
-
* try all decoders one by one.
|
|
51
|
-
*/
|
|
52
|
-
export function taggedUnion<O extends Record<string, Decoder<any>>>(
|
|
53
|
-
field: string,
|
|
54
|
-
mapping: O,
|
|
55
|
-
): Decoder<Values<{ [key in keyof O]: DecoderType<O[key]> }>>;
|
package/lib/unions.js
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true
|
|
4
|
-
exports.either = void 0
|
|
5
|
-
exports.oneOf = oneOf
|
|
6
|
-
exports.taggedUnion = taggedUnion
|
|
7
|
-
var _Decoder = require('../Decoder')
|
|
8
|
-
var _utils = require('../_utils')
|
|
9
|
-
var _objects = require('./objects')
|
|
10
|
-
var _utilities = require('./utilities')
|
|
11
|
-
var EITHER_PREFIX = 'Either:\n'
|
|
12
|
-
|
|
13
|
-
function itemize(s) {
|
|
14
|
-
return '-' + (0, _utils.indent)(s).substring(1)
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function nest(errText) {
|
|
18
|
-
return errText.startsWith(EITHER_PREFIX) ? errText.substr(EITHER_PREFIX.length) : itemize(errText)
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function _either() {
|
|
22
|
-
for (var _len = arguments.length, decoders = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
23
|
-
decoders[_key] = arguments[_key]
|
|
24
|
-
}
|
|
25
|
-
if (decoders.length === 0) {
|
|
26
|
-
throw new Error('Pass at least one decoder to either()')
|
|
27
|
-
}
|
|
28
|
-
return (0, _Decoder.define)(function (blob, _, err) {
|
|
29
|
-
var errors = []
|
|
30
|
-
for (var _i = 0; _i < decoders.length; _i++) {
|
|
31
|
-
var result = decoders[_i].decode(blob)
|
|
32
|
-
if (result.ok) {
|
|
33
|
-
return result
|
|
34
|
-
} else {
|
|
35
|
-
errors.push(result.error)
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
var text =
|
|
39
|
-
EITHER_PREFIX +
|
|
40
|
-
errors
|
|
41
|
-
.map(function (err) {
|
|
42
|
-
return nest((0, _utils.summarize)(err).join('\n'))
|
|
43
|
-
})
|
|
44
|
-
.join('\n')
|
|
45
|
-
return err(text)
|
|
46
|
-
})
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
var either = _either
|
|
50
|
-
|
|
51
|
-
exports.either = either
|
|
52
|
-
function oneOf(constants) {
|
|
53
|
-
return (0, _Decoder.define)(function (blob, ok, err) {
|
|
54
|
-
var winner = constants.find(function (c) {
|
|
55
|
-
return c === blob
|
|
56
|
-
})
|
|
57
|
-
if (winner !== undefined) {
|
|
58
|
-
return ok(winner)
|
|
59
|
-
}
|
|
60
|
-
return err(
|
|
61
|
-
'Must be one of ' +
|
|
62
|
-
constants
|
|
63
|
-
.map(function (value) {
|
|
64
|
-
return JSON.stringify(value)
|
|
65
|
-
})
|
|
66
|
-
.join(', ')
|
|
67
|
-
)
|
|
68
|
-
})
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function taggedUnion(field, mapping) {
|
|
72
|
-
var _object
|
|
73
|
-
var base = (0, _objects.object)(((_object = {}), (_object[field] = (0, _utilities.prep)(String, oneOf(Object.keys(mapping)))), _object)).transform(function (o) {
|
|
74
|
-
return o[field]
|
|
75
|
-
})
|
|
76
|
-
return base.peek_UNSTABLE(function (_ref) {
|
|
77
|
-
var blob = _ref[0],
|
|
78
|
-
key = _ref[1]
|
|
79
|
-
var decoder = mapping[key]
|
|
80
|
-
return decoder.decode(blob)
|
|
81
|
-
})
|
|
82
|
-
}
|
package/lib/unions.js.flow
DELETED
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { define } from '../Decoder';
|
|
4
|
-
import { indent, summarize } from '../_utils';
|
|
5
|
-
import { object } from './objects';
|
|
6
|
-
import { prep } from './utilities';
|
|
7
|
-
import type { _Any } from '../_utils';
|
|
8
|
-
import type { Decoder, DecodeResult, Scalar } from '../Decoder';
|
|
9
|
-
|
|
10
|
-
const EITHER_PREFIX = 'Either:\n';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Indents and adds a dash in front of this (potentially multiline) string.
|
|
14
|
-
*/
|
|
15
|
-
function itemize(s: string): string {
|
|
16
|
-
return `-${indent(s).substring(1)}`;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Nests another error as an item under a new-to-be-created "Either error". If
|
|
21
|
-
* the given subitem already is an "Either error" of itself, don't indent, but
|
|
22
|
-
* just "inject" its items at the same error level, for nicely flattened either
|
|
23
|
-
* expressions.
|
|
24
|
-
*
|
|
25
|
-
* Avoids:
|
|
26
|
-
*
|
|
27
|
-
* Either:
|
|
28
|
-
* - Either:
|
|
29
|
-
* - Must be P
|
|
30
|
-
* - Either:
|
|
31
|
-
* - Must be Q
|
|
32
|
-
* - Must be R
|
|
33
|
-
* - Must be S
|
|
34
|
-
*
|
|
35
|
-
* And "flattens" these to:
|
|
36
|
-
*
|
|
37
|
-
* Either:
|
|
38
|
-
* - Must be P
|
|
39
|
-
* - Must be Q
|
|
40
|
-
* - Must be R
|
|
41
|
-
* - Must be S
|
|
42
|
-
*
|
|
43
|
-
*/
|
|
44
|
-
function nest(errText: string): string {
|
|
45
|
-
return errText.startsWith(EITHER_PREFIX)
|
|
46
|
-
? errText.substr(EITHER_PREFIX.length)
|
|
47
|
-
: itemize(errText);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// prettier-ignore
|
|
51
|
-
interface EitherT {
|
|
52
|
-
<A>(a: Decoder<A>): Decoder<A>;
|
|
53
|
-
<A, B>(a: Decoder<A>, b: Decoder<B>): Decoder<A | B>;
|
|
54
|
-
<A, B, C>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>): Decoder<A | B | C>;
|
|
55
|
-
<A, B, C, D>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>): Decoder<A | B | C | D>;
|
|
56
|
-
<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>;
|
|
57
|
-
<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>;
|
|
58
|
-
<A, B, C, D, E, F, G>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>, e: Decoder<E>, f: Decoder<F>, g: Decoder<G>): Decoder<A | B | C | D | E | F | G>;
|
|
59
|
-
<A, B, C, D, E, F, G, H>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>, e: Decoder<E>, f: Decoder<F>, g: Decoder<G>, h: Decoder<H>): Decoder<A | B | C | D | E | F | G | H>;
|
|
60
|
-
<A, B, C, D, E, F, G, H, I>(a: Decoder<A>, b: Decoder<B>, c: Decoder<C>, d: Decoder<D>, e: Decoder<E>, f: Decoder<F>, g: Decoder<G>, h: Decoder<H>, i: Decoder<I>): Decoder<A | B | C | D | E | F | G | H | I>;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function _either(...decoders: $ReadOnlyArray<Decoder<mixed>>): Decoder<mixed> {
|
|
64
|
-
if (decoders.length === 0) {
|
|
65
|
-
throw new Error('Pass at least one decoder to either()');
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
return define((blob, _, err) => {
|
|
69
|
-
// Collect errors here along the way
|
|
70
|
-
const errors = [];
|
|
71
|
-
|
|
72
|
-
for (let i = 0; i < decoders.length; i++) {
|
|
73
|
-
const result: DecodeResult<mixed> = decoders[i].decode(blob);
|
|
74
|
-
if (result.ok) {
|
|
75
|
-
return result;
|
|
76
|
-
} else {
|
|
77
|
-
errors.push(result.error);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Decoding all alternatives failed, return the combined error message
|
|
82
|
-
const text =
|
|
83
|
-
EITHER_PREFIX +
|
|
84
|
-
errors.map((err) => nest(summarize(err).join('\n'))).join('\n');
|
|
85
|
-
return err(text);
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/**
|
|
90
|
-
* Accepts values accepted by any of the given decoders.
|
|
91
|
-
*
|
|
92
|
-
* The decoders are tried on the input one by one, in the given order. The
|
|
93
|
-
* first one that accepts the input "wins". If all decoders reject the input,
|
|
94
|
-
* the input gets rejected.
|
|
95
|
-
*/
|
|
96
|
-
export const either: EitherT = (_either: _Any);
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Accepts any value that is strictly-equal (using `===`) to one of the
|
|
100
|
-
* specified values.
|
|
101
|
-
*/
|
|
102
|
-
export function oneOf<T: Scalar>(constants: $ReadOnlyArray<T>): Decoder<T> {
|
|
103
|
-
return define((blob, ok, err) => {
|
|
104
|
-
const winner = constants.find((c) => c === blob);
|
|
105
|
-
if (winner !== undefined) {
|
|
106
|
-
return ok(winner);
|
|
107
|
-
}
|
|
108
|
-
return err(
|
|
109
|
-
`Must be one of ${constants
|
|
110
|
-
.map((value) => JSON.stringify(value))
|
|
111
|
-
.join(', ')}`,
|
|
112
|
-
);
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* If you are decoding tagged unions you may want to use the `taggedUnion()`
|
|
118
|
-
* decoder instead of the general purpose `either()` decoder to get better
|
|
119
|
-
* error messages and better performance.
|
|
120
|
-
*
|
|
121
|
-
* This decoder is optimized for [tagged
|
|
122
|
-
* unions](https://en.wikipedia.org/wiki/Tagged_union), i.e. a union of
|
|
123
|
-
* objects where one field is used as the discriminator.
|
|
124
|
-
*
|
|
125
|
-
* ```ts
|
|
126
|
-
* const A = object({ tag: constant('A'), foo: string });
|
|
127
|
-
* const B = object({ tag: constant('B'), bar: number });
|
|
128
|
-
*
|
|
129
|
-
* const AorB = taggedUnion('tag', { A, B });
|
|
130
|
-
* // ^^^
|
|
131
|
-
* ```
|
|
132
|
-
*
|
|
133
|
-
* Decoding now works in two steps:
|
|
134
|
-
*
|
|
135
|
-
* 1. Look at the `'tag'` field in the incoming object (this is the field
|
|
136
|
-
* that decides which decoder will be used)
|
|
137
|
-
* 2. If the value is `'A'`, then decoder `A` will be used. If it's `'B'`, then
|
|
138
|
-
* decoder `B` will be used. Otherwise, this will fail.
|
|
139
|
-
*
|
|
140
|
-
* This is effectively equivalent to `either(A, B)`, but will provide better
|
|
141
|
-
* error messages and is more performant at runtime because it doesn't have to
|
|
142
|
-
* try all decoders one by one.
|
|
143
|
-
*/
|
|
144
|
-
export function taggedUnion<O: { +[field: string]: Decoder<_Any>, ... }>(
|
|
145
|
-
field: string,
|
|
146
|
-
mapping: O,
|
|
147
|
-
): Decoder<$Values<$ObjMap<O, <T>(Decoder<T>) => T>>> {
|
|
148
|
-
const base: Decoder<string> = object({
|
|
149
|
-
[field]: prep(String, oneOf(Object.keys(mapping))),
|
|
150
|
-
}).transform((o) => o[field]);
|
|
151
|
-
return base.peek_UNSTABLE(([blob, key]) => {
|
|
152
|
-
const decoder = mapping[key];
|
|
153
|
-
return decoder.decode(blob);
|
|
154
|
-
});
|
|
155
|
-
}
|
package/lib/unions.mjs
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { define } from '../Decoder.mjs'
|
|
2
|
-
import { indent, summarize } from '../_utils.mjs'
|
|
3
|
-
import { object } from './objects.mjs'
|
|
4
|
-
import { prep } from './utilities.mjs'
|
|
5
|
-
var EITHER_PREFIX = 'Either:\n'
|
|
6
|
-
|
|
7
|
-
function itemize(s) {
|
|
8
|
-
return '-' + indent(s).substring(1)
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
function nest(errText) {
|
|
12
|
-
return errText.startsWith(EITHER_PREFIX) ? errText.substr(EITHER_PREFIX.length) : itemize(errText)
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function _either() {
|
|
16
|
-
for (var _len = arguments.length, decoders = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
17
|
-
decoders[_key] = arguments[_key]
|
|
18
|
-
}
|
|
19
|
-
if (decoders.length === 0) {
|
|
20
|
-
throw new Error('Pass at least one decoder to either()')
|
|
21
|
-
}
|
|
22
|
-
return define(function (blob, _, err) {
|
|
23
|
-
var errors = []
|
|
24
|
-
for (var _i = 0; _i < decoders.length; _i++) {
|
|
25
|
-
var result = decoders[_i].decode(blob)
|
|
26
|
-
if (result.ok) {
|
|
27
|
-
return result
|
|
28
|
-
} else {
|
|
29
|
-
errors.push(result.error)
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
var text =
|
|
33
|
-
EITHER_PREFIX +
|
|
34
|
-
errors
|
|
35
|
-
.map(function (err) {
|
|
36
|
-
return nest(summarize(err).join('\n'))
|
|
37
|
-
})
|
|
38
|
-
.join('\n')
|
|
39
|
-
return err(text)
|
|
40
|
-
})
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export var either = _either
|
|
44
|
-
|
|
45
|
-
export function oneOf(constants) {
|
|
46
|
-
return define(function (blob, ok, err) {
|
|
47
|
-
var winner = constants.find(function (c) {
|
|
48
|
-
return c === blob
|
|
49
|
-
})
|
|
50
|
-
if (winner !== undefined) {
|
|
51
|
-
return ok(winner)
|
|
52
|
-
}
|
|
53
|
-
return err(
|
|
54
|
-
'Must be one of ' +
|
|
55
|
-
constants
|
|
56
|
-
.map(function (value) {
|
|
57
|
-
return JSON.stringify(value)
|
|
58
|
-
})
|
|
59
|
-
.join(', ')
|
|
60
|
-
)
|
|
61
|
-
})
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function taggedUnion(field, mapping) {
|
|
65
|
-
var _object
|
|
66
|
-
var base = object(((_object = {}), (_object[field] = prep(String, oneOf(Object.keys(mapping)))), _object)).transform(function (o) {
|
|
67
|
-
return o[field]
|
|
68
|
-
})
|
|
69
|
-
return base.peek_UNSTABLE(function (_ref) {
|
|
70
|
-
var blob = _ref[0],
|
|
71
|
-
key = _ref[1]
|
|
72
|
-
var decoder = mapping[key]
|
|
73
|
-
return decoder.decode(blob)
|
|
74
|
-
})
|
|
75
|
-
}
|
package/lib/utilities.d.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { Decoder } from '../Decoder';
|
|
2
|
-
|
|
3
|
-
export interface Klass<T> extends Function {
|
|
4
|
-
new (...args: readonly any[]): T;
|
|
5
|
-
}
|
|
6
|
-
|
|
7
|
-
export type Instance<K> = K extends Klass<infer T> ? T : never;
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Accepts any value that is an ``instanceof`` the given class.
|
|
11
|
-
*/
|
|
12
|
-
export function instanceOf<K extends Klass<any>>(klass: K): Decoder<Instance<K>>;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Lazily evaluate the given decoder. This is useful to build self-referential
|
|
16
|
-
* types for recursive data structures.
|
|
17
|
-
*/
|
|
18
|
-
export function lazy<T>(decoderFn: () => Decoder<T>): Decoder<T>;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Pre-process the data input before passing it into the decoder. This gives
|
|
22
|
-
* you the ability to arbitrarily customize the input on the fly before passing
|
|
23
|
-
* it to the decoder. Of course, the input value at that point is still of
|
|
24
|
-
* ``unknown`` type, so you will have to deal with that accordingly.
|
|
25
|
-
*/
|
|
26
|
-
export function prep<T>(
|
|
27
|
-
mapperFn: (blob: unknown) => unknown,
|
|
28
|
-
decoder: Decoder<T>,
|
|
29
|
-
): Decoder<T>;
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Rejects all inputs, and always fails with the given error message. May be
|
|
33
|
-
* useful for explicitly disallowing keys, or for testing purposes.
|
|
34
|
-
*/
|
|
35
|
-
export function never(msg: string): Decoder<never>;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Alias of never().
|
|
39
|
-
*/
|
|
40
|
-
export function fail(msg: string): Decoder<never>;
|
package/lib/utilities.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true
|
|
4
|
-
exports.fail = void 0
|
|
5
|
-
exports.instanceOf = instanceOf
|
|
6
|
-
exports.lazy = lazy
|
|
7
|
-
exports.never = never
|
|
8
|
-
exports.prep = prep
|
|
9
|
-
var _annotate = require('../annotate')
|
|
10
|
-
var _Decoder = require('../Decoder')
|
|
11
|
-
|
|
12
|
-
function instanceOf(klass) {
|
|
13
|
-
return (0, _Decoder.define)(function (blob, ok, err) {
|
|
14
|
-
return blob instanceof klass ? ok(blob) : err('Must be ' + klass.name + ' instance')
|
|
15
|
-
})
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function lazy(decoderFn) {
|
|
19
|
-
return (0, _Decoder.define)(function (blob) {
|
|
20
|
-
return decoderFn().decode(blob)
|
|
21
|
-
})
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function prep(mapperFn, decoder) {
|
|
25
|
-
return (0, _Decoder.define)(function (originalInput, _, err) {
|
|
26
|
-
var blob
|
|
27
|
-
try {
|
|
28
|
-
blob = mapperFn(originalInput)
|
|
29
|
-
} catch (e) {
|
|
30
|
-
return err((0, _annotate.annotate)(originalInput, e.message))
|
|
31
|
-
}
|
|
32
|
-
var r = decoder.decode(blob)
|
|
33
|
-
return r.ok ? r : err((0, _annotate.annotate)(originalInput, r.error.text))
|
|
34
|
-
})
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function never(msg) {
|
|
38
|
-
return (0, _Decoder.define)(function (_, __, err) {
|
|
39
|
-
return err(msg)
|
|
40
|
-
})
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
var fail = never
|
|
44
|
-
exports.fail = fail
|
package/lib/utilities.js.flow
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { annotate } from '../annotate';
|
|
4
|
-
import { define } from '../Decoder';
|
|
5
|
-
import type { Decoder } from '../Decoder';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Accepts any value that is an ``instanceof`` the given class.
|
|
9
|
-
*/
|
|
10
|
-
export function instanceOf<T>(klass: Class<T>): Decoder<T> {
|
|
11
|
-
return define((blob, ok, err) =>
|
|
12
|
-
blob instanceof klass
|
|
13
|
-
? ok(blob)
|
|
14
|
-
: err(
|
|
15
|
-
`Must be ${
|
|
16
|
-
// $FlowFixMe[incompatible-use] - klass.name is fine?
|
|
17
|
-
klass.name
|
|
18
|
-
} instance`,
|
|
19
|
-
),
|
|
20
|
-
);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Lazily evaluate the given decoder. This is useful to build self-referential
|
|
25
|
-
* types for recursive data structures.
|
|
26
|
-
*/
|
|
27
|
-
export function lazy<T>(decoderFn: () => Decoder<T>): Decoder<T> {
|
|
28
|
-
return define((blob) => decoderFn().decode(blob));
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Pre-process the data input before passing it into the decoder. This gives
|
|
33
|
-
* you the ability to arbitrarily customize the input on the fly before passing
|
|
34
|
-
* it to the decoder. Of course, the input value at that point is still of
|
|
35
|
-
* ``unknown`` type, so you will have to deal with that accordingly.
|
|
36
|
-
*/
|
|
37
|
-
export function prep<T>(mapperFn: (mixed) => mixed, decoder: Decoder<T>): Decoder<T> {
|
|
38
|
-
return define((originalInput, _, err) => {
|
|
39
|
-
let blob;
|
|
40
|
-
try {
|
|
41
|
-
blob = mapperFn(originalInput);
|
|
42
|
-
} catch (e) {
|
|
43
|
-
return err(annotate(originalInput, e.message));
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const r = decoder.decode(blob);
|
|
47
|
-
return r.ok ? r : err(annotate(originalInput, r.error.text));
|
|
48
|
-
// ^^^^^^^^^^^^^
|
|
49
|
-
// Annotates the _original_ input value
|
|
50
|
-
// (instead of echoing back blob)
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Rejects all inputs, and always fails with the given error message. May be
|
|
56
|
-
* useful for explicitly disallowing keys, or for testing purposes.
|
|
57
|
-
*/
|
|
58
|
-
export function never(msg: string): Decoder<empty> {
|
|
59
|
-
return define((_, __, err) => err(msg));
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Alias of never().
|
|
64
|
-
*/
|
|
65
|
-
export const fail: (msg: string) => Decoder<empty> = never;
|
package/lib/utilities.mjs
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { annotate } from '../annotate.mjs'
|
|
2
|
-
import { define } from '../Decoder.mjs'
|
|
3
|
-
|
|
4
|
-
export function instanceOf(klass) {
|
|
5
|
-
return define(function (blob, ok, err) {
|
|
6
|
-
return blob instanceof klass ? ok(blob) : err('Must be ' + klass.name + ' instance')
|
|
7
|
-
})
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function lazy(decoderFn) {
|
|
11
|
-
return define(function (blob) {
|
|
12
|
-
return decoderFn().decode(blob)
|
|
13
|
-
})
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function prep(mapperFn, decoder) {
|
|
17
|
-
return define(function (originalInput, _, err) {
|
|
18
|
-
var blob
|
|
19
|
-
try {
|
|
20
|
-
blob = mapperFn(originalInput)
|
|
21
|
-
} catch (e) {
|
|
22
|
-
return err(annotate(originalInput, e.message))
|
|
23
|
-
}
|
|
24
|
-
var r = decoder.decode(blob)
|
|
25
|
-
return r.ok ? r : err(annotate(originalInput, r.error.text))
|
|
26
|
-
})
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
export function never(msg) {
|
|
30
|
-
return define(function (_, __, err) {
|
|
31
|
-
return err(msg)
|
|
32
|
-
})
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export var fail = never
|