decoders 2.0.0-beta8 → 2.0.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/CHANGELOG.md +67 -20
- package/Decoder.d.ts +94 -0
- package/Decoder.js +115 -0
- package/Decoder.js.flow +286 -0
- package/Decoder.mjs +108 -0
- package/README.md +122 -1345
- package/_utils.d.ts +0 -1
- package/_utils.js +51 -68
- package/_utils.js.flow +30 -20
- package/_utils.mjs +39 -54
- package/annotate.js +79 -72
- package/annotate.mjs +62 -58
- package/format.d.ts +4 -2
- package/format.js +64 -74
- package/format.js.flow +14 -12
- package/format.mjs +58 -68
- package/index.d.ts +38 -41
- package/index.js +66 -98
- package/index.js.flow +39 -58
- package/index.mjs +11 -37
- package/{core → lib}/_helpers.d.ts +0 -0
- package/lib/arrays.d.ts +59 -0
- package/lib/arrays.js +97 -0
- package/lib/arrays.js.flow +138 -0
- package/lib/arrays.mjs +84 -0
- package/lib/basics.d.ts +93 -0
- package/lib/basics.js +94 -0
- package/lib/basics.js.flow +124 -0
- package/lib/basics.mjs +75 -0
- package/lib/booleans.d.ts +16 -0
- package/lib/booleans.js +25 -0
- package/lib/booleans.js.flow +22 -0
- package/lib/booleans.mjs +15 -0
- package/lib/dates.d.ts +15 -0
- package/lib/dates.js +28 -0
- package/lib/dates.js.flow +40 -0
- package/lib/dates.mjs +19 -0
- package/lib/json.d.ts +35 -0
- package/lib/json.js +33 -0
- package/lib/json.js.flow +50 -0
- package/lib/json.mjs +18 -0
- package/lib/numbers.d.ts +31 -0
- package/lib/numbers.js +31 -0
- package/lib/numbers.js.flow +48 -0
- package/lib/numbers.mjs +21 -0
- package/lib/objects.d.ts +76 -0
- package/lib/objects.js +161 -0
- package/lib/objects.js.flow +238 -0
- package/lib/objects.mjs +147 -0
- package/lib/strings.d.ts +56 -0
- package/lib/strings.js +58 -0
- package/lib/strings.js.flow +90 -0
- package/lib/strings.mjs +40 -0
- package/lib/unions.d.ts +55 -0
- package/lib/unions.js +96 -0
- package/lib/unions.js.flow +155 -0
- package/lib/unions.mjs +83 -0
- package/lib/utilities.d.ts +40 -0
- package/lib/utilities.js +48 -0
- package/lib/utilities.js.flow +65 -0
- package/lib/utilities.mjs +37 -0
- package/package.json +64 -17
- package/result.d.ts +2 -25
- package/result.js +9 -90
- package/result.js.flow +4 -76
- package/result.mjs +5 -71
- package/_guard.d.ts +0 -7
- package/_guard.js +0 -22
- package/_guard.js.flow +0 -20
- package/_guard.mjs +0 -15
- package/_types.d.ts +0 -13
- package/_types.js +0 -1
- package/_types.js.flow +0 -20
- package/_types.mjs +0 -0
- package/core/array.d.ts +0 -5
- package/core/array.js +0 -104
- package/core/array.js.flow +0 -100
- package/core/array.mjs +0 -91
- package/core/boolean.d.ts +0 -5
- package/core/boolean.js +0 -40
- package/core/boolean.js.flow +0 -27
- package/core/boolean.mjs +0 -28
- package/core/composition.d.ts +0 -14
- package/core/composition.js +0 -54
- package/core/composition.js.flow +0 -48
- package/core/composition.mjs +0 -44
- package/core/constants.d.ts +0 -11
- package/core/constants.js +0 -65
- package/core/constants.js.flow +0 -44
- package/core/constants.mjs +0 -46
- package/core/date.d.ts +0 -4
- package/core/date.js +0 -42
- package/core/date.js.flow +0 -38
- package/core/date.mjs +0 -28
- package/core/describe.d.ts +0 -3
- package/core/describe.js +0 -22
- package/core/describe.js.flow +0 -17
- package/core/describe.mjs +0 -16
- package/core/dispatch.d.ts +0 -8
- package/core/dispatch.js +0 -58
- package/core/dispatch.js.flow +0 -58
- package/core/dispatch.mjs +0 -51
- package/core/either.d.ts +0 -61
- package/core/either.js +0 -113
- package/core/either.js.flow +0 -151
- package/core/either.mjs +0 -90
- package/core/fail.d.ts +0 -3
- package/core/fail.js +0 -17
- package/core/fail.js.flow +0 -12
- package/core/fail.mjs +0 -11
- package/core/instanceOf.d.ts +0 -3
- package/core/instanceOf.js +0 -15
- package/core/instanceOf.js.flow +0 -20
- package/core/instanceOf.mjs +0 -8
- package/core/json.d.ts +0 -11
- package/core/json.js +0 -31
- package/core/json.js.flow +0 -28
- package/core/json.mjs +0 -15
- package/core/lazy.d.ts +0 -3
- package/core/lazy.js +0 -16
- package/core/lazy.js.flow +0 -15
- package/core/lazy.mjs +0 -11
- package/core/mapping.d.ts +0 -6
- package/core/mapping.js +0 -67
- package/core/mapping.js.flow +0 -62
- package/core/mapping.mjs +0 -58
- package/core/number.d.ts +0 -6
- package/core/number.js +0 -36
- package/core/number.js.flow +0 -40
- package/core/number.mjs +0 -25
- package/core/object.d.ts +0 -33
- package/core/object.js +0 -190
- package/core/object.js.flow +0 -203
- package/core/object.mjs +0 -175
- package/core/optional.d.ts +0 -5
- package/core/optional.js +0 -50
- package/core/optional.js.flow +0 -41
- package/core/optional.mjs +0 -38
- package/core/string.d.ts +0 -10
- package/core/string.js +0 -68
- package/core/string.js.flow +0 -59
- package/core/string.mjs +0 -49
- package/core/tuple.d.ts +0 -30
- package/core/tuple.js +0 -177
- package/core/tuple.js.flow +0 -211
- package/core/tuple.mjs +0 -159
package/core/number.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.positiveNumber = exports.positiveInteger = exports.number = exports.integer = void 0;
|
|
5
|
-
|
|
6
|
-
var _annotate = require("../annotate");
|
|
7
|
-
|
|
8
|
-
var _result = require("../result");
|
|
9
|
-
|
|
10
|
-
var _composition = require("./composition");
|
|
11
|
-
|
|
12
|
-
var anyNumber = function anyNumber(blob) {
|
|
13
|
-
return typeof blob === 'number' && !Number.isNaN(blob) ? (0, _result.ok)(blob) : (0, _result.err)((0, _annotate.annotate)(blob, 'Must be number'));
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
var isInteger = function isInteger(n) {
|
|
17
|
-
return Number.isInteger(n);
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
var isFinite = function isFinite(n) {
|
|
21
|
-
return Number.isFinite(n);
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
var number = (0, _composition.predicate)(anyNumber, isFinite, 'Number must be finite');
|
|
25
|
-
exports.number = number;
|
|
26
|
-
var positiveNumber = (0, _composition.predicate)(number, function (n) {
|
|
27
|
-
return n >= 0;
|
|
28
|
-
}, 'Number must be positive'); // Integers
|
|
29
|
-
|
|
30
|
-
exports.positiveNumber = positiveNumber;
|
|
31
|
-
var integer = (0, _composition.predicate)(number, isInteger, 'Number must be an integer');
|
|
32
|
-
exports.integer = integer;
|
|
33
|
-
var positiveInteger = (0, _composition.predicate)(integer, function (n) {
|
|
34
|
-
return n >= 0;
|
|
35
|
-
}, 'Number must be positive');
|
|
36
|
-
exports.positiveInteger = positiveInteger;
|
package/core/number.js.flow
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { annotate } from '../annotate';
|
|
4
|
-
import { err, ok } from '../result';
|
|
5
|
-
import { predicate } from './composition';
|
|
6
|
-
import type { Decoder } from '../_types';
|
|
7
|
-
|
|
8
|
-
const anyNumber: Decoder<number> = (blob: mixed) => {
|
|
9
|
-
return typeof blob === 'number' && !Number.isNaN(blob)
|
|
10
|
-
? ok(blob)
|
|
11
|
-
: err(annotate(blob, 'Must be number'));
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
const isInteger = (n: number) => Number.isInteger(n);
|
|
15
|
-
const isFinite = (n: number) => Number.isFinite(n);
|
|
16
|
-
|
|
17
|
-
export const number: Decoder<number> = predicate(
|
|
18
|
-
anyNumber,
|
|
19
|
-
isFinite,
|
|
20
|
-
'Number must be finite',
|
|
21
|
-
);
|
|
22
|
-
|
|
23
|
-
export const positiveNumber: Decoder<number> = predicate(
|
|
24
|
-
number,
|
|
25
|
-
(n) => n >= 0,
|
|
26
|
-
'Number must be positive',
|
|
27
|
-
);
|
|
28
|
-
|
|
29
|
-
// Integers
|
|
30
|
-
export const integer: Decoder<number> = predicate(
|
|
31
|
-
number,
|
|
32
|
-
isInteger,
|
|
33
|
-
'Number must be an integer',
|
|
34
|
-
);
|
|
35
|
-
|
|
36
|
-
export const positiveInteger: Decoder<number> = predicate(
|
|
37
|
-
integer,
|
|
38
|
-
(n) => n >= 0,
|
|
39
|
-
'Number must be positive',
|
|
40
|
-
);
|
package/core/number.mjs
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { annotate } from '../annotate.mjs';
|
|
2
|
-
import { err, ok } from '../result.mjs';
|
|
3
|
-
import { predicate } from './composition.mjs';
|
|
4
|
-
|
|
5
|
-
var anyNumber = function anyNumber(blob) {
|
|
6
|
-
return typeof blob === 'number' && !Number.isNaN(blob) ? ok(blob) : err(annotate(blob, 'Must be number'));
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
var isInteger = function isInteger(n) {
|
|
10
|
-
return Number.isInteger(n);
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
var isFinite = function isFinite(n) {
|
|
14
|
-
return Number.isFinite(n);
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
export var number = predicate(anyNumber, isFinite, 'Number must be finite');
|
|
18
|
-
export var positiveNumber = predicate(number, function (n) {
|
|
19
|
-
return n >= 0;
|
|
20
|
-
}, 'Number must be positive'); // Integers
|
|
21
|
-
|
|
22
|
-
export var integer = predicate(number, isInteger, 'Number must be an integer');
|
|
23
|
-
export var positiveInteger = predicate(integer, function (n) {
|
|
24
|
-
return n >= 0;
|
|
25
|
-
}, 'Number must be positive');
|
package/core/object.d.ts
DELETED
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import { Decoder, DecoderType } from '../_types';
|
|
2
|
-
import { AllowImplicit } from './_helpers';
|
|
3
|
-
|
|
4
|
-
export type ObjectDecoderType<T> = AllowImplicit<{
|
|
5
|
-
[key in keyof T]: DecoderType<T[key]>;
|
|
6
|
-
}>;
|
|
7
|
-
|
|
8
|
-
export const pojo: Decoder<{ [key: string]: unknown }>;
|
|
9
|
-
|
|
10
|
-
export function object<O extends { [key: string]: Decoder<any> }>(
|
|
11
|
-
mapping: O,
|
|
12
|
-
): Decoder<{ [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K] }>;
|
|
13
|
-
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
14
|
-
// This is basically just equivalent to:
|
|
15
|
-
// ObjectDecoderType<O>
|
|
16
|
-
//
|
|
17
|
-
// But by "resolving" this with a mapped type, we remove the helper
|
|
18
|
-
// type names from the inferred type here, making this much easier to
|
|
19
|
-
// work with while developing.
|
|
20
|
-
|
|
21
|
-
export function exact<O extends { [key: string]: Decoder<any> }>(
|
|
22
|
-
mapping: O,
|
|
23
|
-
): Decoder<{ [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K] }>;
|
|
24
|
-
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
25
|
-
// Ditto (see above)
|
|
26
|
-
|
|
27
|
-
export function inexact<O extends { [key: string]: Decoder<any> }>(
|
|
28
|
-
mapping: O,
|
|
29
|
-
): Decoder<
|
|
30
|
-
{ [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K] } & {
|
|
31
|
-
[extra: string]: unknown;
|
|
32
|
-
}
|
|
33
|
-
>;
|
package/core/object.js
DELETED
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true;
|
|
4
|
-
exports.exact = exact;
|
|
5
|
-
exports.inexact = inexact;
|
|
6
|
-
exports.object = object;
|
|
7
|
-
exports.pojo = void 0;
|
|
8
|
-
|
|
9
|
-
var _annotate = require("../annotate");
|
|
10
|
-
|
|
11
|
-
var _composition = require("./composition");
|
|
12
|
-
|
|
13
|
-
var _result = require("../result");
|
|
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 isPojo(o) {
|
|
18
|
-
return o !== null && o !== undefined && typeof o === 'object' && // This still seems to be the only reliable way to determine whether
|
|
19
|
-
// something is a pojo... ¯\_(ツ)_/¯
|
|
20
|
-
// $FlowFixMe[method-unbinding]
|
|
21
|
-
Object.prototype.toString.call(o) === '[object Object]';
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function subtract(xs, ys) {
|
|
25
|
-
var result = new Set();
|
|
26
|
-
xs.forEach(function (x) {
|
|
27
|
-
if (!ys.has(x)) {
|
|
28
|
-
result.add(x);
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
return result;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
var pojo = function pojo(blob) {
|
|
35
|
-
return isPojo(blob) ? (0, _result.ok)( // NOTE:
|
|
36
|
-
// Since Flow 0.98, typeof o === 'object' refines to
|
|
37
|
-
// {| +[string]: mixed |}
|
|
38
|
-
// instead of
|
|
39
|
-
// {| [string]: mixed |}
|
|
40
|
-
//
|
|
41
|
-
// For rationale, see https://github.com/facebook/flow/issues/7685.
|
|
42
|
-
// In this case, we don't want to output a read-only version of
|
|
43
|
-
// the object because it's up to the user of decoders to
|
|
44
|
-
// determine what they want to do with the decoded output. If they
|
|
45
|
-
// want to write items into the array, that's fine! The fastest
|
|
46
|
-
// way to turn a read-only Object to a writeable one in ES6 seems
|
|
47
|
-
// to be to use object-spread. (Going off this benchmark:
|
|
48
|
-
// https://thecodebarbarian.com/object-assign-vs-object-spread.html)
|
|
49
|
-
_extends({}, blob)) : (0, _result.err)((0, _annotate.annotate)(blob, 'Must be an object'));
|
|
50
|
-
};
|
|
51
|
-
/**
|
|
52
|
-
* Given a mapping of fields-to-decoders, builds a decoder for an object type.
|
|
53
|
-
*
|
|
54
|
-
* For example, given decoders for a number and a string, we can construct an
|
|
55
|
-
* "object description" like so:
|
|
56
|
-
*
|
|
57
|
-
* { id: number, name: string }
|
|
58
|
-
*
|
|
59
|
-
* Which is of type:
|
|
60
|
-
*
|
|
61
|
-
* { id: Decoder<number>, name: Decoder<string> }
|
|
62
|
-
*
|
|
63
|
-
* Passing this to object() will produce the following return type:
|
|
64
|
-
*
|
|
65
|
-
* Decoder<{ id: number, name: string }>
|
|
66
|
-
*
|
|
67
|
-
* Put simply: it'll "peel off" all of the nested Decoders, puts them together
|
|
68
|
-
* in an object, and wraps it in a Decoder<...>.
|
|
69
|
-
*/
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
exports.pojo = pojo;
|
|
73
|
-
|
|
74
|
-
function object(mapping) {
|
|
75
|
-
var known = new Set(Object.keys(mapping));
|
|
76
|
-
return (0, _composition.compose)(pojo, function (blob) {
|
|
77
|
-
var actual = new Set(Object.keys(blob)); // At this point, "missing" will also include all fields that may
|
|
78
|
-
// validly be optional. We'll let the underlying decoder decide and
|
|
79
|
-
// remove the key from this missing set if the decoder accepts the
|
|
80
|
-
// value.
|
|
81
|
-
|
|
82
|
-
var missing = subtract(known, actual);
|
|
83
|
-
var record = {};
|
|
84
|
-
var errors = null;
|
|
85
|
-
Object.keys(mapping).forEach(function (key) {
|
|
86
|
-
var decoder = mapping[key];
|
|
87
|
-
var rawValue = blob[key];
|
|
88
|
-
var result = decoder(rawValue);
|
|
89
|
-
|
|
90
|
-
if (result.type === 'ok') {
|
|
91
|
-
var value = result.value;
|
|
92
|
-
|
|
93
|
-
if (value !== undefined) {
|
|
94
|
-
record[key] = value;
|
|
95
|
-
} // If this succeeded, remove the key from the missing keys
|
|
96
|
-
// tracker
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
missing["delete"](key);
|
|
100
|
-
} else {
|
|
101
|
-
var ann = result.error; // Keep track of the annotation, but don't return just yet. We
|
|
102
|
-
// want to collect more error information.
|
|
103
|
-
|
|
104
|
-
if (rawValue === undefined) {
|
|
105
|
-
// Explicitly add it to the missing set if the value is
|
|
106
|
-
// undefined. This covers explicit undefineds to be
|
|
107
|
-
// treated the same as implicit undefineds (aka missing
|
|
108
|
-
// keys).
|
|
109
|
-
missing.add(key);
|
|
110
|
-
} else {
|
|
111
|
-
if (errors === null) {
|
|
112
|
-
errors = {};
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
errors[key] = ann;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}); // Deal with errors now. There are two classes of errors we want to
|
|
119
|
-
// report. First of all, we want to report any inline errors in this
|
|
120
|
-
// object. Lastly, any fields that are missing should be annotated on
|
|
121
|
-
// the outer object itself.
|
|
122
|
-
|
|
123
|
-
if (errors || missing.size > 0) {
|
|
124
|
-
var objAnn = (0, _annotate.annotateObject)(blob);
|
|
125
|
-
|
|
126
|
-
if (errors) {
|
|
127
|
-
objAnn = (0, _annotate.merge)(objAnn, errors);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
if (missing.size > 0) {
|
|
131
|
-
var errMsg = Array.from(missing).map(function (key) {
|
|
132
|
-
return "\"" + key + "\"";
|
|
133
|
-
}).join(', ');
|
|
134
|
-
var pluralized = missing.size > 1 ? 'keys' : 'key';
|
|
135
|
-
objAnn = (0, _annotate.updateText)(objAnn, "Missing " + pluralized + ": " + errMsg);
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
return (0, _result.err)(objAnn);
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
return (0, _result.ok)(record);
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
function exact(mapping) {
|
|
146
|
-
// Check the inputted object for any superfluous keys
|
|
147
|
-
var allowed = new Set(Object.keys(mapping));
|
|
148
|
-
var checked = (0, _composition.compose)(pojo, function (blob) {
|
|
149
|
-
var actual = new Set(Object.keys(blob));
|
|
150
|
-
var superfluous = subtract(actual, allowed);
|
|
151
|
-
|
|
152
|
-
if (superfluous.size > 0) {
|
|
153
|
-
return (0, _result.err)((0, _annotate.annotate)(blob, "Superfluous keys: " + Array.from(superfluous).join(', ')));
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
return (0, _result.ok)(blob);
|
|
157
|
-
}); // Defer to the "object" decoder for doing the real decoding work. Since
|
|
158
|
-
// we made sure there are no superfluous keys in this structure, it's now
|
|
159
|
-
// safe to force-cast it to an $Exact<> type.
|
|
160
|
-
|
|
161
|
-
var decoder = object(mapping);
|
|
162
|
-
return (0, _composition.compose)(checked, decoder);
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
function inexact(mapping) {
|
|
166
|
-
return (0, _composition.compose)(pojo, function (blob) {
|
|
167
|
-
var allkeys = new Set(Object.keys(blob));
|
|
168
|
-
var decoder = (0, _composition.map)(object(mapping), function (safepart) {
|
|
169
|
-
var safekeys = new Set(Object.keys(mapping)); // To account for hard-coded keys that aren't part of the input
|
|
170
|
-
|
|
171
|
-
safekeys.forEach(function (k) {
|
|
172
|
-
return allkeys.add(k);
|
|
173
|
-
});
|
|
174
|
-
var rv = {};
|
|
175
|
-
allkeys.forEach(function (k) {
|
|
176
|
-
if (safekeys.has(k)) {
|
|
177
|
-
var value = safepart[k];
|
|
178
|
-
|
|
179
|
-
if (value !== undefined) {
|
|
180
|
-
rv[k] = value;
|
|
181
|
-
}
|
|
182
|
-
} else {
|
|
183
|
-
rv[k] = blob[k];
|
|
184
|
-
}
|
|
185
|
-
});
|
|
186
|
-
return rv;
|
|
187
|
-
});
|
|
188
|
-
return decoder(blob);
|
|
189
|
-
});
|
|
190
|
-
}
|
package/core/object.js.flow
DELETED
|
@@ -1,203 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { annotate, annotateObject, merge, updateText } from '../annotate';
|
|
4
|
-
import { compose, map } from './composition';
|
|
5
|
-
import { err, ok } from '../result';
|
|
6
|
-
import type { Annotation } from '../annotate';
|
|
7
|
-
import type { Decoder, DecoderType } from '../_types';
|
|
8
|
-
|
|
9
|
-
// $FlowFixMe[unclear-type] (not really an issue) - deliberate use of `any` - not sure how we should get rid of this
|
|
10
|
-
type AnyDecoder = any;
|
|
11
|
-
|
|
12
|
-
// $FlowFixMe[unclear-type] (not really an issue) - deliberately casting
|
|
13
|
-
type cast = any;
|
|
14
|
-
|
|
15
|
-
function isPojo(o: mixed): boolean %checks {
|
|
16
|
-
return (
|
|
17
|
-
o !== null &&
|
|
18
|
-
o !== undefined &&
|
|
19
|
-
typeof o === 'object' &&
|
|
20
|
-
// This still seems to be the only reliable way to determine whether
|
|
21
|
-
// something is a pojo... ¯\_(ツ)_/¯
|
|
22
|
-
// $FlowFixMe[method-unbinding]
|
|
23
|
-
Object.prototype.toString.call(o) === '[object Object]'
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
function subtract(xs: Set<string>, ys: Set<string>): Set<string> {
|
|
28
|
-
const result = new Set();
|
|
29
|
-
xs.forEach((x) => {
|
|
30
|
-
if (!ys.has(x)) {
|
|
31
|
-
result.add(x);
|
|
32
|
-
}
|
|
33
|
-
});
|
|
34
|
-
return result;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export const pojo: Decoder<{| [string]: mixed |}> = (blob: mixed) => {
|
|
38
|
-
return isPojo(blob)
|
|
39
|
-
? ok(
|
|
40
|
-
// NOTE:
|
|
41
|
-
// Since Flow 0.98, typeof o === 'object' refines to
|
|
42
|
-
// {| +[string]: mixed |}
|
|
43
|
-
// instead of
|
|
44
|
-
// {| [string]: mixed |}
|
|
45
|
-
//
|
|
46
|
-
// For rationale, see https://github.com/facebook/flow/issues/7685.
|
|
47
|
-
// In this case, we don't want to output a read-only version of
|
|
48
|
-
// the object because it's up to the user of decoders to
|
|
49
|
-
// determine what they want to do with the decoded output. If they
|
|
50
|
-
// want to write items into the array, that's fine! The fastest
|
|
51
|
-
// way to turn a read-only Object to a writeable one in ES6 seems
|
|
52
|
-
// to be to use object-spread. (Going off this benchmark:
|
|
53
|
-
// https://thecodebarbarian.com/object-assign-vs-object-spread.html)
|
|
54
|
-
{ ...blob },
|
|
55
|
-
)
|
|
56
|
-
: err(annotate(blob, 'Must be an object'));
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Given a mapping of fields-to-decoders, builds a decoder for an object type.
|
|
61
|
-
*
|
|
62
|
-
* For example, given decoders for a number and a string, we can construct an
|
|
63
|
-
* "object description" like so:
|
|
64
|
-
*
|
|
65
|
-
* { id: number, name: string }
|
|
66
|
-
*
|
|
67
|
-
* Which is of type:
|
|
68
|
-
*
|
|
69
|
-
* { id: Decoder<number>, name: Decoder<string> }
|
|
70
|
-
*
|
|
71
|
-
* Passing this to object() will produce the following return type:
|
|
72
|
-
*
|
|
73
|
-
* Decoder<{ id: number, name: string }>
|
|
74
|
-
*
|
|
75
|
-
* Put simply: it'll "peel off" all of the nested Decoders, puts them together
|
|
76
|
-
* in an object, and wraps it in a Decoder<...>.
|
|
77
|
-
*/
|
|
78
|
-
export function object<O: { +[field: string]: AnyDecoder, ... }>(
|
|
79
|
-
mapping: O,
|
|
80
|
-
): Decoder<$ObjMap<O, DecoderType>> {
|
|
81
|
-
const known = new Set(Object.keys(mapping));
|
|
82
|
-
return compose(pojo, (blob: { +[string]: mixed }) => {
|
|
83
|
-
const actual = new Set(Object.keys(blob));
|
|
84
|
-
|
|
85
|
-
// At this point, "missing" will also include all fields that may
|
|
86
|
-
// validly be optional. We'll let the underlying decoder decide and
|
|
87
|
-
// remove the key from this missing set if the decoder accepts the
|
|
88
|
-
// value.
|
|
89
|
-
const missing = subtract(known, actual);
|
|
90
|
-
|
|
91
|
-
let record = {};
|
|
92
|
-
let errors: { [key: string]: Annotation } | null = null;
|
|
93
|
-
|
|
94
|
-
Object.keys(mapping).forEach((key) => {
|
|
95
|
-
const decoder = mapping[key];
|
|
96
|
-
const rawValue = blob[key];
|
|
97
|
-
const result = decoder(rawValue);
|
|
98
|
-
|
|
99
|
-
if (result.type === 'ok') {
|
|
100
|
-
const value = result.value;
|
|
101
|
-
if (value !== undefined) {
|
|
102
|
-
record[key] = value;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
// If this succeeded, remove the key from the missing keys
|
|
106
|
-
// tracker
|
|
107
|
-
missing.delete(key);
|
|
108
|
-
} else {
|
|
109
|
-
const ann = result.error;
|
|
110
|
-
|
|
111
|
-
// Keep track of the annotation, but don't return just yet. We
|
|
112
|
-
// want to collect more error information.
|
|
113
|
-
if (rawValue === undefined) {
|
|
114
|
-
// Explicitly add it to the missing set if the value is
|
|
115
|
-
// undefined. This covers explicit undefineds to be
|
|
116
|
-
// treated the same as implicit undefineds (aka missing
|
|
117
|
-
// keys).
|
|
118
|
-
missing.add(key);
|
|
119
|
-
} else {
|
|
120
|
-
if (errors === null) {
|
|
121
|
-
errors = {};
|
|
122
|
-
}
|
|
123
|
-
errors[key] = ann;
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
// Deal with errors now. There are two classes of errors we want to
|
|
129
|
-
// report. First of all, we want to report any inline errors in this
|
|
130
|
-
// object. Lastly, any fields that are missing should be annotated on
|
|
131
|
-
// the outer object itself.
|
|
132
|
-
if (errors || missing.size > 0) {
|
|
133
|
-
let objAnn = annotateObject(blob);
|
|
134
|
-
|
|
135
|
-
if (errors) {
|
|
136
|
-
objAnn = merge(objAnn, errors);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
if (missing.size > 0) {
|
|
140
|
-
const errMsg = Array.from(missing)
|
|
141
|
-
.map((key) => `"${key}"`)
|
|
142
|
-
.join(', ');
|
|
143
|
-
const pluralized = missing.size > 1 ? 'keys' : 'key';
|
|
144
|
-
objAnn = updateText(objAnn, `Missing ${pluralized}: ${errMsg}`);
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return err(objAnn);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
return ok(record);
|
|
151
|
-
});
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
export function exact<O: { +[field: string]: AnyDecoder, ... }>(
|
|
155
|
-
mapping: O,
|
|
156
|
-
): Decoder<$ObjMap<$Exact<O>, DecoderType>> {
|
|
157
|
-
// Check the inputted object for any superfluous keys
|
|
158
|
-
const allowed = new Set(Object.keys(mapping));
|
|
159
|
-
const checked = compose(pojo, (blob: {| [string]: mixed |}) => {
|
|
160
|
-
const actual = new Set(Object.keys(blob));
|
|
161
|
-
const superfluous = subtract(actual, allowed);
|
|
162
|
-
if (superfluous.size > 0) {
|
|
163
|
-
return err(
|
|
164
|
-
annotate(blob, `Superfluous keys: ${Array.from(superfluous).join(', ')}`),
|
|
165
|
-
);
|
|
166
|
-
}
|
|
167
|
-
return ok(blob);
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
// Defer to the "object" decoder for doing the real decoding work. Since
|
|
171
|
-
// we made sure there are no superfluous keys in this structure, it's now
|
|
172
|
-
// safe to force-cast it to an $Exact<> type.
|
|
173
|
-
const decoder = ((object(mapping): cast): Decoder<$ObjMap<$Exact<O>, DecoderType>>);
|
|
174
|
-
return compose(checked, decoder);
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
export function inexact<O: { +[field: string]: AnyDecoder }>(
|
|
178
|
-
mapping: O,
|
|
179
|
-
): Decoder<$ObjMap<O, DecoderType> & { +[string]: mixed }> {
|
|
180
|
-
return compose(pojo, (blob: {| [string]: mixed |}) => {
|
|
181
|
-
const allkeys = new Set(Object.keys(blob));
|
|
182
|
-
const decoder = map(object(mapping), (safepart: $ObjMap<O, DecoderType>) => {
|
|
183
|
-
const safekeys = new Set(Object.keys(mapping));
|
|
184
|
-
|
|
185
|
-
// To account for hard-coded keys that aren't part of the input
|
|
186
|
-
safekeys.forEach((k) => allkeys.add(k));
|
|
187
|
-
|
|
188
|
-
const rv = {};
|
|
189
|
-
allkeys.forEach((k) => {
|
|
190
|
-
if (safekeys.has(k)) {
|
|
191
|
-
const value = safepart[k];
|
|
192
|
-
if (value !== undefined) {
|
|
193
|
-
rv[k] = value;
|
|
194
|
-
}
|
|
195
|
-
} else {
|
|
196
|
-
rv[k] = blob[k];
|
|
197
|
-
}
|
|
198
|
-
});
|
|
199
|
-
return rv;
|
|
200
|
-
});
|
|
201
|
-
return decoder(blob);
|
|
202
|
-
});
|
|
203
|
-
}
|