decoders 2.0.0-beta1 → 2.0.0-beta13
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/CHANGELOG.md +53 -5
- package/Decoder.d.ts +94 -0
- package/Decoder.js +222 -0
- package/Decoder.js.flow +286 -0
- package/Decoder.mjs +215 -0
- package/NotSupportedTSVersion.d.ts +1 -0
- package/README.md +124 -961
- package/_utils.d.ts +9 -0
- package/{cjs/_utils.js → _utils.js} +12 -18
- package/{cjs/_utils.js.flow → _utils.js.flow} +15 -19
- package/{es/_utils.js → _utils.mjs} +11 -15
- package/{ts/annotate.d.ts → annotate.d.ts} +25 -21
- package/{cjs/annotate.js → annotate.js} +0 -0
- package/{cjs/annotate.js.flow → annotate.js.flow} +0 -0
- package/{es/annotate.js → annotate.mjs} +0 -0
- package/format.d.ts +6 -0
- package/{cjs/format/inline.js → format.js} +7 -2
- package/{cjs/format/inline.js.flow → format.js.flow} +9 -3
- package/{es/format/inline.js → format.mjs} +5 -2
- package/index.d.ts +40 -0
- package/index.js +89 -0
- package/index.js.flow +44 -0
- package/index.mjs +11 -0
- package/{ts → lib}/_helpers.d.ts +0 -0
- package/lib/arrays.d.ts +59 -0
- package/lib/arrays.js +139 -0
- package/lib/arrays.js.flow +138 -0
- package/lib/arrays.mjs +124 -0
- package/lib/basics.d.ts +93 -0
- package/lib/basics.js +144 -0
- package/lib/basics.js.flow +124 -0
- package/lib/basics.mjs +120 -0
- package/lib/booleans.d.ts +16 -0
- package/lib/booleans.js +35 -0
- package/lib/booleans.js.flow +22 -0
- package/lib/booleans.mjs +25 -0
- package/lib/dates.d.ts +15 -0
- package/lib/dates.js +44 -0
- package/lib/dates.js.flow +40 -0
- package/lib/dates.mjs +34 -0
- package/lib/json.d.ts +35 -0
- package/lib/json.js +55 -0
- package/lib/json.js.flow +50 -0
- package/lib/json.mjs +40 -0
- package/lib/numbers.d.ts +31 -0
- package/lib/numbers.js +51 -0
- package/lib/numbers.js.flow +48 -0
- package/lib/numbers.mjs +41 -0
- package/lib/objects.d.ts +75 -0
- package/lib/objects.js +240 -0
- package/lib/objects.js.flow +246 -0
- package/lib/objects.mjs +223 -0
- package/lib/strings.d.ts +56 -0
- package/lib/strings.js +101 -0
- package/lib/strings.js.flow +90 -0
- package/lib/strings.mjs +82 -0
- package/lib/unions.d.ts +55 -0
- package/lib/unions.js +160 -0
- package/lib/unions.js.flow +155 -0
- package/lib/unions.mjs +146 -0
- package/lib/utilities.d.ts +34 -0
- package/lib/utilities.js +75 -0
- package/lib/utilities.js.flow +65 -0
- package/lib/utilities.mjs +60 -0
- package/package.json +79 -29
- package/result.d.ts +16 -0
- package/result.js +34 -0
- package/result.js.flow +26 -0
- package/result.mjs +27 -0
- package/cjs/_guard.js +0 -26
- package/cjs/_guard.js.flow +0 -20
- package/cjs/_types.js +0 -1
- package/cjs/_types.js.flow +0 -20
- package/cjs/format/index.js +0 -12
- package/cjs/format/index.js.flow +0 -4
- package/cjs/format/short.js +0 -10
- package/cjs/format/short.js.flow +0 -8
- package/cjs/index.js +0 -120
- package/cjs/index.js.flow +0 -63
- package/cjs/result.js +0 -172
- package/cjs/result.js.flow +0 -166
- package/cjs/stdlib/array.js +0 -108
- package/cjs/stdlib/array.js.flow +0 -103
- package/cjs/stdlib/boolean.js +0 -44
- package/cjs/stdlib/boolean.js.flow +0 -29
- package/cjs/stdlib/composition.js +0 -56
- package/cjs/stdlib/composition.js.flow +0 -43
- package/cjs/stdlib/constants.js +0 -69
- package/cjs/stdlib/constants.js.flow +0 -46
- package/cjs/stdlib/date.js +0 -46
- package/cjs/stdlib/date.js.flow +0 -40
- package/cjs/stdlib/describe.js +0 -26
- package/cjs/stdlib/describe.js.flow +0 -17
- package/cjs/stdlib/dispatch.js +0 -62
- package/cjs/stdlib/dispatch.js.flow +0 -58
- package/cjs/stdlib/either.js +0 -117
- package/cjs/stdlib/either.js.flow +0 -151
- package/cjs/stdlib/fail.js +0 -21
- package/cjs/stdlib/fail.js.flow +0 -12
- package/cjs/stdlib/instanceOf.js +0 -19
- package/cjs/stdlib/instanceOf.js.flow +0 -20
- package/cjs/stdlib/json.js +0 -31
- package/cjs/stdlib/json.js.flow +0 -28
- package/cjs/stdlib/lazy.js +0 -16
- package/cjs/stdlib/lazy.js.flow +0 -15
- package/cjs/stdlib/mapping.js +0 -67
- package/cjs/stdlib/mapping.js.flow +0 -54
- package/cjs/stdlib/number.js +0 -40
- package/cjs/stdlib/number.js.flow +0 -34
- package/cjs/stdlib/object.js +0 -194
- package/cjs/stdlib/object.js.flow +0 -203
- package/cjs/stdlib/optional.js +0 -54
- package/cjs/stdlib/optional.js.flow +0 -41
- package/cjs/stdlib/string.js +0 -98
- package/cjs/stdlib/string.js.flow +0 -82
- package/cjs/stdlib/tuple.js +0 -173
- package/cjs/stdlib/tuple.js.flow +0 -220
- package/es/_guard.js +0 -15
- package/es/_types.js +0 -0
- package/es/format/index.js +0 -2
- package/es/format/short.js +0 -4
- package/es/index.js +0 -37
- package/es/result.js +0 -139
- package/es/stdlib/array.js +0 -91
- package/es/stdlib/boolean.js +0 -28
- package/es/stdlib/composition.js +0 -42
- package/es/stdlib/constants.js +0 -46
- package/es/stdlib/date.js +0 -28
- package/es/stdlib/describe.js +0 -16
- package/es/stdlib/dispatch.js +0 -51
- package/es/stdlib/either.js +0 -90
- package/es/stdlib/fail.js +0 -11
- package/es/stdlib/instanceOf.js +0 -8
- package/es/stdlib/json.js +0 -15
- package/es/stdlib/lazy.js +0 -11
- package/es/stdlib/mapping.js +0 -54
- package/es/stdlib/number.js +0 -25
- package/es/stdlib/object.js +0 -175
- package/es/stdlib/optional.js +0 -38
- package/es/stdlib/string.js +0 -76
- package/es/stdlib/tuple.js +0 -155
- package/ts/_guard.d.ts +0 -7
- package/ts/_types.d.ts +0 -16
- package/ts/_utils.d.ts +0 -13
- package/ts/array.d.ts +0 -5
- package/ts/boolean.d.ts +0 -5
- package/ts/constants.d.ts +0 -11
- package/ts/date.d.ts +0 -4
- package/ts/describe.d.ts +0 -3
- package/ts/dispatch.d.ts +0 -8
- package/ts/either.d.ts +0 -61
- package/ts/fail.d.ts +0 -3
- package/ts/index.d.ts +0 -42
- package/ts/inline.d.ts +0 -3
- package/ts/instanceOf.d.ts +0 -3
- package/ts/json.d.ts +0 -11
- package/ts/lazy.d.ts +0 -3
- package/ts/mapping.d.ts +0 -4
- package/ts/number.d.ts +0 -6
- package/ts/object.d.ts +0 -33
- package/ts/optional.d.ts +0 -5
- package/ts/result.d.ts +0 -39
- package/ts/short.d.ts +0 -3
- package/ts/string.d.ts +0 -7
- package/ts/tuple.d.ts +0 -30
package/lib/numbers.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { Decoder } from '../Decoder';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Accepts any valid ``number`` value.
|
|
5
|
+
*
|
|
6
|
+
* This also accepts special values like `NaN` and `Infinity`. Unless you
|
|
7
|
+
* want to deliberately accept those, you'll likely want to use the
|
|
8
|
+
* `number` decoder instead.
|
|
9
|
+
*/
|
|
10
|
+
export const anyNumber: Decoder<number>;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Accepts finite numbers (can be integer or float values). Values `NaN`,
|
|
14
|
+
* or positive and negative `Infinity` will get rejected.
|
|
15
|
+
*/
|
|
16
|
+
export const number: Decoder<number>;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Accepts only finite whole numbers.
|
|
20
|
+
*/
|
|
21
|
+
export const integer: Decoder<number>;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Accepts only positive finite numbers.
|
|
25
|
+
*/
|
|
26
|
+
export const positiveNumber: Decoder<number>;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Accepts only positive finite whole numbers.
|
|
30
|
+
*/
|
|
31
|
+
export const positiveInteger: Decoder<number>;
|
package/lib/numbers.js
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.positiveNumber = exports.positiveInteger = exports.number = exports.integer = exports.anyNumber = void 0;
|
|
5
|
+
|
|
6
|
+
var _Decoder = require("../Decoder");
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Accepts any valid ``number`` value.
|
|
10
|
+
*
|
|
11
|
+
* This also accepts special values like `NaN` and `Infinity`. Unless you
|
|
12
|
+
* want to deliberately accept those, you'll likely want to use the
|
|
13
|
+
* `number` decoder instead.
|
|
14
|
+
*/
|
|
15
|
+
var anyNumber = (0, _Decoder.define)(function (blob, ok, err) {
|
|
16
|
+
return typeof blob === 'number' ? ok(blob) : err('Must be number');
|
|
17
|
+
});
|
|
18
|
+
/**
|
|
19
|
+
* Accepts finite numbers (can be integer or float values). Values `NaN`,
|
|
20
|
+
* or positive and negative `Infinity` will get rejected.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
exports.anyNumber = anyNumber;
|
|
24
|
+
var number = anyNumber.refine(function (n) {
|
|
25
|
+
return Number.isFinite(n);
|
|
26
|
+
}, 'Number must be finite');
|
|
27
|
+
/**
|
|
28
|
+
* Accepts only finite whole numbers.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
exports.number = number;
|
|
32
|
+
var integer = number.refine(function (n) {
|
|
33
|
+
return Number.isInteger(n);
|
|
34
|
+
}, 'Number must be an integer');
|
|
35
|
+
/**
|
|
36
|
+
* Accepts only positive finite numbers.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
exports.integer = integer;
|
|
40
|
+
var positiveNumber = number.refine(function (n) {
|
|
41
|
+
return n >= 0;
|
|
42
|
+
}, 'Number must be positive');
|
|
43
|
+
/**
|
|
44
|
+
* Accepts only positive finite whole numbers.
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
exports.positiveNumber = positiveNumber;
|
|
48
|
+
var positiveInteger = integer.refine(function (n) {
|
|
49
|
+
return n >= 0;
|
|
50
|
+
}, 'Number must be positive');
|
|
51
|
+
exports.positiveInteger = positiveInteger;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import { define } from '../Decoder';
|
|
4
|
+
import type { Decoder } from '../Decoder';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Accepts any valid ``number`` value.
|
|
8
|
+
*
|
|
9
|
+
* This also accepts special values like `NaN` and `Infinity`. Unless you
|
|
10
|
+
* want to deliberately accept those, you'll likely want to use the
|
|
11
|
+
* `number` decoder instead.
|
|
12
|
+
*/
|
|
13
|
+
export const anyNumber: Decoder<number> = define((blob, ok, err) =>
|
|
14
|
+
typeof blob === 'number' ? ok(blob) : err('Must be number'),
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Accepts finite numbers (can be integer or float values). Values `NaN`,
|
|
19
|
+
* or positive and negative `Infinity` will get rejected.
|
|
20
|
+
*/
|
|
21
|
+
export const number: Decoder<number> = anyNumber.refine(
|
|
22
|
+
(n) => Number.isFinite(n),
|
|
23
|
+
'Number must be finite',
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Accepts only finite whole numbers.
|
|
28
|
+
*/
|
|
29
|
+
export const integer: Decoder<number> = number.refine(
|
|
30
|
+
(n) => Number.isInteger(n),
|
|
31
|
+
'Number must be an integer',
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Accepts only positive finite numbers.
|
|
36
|
+
*/
|
|
37
|
+
export const positiveNumber: Decoder<number> = number.refine(
|
|
38
|
+
(n) => n >= 0,
|
|
39
|
+
'Number must be positive',
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Accepts only positive finite whole numbers.
|
|
44
|
+
*/
|
|
45
|
+
export const positiveInteger: Decoder<number> = integer.refine(
|
|
46
|
+
(n) => n >= 0,
|
|
47
|
+
'Number must be positive',
|
|
48
|
+
);
|
package/lib/numbers.mjs
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { define } from '../Decoder.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Accepts any valid ``number`` value.
|
|
5
|
+
*
|
|
6
|
+
* This also accepts special values like `NaN` and `Infinity`. Unless you
|
|
7
|
+
* want to deliberately accept those, you'll likely want to use the
|
|
8
|
+
* `number` decoder instead.
|
|
9
|
+
*/
|
|
10
|
+
export var anyNumber = define(function (blob, ok, err) {
|
|
11
|
+
return typeof blob === 'number' ? ok(blob) : err('Must be number');
|
|
12
|
+
});
|
|
13
|
+
/**
|
|
14
|
+
* Accepts finite numbers (can be integer or float values). Values `NaN`,
|
|
15
|
+
* or positive and negative `Infinity` will get rejected.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export var number = anyNumber.refine(function (n) {
|
|
19
|
+
return Number.isFinite(n);
|
|
20
|
+
}, 'Number must be finite');
|
|
21
|
+
/**
|
|
22
|
+
* Accepts only finite whole numbers.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
export var integer = number.refine(function (n) {
|
|
26
|
+
return Number.isInteger(n);
|
|
27
|
+
}, 'Number must be an integer');
|
|
28
|
+
/**
|
|
29
|
+
* Accepts only positive finite numbers.
|
|
30
|
+
*/
|
|
31
|
+
|
|
32
|
+
export var positiveNumber = number.refine(function (n) {
|
|
33
|
+
return n >= 0;
|
|
34
|
+
}, 'Number must be positive');
|
|
35
|
+
/**
|
|
36
|
+
* Accepts only positive finite whole numbers.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
export var positiveInteger = integer.refine(function (n) {
|
|
40
|
+
return n >= 0;
|
|
41
|
+
}, 'Number must be positive');
|
package/lib/objects.d.ts
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/// <reference lib="es6" />
|
|
2
|
+
|
|
3
|
+
import { Decoder, DecoderType } from '../Decoder';
|
|
4
|
+
import { AllowImplicit } from './_helpers';
|
|
5
|
+
|
|
6
|
+
export type ObjectDecoderType<T> = AllowImplicit<{
|
|
7
|
+
[key in keyof T]: DecoderType<T[key]>;
|
|
8
|
+
}>;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Accepts any "plain old JavaScript object", but doesn't validate its keys or
|
|
12
|
+
* values further.
|
|
13
|
+
*/
|
|
14
|
+
export const pojo: Decoder<{ [key: string]: unknown }>;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Accepts objects with fields matching the given decoders. Extra fields that
|
|
18
|
+
* exist on the input object are ignored and will not be returned.
|
|
19
|
+
*/
|
|
20
|
+
export function object(decodersByKey: Record<any, never>): Decoder<Record<string, never>>;
|
|
21
|
+
export function object<O extends { [key: string]: Decoder<any> }>(
|
|
22
|
+
decodersByKey: O,
|
|
23
|
+
): Decoder<{ [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K] }>;
|
|
24
|
+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
25
|
+
// This is basically just equivalent to:
|
|
26
|
+
// ObjectDecoderType<O>
|
|
27
|
+
//
|
|
28
|
+
// But by "resolving" this with a mapped type, we remove the helper
|
|
29
|
+
// type names from the inferred type here, making this much easier to
|
|
30
|
+
// work with while developing.
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Like `object()`, but will reject inputs that contain extra fields that are
|
|
34
|
+
* not specified explicitly.
|
|
35
|
+
*/
|
|
36
|
+
export function exact(decodersByKey: Record<any, never>): Decoder<Record<string, never>>;
|
|
37
|
+
export function exact<O extends { [key: string]: Decoder<any> }>(
|
|
38
|
+
decodersByKey: O,
|
|
39
|
+
): Decoder<{ [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K] }>;
|
|
40
|
+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
41
|
+
// Ditto (see above)
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Like `object()`, but will pass through any extra fields on the input object
|
|
45
|
+
* unvalidated that will thus be of `unknown` type statically.
|
|
46
|
+
*/
|
|
47
|
+
export function inexact(
|
|
48
|
+
decodersByKey: Record<any, never>,
|
|
49
|
+
): Decoder<{ [extra: string]: unknown }>;
|
|
50
|
+
export function inexact<O extends { [key: string]: Decoder<any> }>(
|
|
51
|
+
decodersByKey: O,
|
|
52
|
+
): Decoder<
|
|
53
|
+
{ [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K] } & {
|
|
54
|
+
[extra: string]: unknown;
|
|
55
|
+
}
|
|
56
|
+
>;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Accepts objects where all values match the given decoder, and returns the
|
|
60
|
+
* result as a `{ [string]: T }`.
|
|
61
|
+
*
|
|
62
|
+
* The main difference between `object()` and `dict()` is that you'd typically
|
|
63
|
+
* use `object()` if this is a record-like object, where all field names are
|
|
64
|
+
* known and the values are heterogeneous. Whereas with `dict()` the keys are
|
|
65
|
+
* typically dynamic and the values homogeneous, like in a dictionary,
|
|
66
|
+
* a lookup table, or a cache.
|
|
67
|
+
*/
|
|
68
|
+
export function dict<T>(decoder: Decoder<T>): Decoder<{ [key: string]: T }>;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Similar to `dict()`, but returns the result as a `Map<string, T>` (an [ES6
|
|
72
|
+
* Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map))
|
|
73
|
+
* instead.
|
|
74
|
+
*/
|
|
75
|
+
export function mapping<T>(decoder: Decoder<T>): Decoder<Map<string, T>>;
|
package/lib/objects.js
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
exports.__esModule = true;
|
|
4
|
+
exports.dict = dict;
|
|
5
|
+
exports.exact = exact;
|
|
6
|
+
exports.inexact = inexact;
|
|
7
|
+
exports.mapping = mapping;
|
|
8
|
+
exports.object = object;
|
|
9
|
+
exports.pojo = void 0;
|
|
10
|
+
|
|
11
|
+
var _annotate = require("../annotate");
|
|
12
|
+
|
|
13
|
+
var _Decoder = require("../Decoder");
|
|
14
|
+
|
|
15
|
+
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
16
|
+
|
|
17
|
+
function subtract(xs, ys) {
|
|
18
|
+
var result = new Set();
|
|
19
|
+
xs.forEach(function (x) {
|
|
20
|
+
if (!ys.has(x)) {
|
|
21
|
+
result.add(x);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Accepts any "plain old JavaScript object", but doesn't validate its keys or
|
|
28
|
+
* values further.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
var pojo = (0, _Decoder.define)(function (blob, ok, err) {
|
|
33
|
+
return blob !== null && blob !== undefined && typeof blob === 'object' && // This still seems to be the only reliable way to determine whether
|
|
34
|
+
// something is a pojo... ¯\_(ツ)_/¯
|
|
35
|
+
// $FlowFixMe[method-unbinding]
|
|
36
|
+
Object.prototype.toString.call(blob) === '[object Object]' ? ok( // NOTE:
|
|
37
|
+
// Since Flow 0.98, typeof o === 'object' refines to
|
|
38
|
+
// {| +[string]: mixed |}
|
|
39
|
+
// instead of
|
|
40
|
+
// {| [string]: mixed |}
|
|
41
|
+
//
|
|
42
|
+
// For rationale, see https://github.com/facebook/flow/issues/7685.
|
|
43
|
+
// In this case, we don't want to output a read-only version of
|
|
44
|
+
// the object because it's up to the user of decoders to
|
|
45
|
+
// determine what they want to do with the decoded output. If they
|
|
46
|
+
// want to write items into the array, that's fine! The fastest
|
|
47
|
+
// way to turn a read-only Object to a writeable one in ES6 seems
|
|
48
|
+
// to be to use object-spread. (Going off this benchmark:
|
|
49
|
+
// https://thecodebarbarian.com/object-assign-vs-object-spread.html)
|
|
50
|
+
_extends({}, blob)) : err('Must be an object');
|
|
51
|
+
});
|
|
52
|
+
/**
|
|
53
|
+
* Accepts objects with fields matching the given decoders. Extra fields that
|
|
54
|
+
* exist on the input object are ignored and will not be returned.
|
|
55
|
+
*/
|
|
56
|
+
|
|
57
|
+
exports.pojo = pojo;
|
|
58
|
+
|
|
59
|
+
function object(decodersByKey) {
|
|
60
|
+
// Compute this set at decoder definition time
|
|
61
|
+
var knownKeys = new Set(Object.keys(decodersByKey));
|
|
62
|
+
return pojo.then(function (plainObj, ok, err) {
|
|
63
|
+
var actualKeys = new Set(Object.keys(plainObj)); // At this point, "missingKeys" will also include all fields that may
|
|
64
|
+
// validly be optional. We'll let the underlying decoder decide and
|
|
65
|
+
// remove the key from this missing set if the decoder accepts the
|
|
66
|
+
// value.
|
|
67
|
+
|
|
68
|
+
var missingKeys = subtract(knownKeys, actualKeys);
|
|
69
|
+
var record = {};
|
|
70
|
+
var errors = null;
|
|
71
|
+
Object.keys(decodersByKey).forEach(function (key) {
|
|
72
|
+
var decoder = decodersByKey[key];
|
|
73
|
+
var rawValue = plainObj[key];
|
|
74
|
+
var result = decoder.decode(rawValue);
|
|
75
|
+
|
|
76
|
+
if (result.ok) {
|
|
77
|
+
var value = result.value;
|
|
78
|
+
|
|
79
|
+
if (value !== undefined) {
|
|
80
|
+
record[key] = value;
|
|
81
|
+
} // If this succeeded, remove the key from the missing keys
|
|
82
|
+
// tracker
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
missingKeys["delete"](key);
|
|
86
|
+
} else {
|
|
87
|
+
var ann = result.error; // Keep track of the annotation, but don't return just yet. We
|
|
88
|
+
// want to collect more error information.
|
|
89
|
+
|
|
90
|
+
if (rawValue === undefined) {
|
|
91
|
+
// Explicitly add it to the missing set if the value is
|
|
92
|
+
// undefined. This covers explicit undefineds to be
|
|
93
|
+
// treated the same as implicit undefineds (aka missing
|
|
94
|
+
// keys).
|
|
95
|
+
missingKeys.add(key);
|
|
96
|
+
} else {
|
|
97
|
+
if (errors === null) {
|
|
98
|
+
errors = {};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
errors[key] = ann;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}); // Deal with errors now. There are two classes of errors we want to
|
|
105
|
+
// report. First of all, we want to report any inline errors in this
|
|
106
|
+
// object. Lastly, any fields that are missing should be annotated on
|
|
107
|
+
// the outer object itself.
|
|
108
|
+
|
|
109
|
+
if (errors || missingKeys.size > 0) {
|
|
110
|
+
var objAnn = (0, _annotate.annotateObject)(plainObj);
|
|
111
|
+
|
|
112
|
+
if (errors) {
|
|
113
|
+
objAnn = (0, _annotate.merge)(objAnn, errors);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (missingKeys.size > 0) {
|
|
117
|
+
var errMsg = Array.from(missingKeys).map(function (key) {
|
|
118
|
+
return "\"" + key + "\"";
|
|
119
|
+
}).join(', ');
|
|
120
|
+
var pluralized = missingKeys.size > 1 ? 'keys' : 'key';
|
|
121
|
+
objAnn = (0, _annotate.updateText)(objAnn, "Missing " + pluralized + ": " + errMsg);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return err(objAnn);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return ok(record);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Like `object()`, but will reject inputs that contain extra fields that are
|
|
132
|
+
* not specified explicitly.
|
|
133
|
+
*/
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
function exact(decodersByKey) {
|
|
137
|
+
// Compute this set at decoder definition time
|
|
138
|
+
var allowedKeys = new Set(Object.keys(decodersByKey)); // Check the inputted object for any unexpected extra keys
|
|
139
|
+
|
|
140
|
+
var checked = pojo.reject(function (plainObj) {
|
|
141
|
+
var actualKeys = new Set(Object.keys(plainObj));
|
|
142
|
+
var extraKeys = subtract(actualKeys, allowedKeys);
|
|
143
|
+
return extraKeys.size > 0 ? "Unexpected extra keys: " + Array.from(extraKeys).join(', ') : // Don't reject
|
|
144
|
+
null;
|
|
145
|
+
}); // Defer to the "object" decoder for doing the real decoding work. Since
|
|
146
|
+
// we made sure there are no superfluous keys in this structure, it's now
|
|
147
|
+
// safe to force-cast it to an $Exact<> type.
|
|
148
|
+
|
|
149
|
+
return checked.then(object(decodersByKey).decode);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Like `object()`, but will pass through any extra fields on the input object
|
|
153
|
+
* unvalidated that will thus be of `unknown` type statically.
|
|
154
|
+
*/
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
function inexact(decodersByKey) {
|
|
158
|
+
return pojo.then(function (plainObj) {
|
|
159
|
+
var allkeys = new Set(Object.keys(plainObj));
|
|
160
|
+
var decoder = object(decodersByKey).transform(function (safepart) {
|
|
161
|
+
var safekeys = new Set(Object.keys(decodersByKey)); // To account for hard-coded keys that aren't part of the input
|
|
162
|
+
|
|
163
|
+
safekeys.forEach(function (k) {
|
|
164
|
+
return allkeys.add(k);
|
|
165
|
+
});
|
|
166
|
+
var rv = {};
|
|
167
|
+
allkeys.forEach(function (k) {
|
|
168
|
+
if (safekeys.has(k)) {
|
|
169
|
+
var value = safepart[k];
|
|
170
|
+
|
|
171
|
+
if (value !== undefined) {
|
|
172
|
+
rv[k] = value;
|
|
173
|
+
}
|
|
174
|
+
} else {
|
|
175
|
+
rv[k] = plainObj[k];
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
return rv;
|
|
179
|
+
});
|
|
180
|
+
return decoder.decode(plainObj);
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Accepts objects where all values match the given decoder, and returns the
|
|
185
|
+
* result as a `{ [string]: T }`.
|
|
186
|
+
*
|
|
187
|
+
* The main difference between `object()` and `dict()` is that you'd typically
|
|
188
|
+
* use `object()` if this is a record-like object, where all field names are
|
|
189
|
+
* known and the values are heterogeneous. Whereas with `dict()` the keys are
|
|
190
|
+
* typically dynamic and the values homogeneous, like in a dictionary,
|
|
191
|
+
* a lookup table, or a cache.
|
|
192
|
+
*/
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
function dict(decoder) {
|
|
196
|
+
return pojo.then(function (plainObj, ok, err) {
|
|
197
|
+
var rv = {};
|
|
198
|
+
var errors = null;
|
|
199
|
+
Object.keys(plainObj).forEach(function (key) {
|
|
200
|
+
var value = plainObj[key];
|
|
201
|
+
var result = decoder.decode(value);
|
|
202
|
+
|
|
203
|
+
if (result.ok) {
|
|
204
|
+
if (errors === null) {
|
|
205
|
+
rv[key] = result.value;
|
|
206
|
+
}
|
|
207
|
+
} else {
|
|
208
|
+
rv = {}; // Clear the success value so it can get garbage collected early
|
|
209
|
+
|
|
210
|
+
if (errors === null) {
|
|
211
|
+
errors = {};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
errors[key] = result.error;
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
if (errors !== null) {
|
|
219
|
+
return err((0, _annotate.merge)((0, _annotate.annotateObject)(plainObj), errors));
|
|
220
|
+
} else {
|
|
221
|
+
return ok(rv);
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
/**
|
|
226
|
+
* Similar to `dict()`, but returns the result as a `Map<string, T>` (an [ES6
|
|
227
|
+
* Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map))
|
|
228
|
+
* instead.
|
|
229
|
+
*/
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
function mapping(decoder) {
|
|
233
|
+
return dict(decoder).transform(function (obj) {
|
|
234
|
+
return new Map( // This is effectively Object.entries(obj), but in a way that Flow
|
|
235
|
+
// will know the types are okay
|
|
236
|
+
Object.keys(obj).map(function (key) {
|
|
237
|
+
return [key, obj[key]];
|
|
238
|
+
}));
|
|
239
|
+
});
|
|
240
|
+
}
|