decoders 2.0.0 → 2.0.2
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 +16 -0
- package/Decoder.js +41 -148
- package/Decoder.js.flow +1 -1
- package/Decoder.mjs +38 -145
- package/README.md +1 -1
- package/_utils.js +50 -61
- package/_utils.js.flow +18 -4
- package/_utils.mjs +41 -52
- package/annotate.js +79 -72
- package/annotate.mjs +62 -58
- package/format.d.ts +2 -2
- package/format.js +64 -74
- package/format.js.flow +11 -11
- package/format.mjs +58 -68
- package/index.js +66 -66
- package/index.mjs +11 -11
- package/lib/arrays.js +47 -89
- package/lib/arrays.mjs +38 -78
- package/lib/basics.js +59 -109
- package/lib/basics.mjs +49 -94
- package/lib/booleans.js +14 -24
- package/lib/booleans.mjs +9 -19
- package/lib/dates.js +18 -34
- package/lib/dates.mjs +12 -27
- package/lib/json.d.ts +3 -3
- package/lib/json.js +20 -42
- package/lib/json.js.flow +2 -2
- package/lib/json.mjs +13 -35
- package/lib/numbers.js +19 -39
- package/lib/numbers.mjs +11 -31
- package/lib/objects.d.ts +11 -10
- package/lib/objects.js +84 -163
- package/lib/objects.js.flow +4 -12
- package/lib/objects.mjs +74 -150
- package/lib/strings.js +41 -84
- package/lib/strings.mjs +25 -67
- package/lib/unions.d.ts +1 -1
- package/lib/unions.js +52 -116
- package/lib/unions.js.flow +1 -1
- package/lib/unions.mjs +46 -109
- package/lib/utilities.d.ts +7 -1
- package/lib/utilities.js +23 -50
- package/lib/utilities.mjs +15 -38
- package/package.json +1 -1
- package/result.js +9 -22
- package/result.mjs +5 -17
package/lib/objects.js
CHANGED
|
@@ -1,240 +1,161 @@
|
|
|
1
|
-
|
|
1
|
+
'use strict'
|
|
2
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
|
|
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
10
|
|
|
11
|
-
var _annotate = require(
|
|
11
|
+
var _annotate = require('../annotate')
|
|
12
12
|
|
|
13
|
-
var _Decoder = require(
|
|
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
|
-
*/
|
|
13
|
+
var _Decoder = require('../Decoder')
|
|
30
14
|
|
|
15
|
+
var _utils = require('../_utils')
|
|
31
16
|
|
|
32
17
|
var pojo = (0, _Decoder.define)(function (blob, ok, err) {
|
|
33
|
-
return blob !== null && blob !== undefined && typeof blob === 'object' &&
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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;
|
|
18
|
+
return blob !== null && blob !== undefined && typeof blob === 'object' && Object.prototype.toString.call(blob) === '[object Object]' ? ok(blob) : err('Must be an object')
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
exports.pojo = pojo
|
|
58
22
|
|
|
59
23
|
function object(decodersByKey) {
|
|
60
|
-
|
|
61
|
-
var knownKeys = new Set(Object.keys(decodersByKey));
|
|
24
|
+
var knownKeys = new Set(Object.keys(decodersByKey))
|
|
62
25
|
return pojo.then(function (plainObj, ok, err) {
|
|
63
|
-
var actualKeys = new Set(Object.keys(plainObj))
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
var missingKeys = subtract(knownKeys, actualKeys);
|
|
69
|
-
var record = {};
|
|
70
|
-
var errors = null;
|
|
26
|
+
var actualKeys = new Set(Object.keys(plainObj))
|
|
27
|
+
|
|
28
|
+
var missingKeys = (0, _utils.subtract)(knownKeys, actualKeys)
|
|
29
|
+
var record = {}
|
|
30
|
+
var errors = null
|
|
71
31
|
Object.keys(decodersByKey).forEach(function (key) {
|
|
72
|
-
var decoder = decodersByKey[key]
|
|
73
|
-
var rawValue = plainObj[key]
|
|
74
|
-
var result = decoder.decode(rawValue)
|
|
32
|
+
var decoder = decodersByKey[key]
|
|
33
|
+
var rawValue = plainObj[key]
|
|
34
|
+
var result = decoder.decode(rawValue)
|
|
75
35
|
|
|
76
36
|
if (result.ok) {
|
|
77
|
-
var value = result.value
|
|
37
|
+
var value = result.value
|
|
78
38
|
|
|
79
39
|
if (value !== undefined) {
|
|
80
|
-
record[key] = value
|
|
81
|
-
}
|
|
82
|
-
// tracker
|
|
83
|
-
|
|
40
|
+
record[key] = value
|
|
41
|
+
}
|
|
84
42
|
|
|
85
|
-
missingKeys[
|
|
43
|
+
missingKeys['delete'](key)
|
|
86
44
|
} else {
|
|
87
|
-
var ann = result.error
|
|
88
|
-
// want to collect more error information.
|
|
45
|
+
var ann = result.error
|
|
89
46
|
|
|
90
47
|
if (rawValue === undefined) {
|
|
91
|
-
|
|
92
|
-
// undefined. This covers explicit undefineds to be
|
|
93
|
-
// treated the same as implicit undefineds (aka missing
|
|
94
|
-
// keys).
|
|
95
|
-
missingKeys.add(key);
|
|
48
|
+
missingKeys.add(key)
|
|
96
49
|
} else {
|
|
97
50
|
if (errors === null) {
|
|
98
|
-
errors = {}
|
|
51
|
+
errors = {}
|
|
99
52
|
}
|
|
100
53
|
|
|
101
|
-
errors[key] = ann
|
|
54
|
+
errors[key] = ann
|
|
102
55
|
}
|
|
103
56
|
}
|
|
104
|
-
})
|
|
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.
|
|
57
|
+
})
|
|
108
58
|
|
|
109
59
|
if (errors || missingKeys.size > 0) {
|
|
110
|
-
var objAnn = (0, _annotate.annotateObject)(plainObj)
|
|
60
|
+
var objAnn = (0, _annotate.annotateObject)(plainObj)
|
|
111
61
|
|
|
112
62
|
if (errors) {
|
|
113
|
-
objAnn = (0, _annotate.merge)(objAnn, errors)
|
|
63
|
+
objAnn = (0, _annotate.merge)(objAnn, errors)
|
|
114
64
|
}
|
|
115
65
|
|
|
116
66
|
if (missingKeys.size > 0) {
|
|
117
|
-
var errMsg = Array.from(missingKeys)
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
67
|
+
var errMsg = Array.from(missingKeys)
|
|
68
|
+
.map(function (key) {
|
|
69
|
+
return '"' + key + '"'
|
|
70
|
+
})
|
|
71
|
+
.join(', ')
|
|
72
|
+
var pluralized = missingKeys.size > 1 ? 'keys' : 'key'
|
|
73
|
+
objAnn = (0, _annotate.updateText)(objAnn, 'Missing ' + pluralized + ': ' + errMsg)
|
|
122
74
|
}
|
|
123
75
|
|
|
124
|
-
return err(objAnn)
|
|
76
|
+
return err(objAnn)
|
|
125
77
|
}
|
|
126
78
|
|
|
127
|
-
return ok(record)
|
|
128
|
-
})
|
|
79
|
+
return ok(record)
|
|
80
|
+
})
|
|
129
81
|
}
|
|
130
|
-
/**
|
|
131
|
-
* Like `object()`, but will reject inputs that contain extra fields that are
|
|
132
|
-
* not specified explicitly.
|
|
133
|
-
*/
|
|
134
|
-
|
|
135
82
|
|
|
136
83
|
function exact(decodersByKey) {
|
|
137
|
-
|
|
138
|
-
var allowedKeys = new Set(Object.keys(decodersByKey)); // Check the inputted object for any unexpected extra keys
|
|
84
|
+
var allowedKeys = new Set(Object.keys(decodersByKey))
|
|
139
85
|
|
|
140
86
|
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 ?
|
|
144
|
-
|
|
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
|
-
*/
|
|
87
|
+
var actualKeys = new Set(Object.keys(plainObj))
|
|
88
|
+
var extraKeys = (0, _utils.subtract)(actualKeys, allowedKeys)
|
|
89
|
+
return extraKeys.size > 0 ? 'Unexpected extra keys: ' + Array.from(extraKeys).join(', ') : null
|
|
90
|
+
})
|
|
155
91
|
|
|
92
|
+
return checked.then(object(decodersByKey).decode)
|
|
93
|
+
}
|
|
156
94
|
|
|
157
95
|
function inexact(decodersByKey) {
|
|
158
96
|
return pojo.then(function (plainObj) {
|
|
159
|
-
var allkeys = new Set(Object.keys(plainObj))
|
|
97
|
+
var allkeys = new Set(Object.keys(plainObj))
|
|
160
98
|
var decoder = object(decodersByKey).transform(function (safepart) {
|
|
161
|
-
var safekeys = new Set(Object.keys(decodersByKey))
|
|
99
|
+
var safekeys = new Set(Object.keys(decodersByKey))
|
|
162
100
|
|
|
163
101
|
safekeys.forEach(function (k) {
|
|
164
|
-
return allkeys.add(k)
|
|
165
|
-
})
|
|
166
|
-
var rv = {}
|
|
102
|
+
return allkeys.add(k)
|
|
103
|
+
})
|
|
104
|
+
var rv = {}
|
|
167
105
|
allkeys.forEach(function (k) {
|
|
168
106
|
if (safekeys.has(k)) {
|
|
169
|
-
var value = safepart[k]
|
|
107
|
+
var value = safepart[k]
|
|
170
108
|
|
|
171
109
|
if (value !== undefined) {
|
|
172
|
-
rv[k] = value
|
|
110
|
+
rv[k] = value
|
|
173
111
|
}
|
|
174
112
|
} else {
|
|
175
|
-
rv[k] = plainObj[k]
|
|
113
|
+
rv[k] = plainObj[k]
|
|
176
114
|
}
|
|
177
|
-
})
|
|
178
|
-
return rv
|
|
179
|
-
})
|
|
180
|
-
return decoder.decode(plainObj)
|
|
181
|
-
})
|
|
115
|
+
})
|
|
116
|
+
return rv
|
|
117
|
+
})
|
|
118
|
+
return decoder.decode(plainObj)
|
|
119
|
+
})
|
|
182
120
|
}
|
|
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
121
|
|
|
195
122
|
function dict(decoder) {
|
|
196
123
|
return pojo.then(function (plainObj, ok, err) {
|
|
197
|
-
var rv = {}
|
|
198
|
-
var errors = null
|
|
124
|
+
var rv = {}
|
|
125
|
+
var errors = null
|
|
199
126
|
Object.keys(plainObj).forEach(function (key) {
|
|
200
|
-
var value = plainObj[key]
|
|
201
|
-
var result = decoder.decode(value)
|
|
127
|
+
var value = plainObj[key]
|
|
128
|
+
var result = decoder.decode(value)
|
|
202
129
|
|
|
203
130
|
if (result.ok) {
|
|
204
131
|
if (errors === null) {
|
|
205
|
-
rv[key] = result.value
|
|
132
|
+
rv[key] = result.value
|
|
206
133
|
}
|
|
207
134
|
} else {
|
|
208
|
-
rv = {}
|
|
135
|
+
rv = {}
|
|
209
136
|
|
|
210
137
|
if (errors === null) {
|
|
211
|
-
errors = {}
|
|
138
|
+
errors = {}
|
|
212
139
|
}
|
|
213
140
|
|
|
214
|
-
errors[key] = result.error
|
|
141
|
+
errors[key] = result.error
|
|
215
142
|
}
|
|
216
|
-
})
|
|
143
|
+
})
|
|
217
144
|
|
|
218
145
|
if (errors !== null) {
|
|
219
|
-
return err((0, _annotate.merge)((0, _annotate.annotateObject)(plainObj), errors))
|
|
146
|
+
return err((0, _annotate.merge)((0, _annotate.annotateObject)(plainObj), errors))
|
|
220
147
|
} else {
|
|
221
|
-
return ok(rv)
|
|
148
|
+
return ok(rv)
|
|
222
149
|
}
|
|
223
|
-
})
|
|
150
|
+
})
|
|
224
151
|
}
|
|
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
152
|
|
|
232
153
|
function mapping(decoder) {
|
|
233
154
|
return dict(decoder).transform(function (obj) {
|
|
234
|
-
return new Map(
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
})
|
|
240
|
-
}
|
|
155
|
+
return new Map(
|
|
156
|
+
Object.keys(obj).map(function (key) {
|
|
157
|
+
return [key, obj[key]]
|
|
158
|
+
})
|
|
159
|
+
)
|
|
160
|
+
})
|
|
161
|
+
}
|
package/lib/objects.js.flow
CHANGED
|
@@ -2,20 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
import { annotateObject, merge, updateText } from '../annotate';
|
|
4
4
|
import { define } from '../Decoder';
|
|
5
|
+
import { subtract } from '../_utils';
|
|
5
6
|
import type { Annotation } from '../annotate';
|
|
6
7
|
import type { _Any as AnyDecoder } from '../_utils';
|
|
7
8
|
import type { Decoder, DecodeResult } from '../Decoder';
|
|
8
9
|
|
|
9
|
-
function subtract(xs: Set<string>, ys: Set<string>): Set<string> {
|
|
10
|
-
const result = new Set();
|
|
11
|
-
xs.forEach((x) => {
|
|
12
|
-
if (!ys.has(x)) {
|
|
13
|
-
result.add(x);
|
|
14
|
-
}
|
|
15
|
-
});
|
|
16
|
-
return result;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
10
|
/**
|
|
20
11
|
* Accepts any "plain old JavaScript object", but doesn't validate its keys or
|
|
21
12
|
* values further.
|
|
@@ -43,7 +34,8 @@ export const pojo: Decoder<{| [string]: mixed |}> = define((blob, ok, err) =>
|
|
|
43
34
|
// way to turn a read-only Object to a writeable one in ES6 seems
|
|
44
35
|
// to be to use object-spread. (Going off this benchmark:
|
|
45
36
|
// https://thecodebarbarian.com/object-assign-vs-object-spread.html)
|
|
46
|
-
|
|
37
|
+
// $FlowFixMe[incompatible-variance]
|
|
38
|
+
blob,
|
|
47
39
|
)
|
|
48
40
|
: err('Must be an object'),
|
|
49
41
|
);
|
|
@@ -192,7 +184,7 @@ export function inexact<O: { +[field: string]: AnyDecoder }>(
|
|
|
192
184
|
|
|
193
185
|
/**
|
|
194
186
|
* Accepts objects where all values match the given decoder, and returns the
|
|
195
|
-
* result as a `
|
|
187
|
+
* result as a `Record<string, T>`.
|
|
196
188
|
*
|
|
197
189
|
* The main difference between `object()` and `dict()` is that you'd typically
|
|
198
190
|
* use `object()` if this is a record-like object, where all field names are
|
package/lib/objects.mjs
CHANGED
|
@@ -1,223 +1,147 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
import { define } from '../Decoder.mjs';
|
|
5
|
-
|
|
6
|
-
function subtract(xs, ys) {
|
|
7
|
-
var result = new Set();
|
|
8
|
-
xs.forEach(function (x) {
|
|
9
|
-
if (!ys.has(x)) {
|
|
10
|
-
result.add(x);
|
|
11
|
-
}
|
|
12
|
-
});
|
|
13
|
-
return result;
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Accepts any "plain old JavaScript object", but doesn't validate its keys or
|
|
17
|
-
* values further.
|
|
18
|
-
*/
|
|
19
|
-
|
|
1
|
+
import { annotateObject, merge, updateText } from '../annotate.mjs'
|
|
2
|
+
import { define } from '../Decoder.mjs'
|
|
3
|
+
import { subtract } from '../_utils.mjs'
|
|
20
4
|
|
|
21
5
|
export var pojo = define(function (blob, ok, err) {
|
|
22
|
-
return blob !== null && blob !== undefined && typeof blob === 'object' &&
|
|
23
|
-
|
|
24
|
-
// $FlowFixMe[method-unbinding]
|
|
25
|
-
Object.prototype.toString.call(blob) === '[object Object]' ? ok( // NOTE:
|
|
26
|
-
// Since Flow 0.98, typeof o === 'object' refines to
|
|
27
|
-
// {| +[string]: mixed |}
|
|
28
|
-
// instead of
|
|
29
|
-
// {| [string]: mixed |}
|
|
30
|
-
//
|
|
31
|
-
// For rationale, see https://github.com/facebook/flow/issues/7685.
|
|
32
|
-
// In this case, we don't want to output a read-only version of
|
|
33
|
-
// the object because it's up to the user of decoders to
|
|
34
|
-
// determine what they want to do with the decoded output. If they
|
|
35
|
-
// want to write items into the array, that's fine! The fastest
|
|
36
|
-
// way to turn a read-only Object to a writeable one in ES6 seems
|
|
37
|
-
// to be to use object-spread. (Going off this benchmark:
|
|
38
|
-
// https://thecodebarbarian.com/object-assign-vs-object-spread.html)
|
|
39
|
-
_extends({}, blob)) : err('Must be an object');
|
|
40
|
-
});
|
|
41
|
-
/**
|
|
42
|
-
* Accepts objects with fields matching the given decoders. Extra fields that
|
|
43
|
-
* exist on the input object are ignored and will not be returned.
|
|
44
|
-
*/
|
|
6
|
+
return blob !== null && blob !== undefined && typeof blob === 'object' && Object.prototype.toString.call(blob) === '[object Object]' ? ok(blob) : err('Must be an object')
|
|
7
|
+
})
|
|
45
8
|
|
|
46
9
|
export function object(decodersByKey) {
|
|
47
|
-
|
|
48
|
-
var knownKeys = new Set(Object.keys(decodersByKey));
|
|
10
|
+
var knownKeys = new Set(Object.keys(decodersByKey))
|
|
49
11
|
return pojo.then(function (plainObj, ok, err) {
|
|
50
|
-
var actualKeys = new Set(Object.keys(plainObj))
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
var missingKeys = subtract(knownKeys, actualKeys);
|
|
56
|
-
var record = {};
|
|
57
|
-
var errors = null;
|
|
12
|
+
var actualKeys = new Set(Object.keys(plainObj))
|
|
13
|
+
|
|
14
|
+
var missingKeys = subtract(knownKeys, actualKeys)
|
|
15
|
+
var record = {}
|
|
16
|
+
var errors = null
|
|
58
17
|
Object.keys(decodersByKey).forEach(function (key) {
|
|
59
|
-
var decoder = decodersByKey[key]
|
|
60
|
-
var rawValue = plainObj[key]
|
|
61
|
-
var result = decoder.decode(rawValue)
|
|
18
|
+
var decoder = decodersByKey[key]
|
|
19
|
+
var rawValue = plainObj[key]
|
|
20
|
+
var result = decoder.decode(rawValue)
|
|
62
21
|
|
|
63
22
|
if (result.ok) {
|
|
64
|
-
var value = result.value
|
|
23
|
+
var value = result.value
|
|
65
24
|
|
|
66
25
|
if (value !== undefined) {
|
|
67
|
-
record[key] = value
|
|
68
|
-
}
|
|
69
|
-
// tracker
|
|
70
|
-
|
|
26
|
+
record[key] = value
|
|
27
|
+
}
|
|
71
28
|
|
|
72
|
-
missingKeys[
|
|
29
|
+
missingKeys['delete'](key)
|
|
73
30
|
} else {
|
|
74
|
-
var ann = result.error
|
|
75
|
-
// want to collect more error information.
|
|
31
|
+
var ann = result.error
|
|
76
32
|
|
|
77
33
|
if (rawValue === undefined) {
|
|
78
|
-
|
|
79
|
-
// undefined. This covers explicit undefineds to be
|
|
80
|
-
// treated the same as implicit undefineds (aka missing
|
|
81
|
-
// keys).
|
|
82
|
-
missingKeys.add(key);
|
|
34
|
+
missingKeys.add(key)
|
|
83
35
|
} else {
|
|
84
36
|
if (errors === null) {
|
|
85
|
-
errors = {}
|
|
37
|
+
errors = {}
|
|
86
38
|
}
|
|
87
39
|
|
|
88
|
-
errors[key] = ann
|
|
40
|
+
errors[key] = ann
|
|
89
41
|
}
|
|
90
42
|
}
|
|
91
|
-
})
|
|
92
|
-
// report. First of all, we want to report any inline errors in this
|
|
93
|
-
// object. Lastly, any fields that are missing should be annotated on
|
|
94
|
-
// the outer object itself.
|
|
43
|
+
})
|
|
95
44
|
|
|
96
45
|
if (errors || missingKeys.size > 0) {
|
|
97
|
-
var objAnn = annotateObject(plainObj)
|
|
46
|
+
var objAnn = annotateObject(plainObj)
|
|
98
47
|
|
|
99
48
|
if (errors) {
|
|
100
|
-
objAnn = merge(objAnn, errors)
|
|
49
|
+
objAnn = merge(objAnn, errors)
|
|
101
50
|
}
|
|
102
51
|
|
|
103
52
|
if (missingKeys.size > 0) {
|
|
104
|
-
var errMsg = Array.from(missingKeys)
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
53
|
+
var errMsg = Array.from(missingKeys)
|
|
54
|
+
.map(function (key) {
|
|
55
|
+
return '"' + key + '"'
|
|
56
|
+
})
|
|
57
|
+
.join(', ')
|
|
58
|
+
var pluralized = missingKeys.size > 1 ? 'keys' : 'key'
|
|
59
|
+
objAnn = updateText(objAnn, 'Missing ' + pluralized + ': ' + errMsg)
|
|
109
60
|
}
|
|
110
61
|
|
|
111
|
-
return err(objAnn)
|
|
62
|
+
return err(objAnn)
|
|
112
63
|
}
|
|
113
64
|
|
|
114
|
-
return ok(record)
|
|
115
|
-
})
|
|
65
|
+
return ok(record)
|
|
66
|
+
})
|
|
116
67
|
}
|
|
117
|
-
/**
|
|
118
|
-
* Like `object()`, but will reject inputs that contain extra fields that are
|
|
119
|
-
* not specified explicitly.
|
|
120
|
-
*/
|
|
121
68
|
|
|
122
69
|
export function exact(decodersByKey) {
|
|
123
|
-
|
|
124
|
-
var allowedKeys = new Set(Object.keys(decodersByKey)); // Check the inputted object for any unexpected extra keys
|
|
70
|
+
var allowedKeys = new Set(Object.keys(decodersByKey))
|
|
125
71
|
|
|
126
72
|
var checked = pojo.reject(function (plainObj) {
|
|
127
|
-
var actualKeys = new Set(Object.keys(plainObj))
|
|
128
|
-
var extraKeys = subtract(actualKeys, allowedKeys)
|
|
129
|
-
return extraKeys.size > 0 ?
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
// safe to force-cast it to an $Exact<> type.
|
|
134
|
-
|
|
135
|
-
return checked.then(object(decodersByKey).decode);
|
|
73
|
+
var actualKeys = new Set(Object.keys(plainObj))
|
|
74
|
+
var extraKeys = subtract(actualKeys, allowedKeys)
|
|
75
|
+
return extraKeys.size > 0 ? 'Unexpected extra keys: ' + Array.from(extraKeys).join(', ') : null
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
return checked.then(object(decodersByKey).decode)
|
|
136
79
|
}
|
|
137
|
-
/**
|
|
138
|
-
* Like `object()`, but will pass through any extra fields on the input object
|
|
139
|
-
* unvalidated that will thus be of `unknown` type statically.
|
|
140
|
-
*/
|
|
141
80
|
|
|
142
81
|
export function inexact(decodersByKey) {
|
|
143
82
|
return pojo.then(function (plainObj) {
|
|
144
|
-
var allkeys = new Set(Object.keys(plainObj))
|
|
83
|
+
var allkeys = new Set(Object.keys(plainObj))
|
|
145
84
|
var decoder = object(decodersByKey).transform(function (safepart) {
|
|
146
|
-
var safekeys = new Set(Object.keys(decodersByKey))
|
|
85
|
+
var safekeys = new Set(Object.keys(decodersByKey))
|
|
147
86
|
|
|
148
87
|
safekeys.forEach(function (k) {
|
|
149
|
-
return allkeys.add(k)
|
|
150
|
-
})
|
|
151
|
-
var rv = {}
|
|
88
|
+
return allkeys.add(k)
|
|
89
|
+
})
|
|
90
|
+
var rv = {}
|
|
152
91
|
allkeys.forEach(function (k) {
|
|
153
92
|
if (safekeys.has(k)) {
|
|
154
|
-
var value = safepart[k]
|
|
93
|
+
var value = safepart[k]
|
|
155
94
|
|
|
156
95
|
if (value !== undefined) {
|
|
157
|
-
rv[k] = value
|
|
96
|
+
rv[k] = value
|
|
158
97
|
}
|
|
159
98
|
} else {
|
|
160
|
-
rv[k] = plainObj[k]
|
|
99
|
+
rv[k] = plainObj[k]
|
|
161
100
|
}
|
|
162
|
-
})
|
|
163
|
-
return rv
|
|
164
|
-
})
|
|
165
|
-
return decoder.decode(plainObj)
|
|
166
|
-
})
|
|
101
|
+
})
|
|
102
|
+
return rv
|
|
103
|
+
})
|
|
104
|
+
return decoder.decode(plainObj)
|
|
105
|
+
})
|
|
167
106
|
}
|
|
168
|
-
/**
|
|
169
|
-
* Accepts objects where all values match the given decoder, and returns the
|
|
170
|
-
* result as a `{ [string]: T }`.
|
|
171
|
-
*
|
|
172
|
-
* The main difference between `object()` and `dict()` is that you'd typically
|
|
173
|
-
* use `object()` if this is a record-like object, where all field names are
|
|
174
|
-
* known and the values are heterogeneous. Whereas with `dict()` the keys are
|
|
175
|
-
* typically dynamic and the values homogeneous, like in a dictionary,
|
|
176
|
-
* a lookup table, or a cache.
|
|
177
|
-
*/
|
|
178
107
|
|
|
179
108
|
export function dict(decoder) {
|
|
180
109
|
return pojo.then(function (plainObj, ok, err) {
|
|
181
|
-
var rv = {}
|
|
182
|
-
var errors = null
|
|
110
|
+
var rv = {}
|
|
111
|
+
var errors = null
|
|
183
112
|
Object.keys(plainObj).forEach(function (key) {
|
|
184
|
-
var value = plainObj[key]
|
|
185
|
-
var result = decoder.decode(value)
|
|
113
|
+
var value = plainObj[key]
|
|
114
|
+
var result = decoder.decode(value)
|
|
186
115
|
|
|
187
116
|
if (result.ok) {
|
|
188
117
|
if (errors === null) {
|
|
189
|
-
rv[key] = result.value
|
|
118
|
+
rv[key] = result.value
|
|
190
119
|
}
|
|
191
120
|
} else {
|
|
192
|
-
rv = {}
|
|
121
|
+
rv = {}
|
|
193
122
|
|
|
194
123
|
if (errors === null) {
|
|
195
|
-
errors = {}
|
|
124
|
+
errors = {}
|
|
196
125
|
}
|
|
197
126
|
|
|
198
|
-
errors[key] = result.error
|
|
127
|
+
errors[key] = result.error
|
|
199
128
|
}
|
|
200
|
-
})
|
|
129
|
+
})
|
|
201
130
|
|
|
202
131
|
if (errors !== null) {
|
|
203
|
-
return err(merge(annotateObject(plainObj), errors))
|
|
132
|
+
return err(merge(annotateObject(plainObj), errors))
|
|
204
133
|
} else {
|
|
205
|
-
return ok(rv)
|
|
134
|
+
return ok(rv)
|
|
206
135
|
}
|
|
207
|
-
})
|
|
136
|
+
})
|
|
208
137
|
}
|
|
209
|
-
/**
|
|
210
|
-
* Similar to `dict()`, but returns the result as a `Map<string, T>` (an [ES6
|
|
211
|
-
* Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map))
|
|
212
|
-
* instead.
|
|
213
|
-
*/
|
|
214
138
|
|
|
215
139
|
export function mapping(decoder) {
|
|
216
140
|
return dict(decoder).transform(function (obj) {
|
|
217
|
-
return new Map(
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
})
|
|
223
|
-
}
|
|
141
|
+
return new Map(
|
|
142
|
+
Object.keys(obj).map(function (key) {
|
|
143
|
+
return [key, obj[key]]
|
|
144
|
+
})
|
|
145
|
+
)
|
|
146
|
+
})
|
|
147
|
+
}
|