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/objects.d.ts
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
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<Record<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 Record<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 Record<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<Record<string, unknown>>;
|
|
50
|
-
export function inexact<O extends Record<string, Decoder<any>>>(
|
|
51
|
-
decodersByKey: O,
|
|
52
|
-
): Decoder<
|
|
53
|
-
{ [K in keyof ObjectDecoderType<O>]: ObjectDecoderType<O>[K] } & Record<
|
|
54
|
-
string,
|
|
55
|
-
unknown
|
|
56
|
-
>
|
|
57
|
-
>;
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Accepts objects where all values match the given decoder, and returns the
|
|
61
|
-
* result as a `Record<string, T>`.
|
|
62
|
-
*
|
|
63
|
-
* The main difference between `object()` and `dict()` is that you'd typically
|
|
64
|
-
* use `object()` if this is a record-like object, where all field names are
|
|
65
|
-
* known and the values are heterogeneous. Whereas with `dict()` the keys are
|
|
66
|
-
* typically dynamic and the values homogeneous, like in a dictionary,
|
|
67
|
-
* a lookup table, or a cache.
|
|
68
|
-
*/
|
|
69
|
-
export function dict<T>(decoder: Decoder<T>): Decoder<Record<string, T>>;
|
|
70
|
-
|
|
71
|
-
/**
|
|
72
|
-
* Similar to `dict()`, but returns the result as a `Map<string, T>` (an [ES6
|
|
73
|
-
* Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map))
|
|
74
|
-
* instead.
|
|
75
|
-
*/
|
|
76
|
-
export function mapping<T>(decoder: Decoder<T>): Decoder<Map<string, T>>;
|
package/lib/objects.js
DELETED
|
@@ -1,138 +0,0 @@
|
|
|
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
|
-
var _annotate = require('../annotate')
|
|
11
|
-
var _Decoder = require('../Decoder')
|
|
12
|
-
var _utils = require('../_utils')
|
|
13
|
-
|
|
14
|
-
var pojo = (0, _Decoder.define)(function (blob, ok, err) {
|
|
15
|
-
return blob !== null && blob !== undefined && typeof blob === 'object' && Object.prototype.toString.call(blob) === '[object Object]' ? ok(blob) : err('Must be an object')
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
exports.pojo = pojo
|
|
19
|
-
function object(decodersByKey) {
|
|
20
|
-
var knownKeys = new Set(Object.keys(decodersByKey))
|
|
21
|
-
return pojo.then(function (plainObj, ok, err) {
|
|
22
|
-
var actualKeys = new Set(Object.keys(plainObj))
|
|
23
|
-
var missingKeys = (0, _utils.subtract)(knownKeys, actualKeys)
|
|
24
|
-
var record = {}
|
|
25
|
-
var errors = null
|
|
26
|
-
Object.keys(decodersByKey).forEach(function (key) {
|
|
27
|
-
var decoder = decodersByKey[key]
|
|
28
|
-
var rawValue = plainObj[key]
|
|
29
|
-
var result = decoder.decode(rawValue)
|
|
30
|
-
if (result.ok) {
|
|
31
|
-
var value = result.value
|
|
32
|
-
if (value !== undefined) {
|
|
33
|
-
record[key] = value
|
|
34
|
-
}
|
|
35
|
-
missingKeys['delete'](key)
|
|
36
|
-
} else {
|
|
37
|
-
var ann = result.error
|
|
38
|
-
if (rawValue === undefined) {
|
|
39
|
-
missingKeys.add(key)
|
|
40
|
-
} else {
|
|
41
|
-
if (errors === null) {
|
|
42
|
-
errors = {}
|
|
43
|
-
}
|
|
44
|
-
errors[key] = ann
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
})
|
|
48
|
-
if (errors || missingKeys.size > 0) {
|
|
49
|
-
var objAnn = (0, _annotate.annotateObject)(plainObj)
|
|
50
|
-
if (errors) {
|
|
51
|
-
objAnn = (0, _annotate.merge)(objAnn, errors)
|
|
52
|
-
}
|
|
53
|
-
if (missingKeys.size > 0) {
|
|
54
|
-
var errMsg = Array.from(missingKeys)
|
|
55
|
-
.map(function (key) {
|
|
56
|
-
return '"' + key + '"'
|
|
57
|
-
})
|
|
58
|
-
.join(', ')
|
|
59
|
-
var pluralized = missingKeys.size > 1 ? 'keys' : 'key'
|
|
60
|
-
objAnn = (0, _annotate.updateText)(objAnn, 'Missing ' + pluralized + ': ' + errMsg)
|
|
61
|
-
}
|
|
62
|
-
return err(objAnn)
|
|
63
|
-
}
|
|
64
|
-
return ok(record)
|
|
65
|
-
})
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function exact(decodersByKey) {
|
|
69
|
-
var allowedKeys = new Set(Object.keys(decodersByKey))
|
|
70
|
-
var checked = pojo.reject(function (plainObj) {
|
|
71
|
-
var actualKeys = new Set(Object.keys(plainObj))
|
|
72
|
-
var extraKeys = (0, _utils.subtract)(actualKeys, allowedKeys)
|
|
73
|
-
return extraKeys.size > 0 ? 'Unexpected extra keys: ' + Array.from(extraKeys).join(', ') : null
|
|
74
|
-
})
|
|
75
|
-
return checked.then(object(decodersByKey).decode)
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function inexact(decodersByKey) {
|
|
79
|
-
return pojo.then(function (plainObj) {
|
|
80
|
-
var allkeys = new Set(Object.keys(plainObj))
|
|
81
|
-
var decoder = object(decodersByKey).transform(function (safepart) {
|
|
82
|
-
var safekeys = new Set(Object.keys(decodersByKey))
|
|
83
|
-
safekeys.forEach(function (k) {
|
|
84
|
-
return allkeys.add(k)
|
|
85
|
-
})
|
|
86
|
-
var rv = {}
|
|
87
|
-
allkeys.forEach(function (k) {
|
|
88
|
-
if (safekeys.has(k)) {
|
|
89
|
-
var value = safepart[k]
|
|
90
|
-
if (value !== undefined) {
|
|
91
|
-
rv[k] = value
|
|
92
|
-
}
|
|
93
|
-
} else {
|
|
94
|
-
rv[k] = plainObj[k]
|
|
95
|
-
}
|
|
96
|
-
})
|
|
97
|
-
return rv
|
|
98
|
-
})
|
|
99
|
-
return decoder.decode(plainObj)
|
|
100
|
-
})
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function dict(decoder) {
|
|
104
|
-
return pojo.then(function (plainObj, ok, err) {
|
|
105
|
-
var rv = {}
|
|
106
|
-
var errors = null
|
|
107
|
-
Object.keys(plainObj).forEach(function (key) {
|
|
108
|
-
var value = plainObj[key]
|
|
109
|
-
var result = decoder.decode(value)
|
|
110
|
-
if (result.ok) {
|
|
111
|
-
if (errors === null) {
|
|
112
|
-
rv[key] = result.value
|
|
113
|
-
}
|
|
114
|
-
} else {
|
|
115
|
-
rv = {}
|
|
116
|
-
if (errors === null) {
|
|
117
|
-
errors = {}
|
|
118
|
-
}
|
|
119
|
-
errors[key] = result.error
|
|
120
|
-
}
|
|
121
|
-
})
|
|
122
|
-
if (errors !== null) {
|
|
123
|
-
return err((0, _annotate.merge)((0, _annotate.annotateObject)(plainObj), errors))
|
|
124
|
-
} else {
|
|
125
|
-
return ok(rv)
|
|
126
|
-
}
|
|
127
|
-
})
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
function mapping(decoder) {
|
|
131
|
-
return dict(decoder).transform(function (obj) {
|
|
132
|
-
return new Map(
|
|
133
|
-
Object.keys(obj).map(function (key) {
|
|
134
|
-
return [key, obj[key]]
|
|
135
|
-
})
|
|
136
|
-
)
|
|
137
|
-
})
|
|
138
|
-
}
|
package/lib/objects.js.flow
DELETED
|
@@ -1,238 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { annotateObject, merge, updateText } from '../annotate';
|
|
4
|
-
import { define } from '../Decoder';
|
|
5
|
-
import { subtract } from '../_utils';
|
|
6
|
-
import type { Annotation } from '../annotate';
|
|
7
|
-
import type { _Any as AnyDecoder } from '../_utils';
|
|
8
|
-
import type { Decoder, DecodeResult } from '../Decoder';
|
|
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<{| [string]: mixed |}> = define((blob, ok, err) =>
|
|
15
|
-
blob !== null &&
|
|
16
|
-
blob !== undefined &&
|
|
17
|
-
typeof blob === 'object' &&
|
|
18
|
-
// 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(blob) === '[object Object]'
|
|
22
|
-
? ok(
|
|
23
|
-
// NOTE:
|
|
24
|
-
// Since Flow 0.98, typeof o === 'object' refines to
|
|
25
|
-
// {| +[string]: mixed |}
|
|
26
|
-
// instead of
|
|
27
|
-
// {| [string]: mixed |}
|
|
28
|
-
//
|
|
29
|
-
// For rationale, see https://github.com/facebook/flow/issues/7685.
|
|
30
|
-
// In this case, we don't want to output a read-only version of
|
|
31
|
-
// the object because it's up to the user of decoders to
|
|
32
|
-
// determine what they want to do with the decoded output. If they
|
|
33
|
-
// want to write items into the array, that's fine! The fastest
|
|
34
|
-
// way to turn a read-only Object to a writeable one in ES6 seems
|
|
35
|
-
// to be to use object-spread. (Going off this benchmark:
|
|
36
|
-
// https://thecodebarbarian.com/object-assign-vs-object-spread.html)
|
|
37
|
-
// $FlowFixMe[incompatible-variance]
|
|
38
|
-
blob,
|
|
39
|
-
)
|
|
40
|
-
: err('Must be an object'),
|
|
41
|
-
);
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Accepts objects with fields matching the given decoders. Extra fields that
|
|
45
|
-
* exist on the input object are ignored and will not be returned.
|
|
46
|
-
*/
|
|
47
|
-
export function object<O: { +[field: string]: AnyDecoder, ... }>(
|
|
48
|
-
decodersByKey: O,
|
|
49
|
-
): Decoder<$ObjMap<O, <T>(Decoder<T>) => T>> {
|
|
50
|
-
// Compute this set at decoder definition time
|
|
51
|
-
const knownKeys = new Set(Object.keys(decodersByKey));
|
|
52
|
-
|
|
53
|
-
return pojo.then((plainObj, ok, err) => {
|
|
54
|
-
const actualKeys = new Set(Object.keys(plainObj));
|
|
55
|
-
|
|
56
|
-
// At this point, "missingKeys" will also include all fields that may
|
|
57
|
-
// validly be optional. We'll let the underlying decoder decide and
|
|
58
|
-
// remove the key from this missing set if the decoder accepts the
|
|
59
|
-
// value.
|
|
60
|
-
const missingKeys = subtract(knownKeys, actualKeys);
|
|
61
|
-
|
|
62
|
-
let record = {};
|
|
63
|
-
let errors: { [key: string]: Annotation } | null = null;
|
|
64
|
-
|
|
65
|
-
Object.keys(decodersByKey).forEach((key) => {
|
|
66
|
-
const decoder = decodersByKey[key];
|
|
67
|
-
const rawValue = plainObj[key];
|
|
68
|
-
const result: DecodeResult<mixed> = decoder.decode(rawValue);
|
|
69
|
-
|
|
70
|
-
if (result.ok) {
|
|
71
|
-
const value = result.value;
|
|
72
|
-
if (value !== undefined) {
|
|
73
|
-
record[key] = value;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// If this succeeded, remove the key from the missing keys
|
|
77
|
-
// tracker
|
|
78
|
-
missingKeys.delete(key);
|
|
79
|
-
} else {
|
|
80
|
-
const ann = result.error;
|
|
81
|
-
|
|
82
|
-
// Keep track of the annotation, but don't return just yet. We
|
|
83
|
-
// want to collect more error information.
|
|
84
|
-
if (rawValue === undefined) {
|
|
85
|
-
// Explicitly add it to the missing set if the value is
|
|
86
|
-
// undefined. This covers explicit undefineds to be
|
|
87
|
-
// treated the same as implicit undefineds (aka missing
|
|
88
|
-
// keys).
|
|
89
|
-
missingKeys.add(key);
|
|
90
|
-
} else {
|
|
91
|
-
if (errors === null) {
|
|
92
|
-
errors = {};
|
|
93
|
-
}
|
|
94
|
-
errors[key] = ann;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
// Deal with errors now. There are two classes of errors we want to
|
|
100
|
-
// report. First of all, we want to report any inline errors in this
|
|
101
|
-
// object. Lastly, any fields that are missing should be annotated on
|
|
102
|
-
// the outer object itself.
|
|
103
|
-
if (errors || missingKeys.size > 0) {
|
|
104
|
-
let objAnn = annotateObject(plainObj);
|
|
105
|
-
|
|
106
|
-
if (errors) {
|
|
107
|
-
objAnn = merge(objAnn, errors);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if (missingKeys.size > 0) {
|
|
111
|
-
const errMsg = Array.from(missingKeys)
|
|
112
|
-
.map((key) => `"${key}"`)
|
|
113
|
-
.join(', ');
|
|
114
|
-
const pluralized = missingKeys.size > 1 ? 'keys' : 'key';
|
|
115
|
-
objAnn = updateText(objAnn, `Missing ${pluralized}: ${errMsg}`);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return err(objAnn);
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
return ok(record);
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/**
|
|
126
|
-
* Like `object()`, but will reject inputs that contain extra fields that are
|
|
127
|
-
* not specified explicitly.
|
|
128
|
-
*/
|
|
129
|
-
export function exact<O: { +[field: string]: AnyDecoder, ... }>(
|
|
130
|
-
decodersByKey: O,
|
|
131
|
-
): Decoder<$ObjMap<$Exact<O>, <T>(Decoder<T>) => T>> {
|
|
132
|
-
// Compute this set at decoder definition time
|
|
133
|
-
const allowedKeys = new Set(Object.keys(decodersByKey));
|
|
134
|
-
|
|
135
|
-
// Check the inputted object for any unexpected extra keys
|
|
136
|
-
const checked = pojo.reject((plainObj) => {
|
|
137
|
-
const actualKeys = new Set(Object.keys(plainObj));
|
|
138
|
-
const extraKeys = subtract(actualKeys, allowedKeys);
|
|
139
|
-
return extraKeys.size > 0
|
|
140
|
-
? `Unexpected extra keys: ${Array.from(extraKeys).join(', ')}`
|
|
141
|
-
: // Don't reject
|
|
142
|
-
null;
|
|
143
|
-
});
|
|
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
|
-
return checked.then(object(decodersByKey).decode);
|
|
149
|
-
}
|
|
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
|
-
export function inexact<O: { +[field: string]: AnyDecoder }>(
|
|
156
|
-
decodersByKey: O,
|
|
157
|
-
): Decoder<$ObjMap<O, <T>(Decoder<T>) => T> & { +[string]: mixed }> {
|
|
158
|
-
return pojo.then((plainObj) => {
|
|
159
|
-
const allkeys = new Set(Object.keys(plainObj));
|
|
160
|
-
const decoder = object(decodersByKey).transform(
|
|
161
|
-
(safepart: $ObjMap<O, <T>(Decoder<T>) => T>) => {
|
|
162
|
-
const safekeys = new Set(Object.keys(decodersByKey));
|
|
163
|
-
|
|
164
|
-
// To account for hard-coded keys that aren't part of the input
|
|
165
|
-
safekeys.forEach((k) => allkeys.add(k));
|
|
166
|
-
|
|
167
|
-
const rv = {};
|
|
168
|
-
allkeys.forEach((k) => {
|
|
169
|
-
if (safekeys.has(k)) {
|
|
170
|
-
const value = safepart[k];
|
|
171
|
-
if (value !== undefined) {
|
|
172
|
-
rv[k] = value;
|
|
173
|
-
}
|
|
174
|
-
} else {
|
|
175
|
-
rv[k] = plainObj[k];
|
|
176
|
-
}
|
|
177
|
-
});
|
|
178
|
-
return rv;
|
|
179
|
-
},
|
|
180
|
-
);
|
|
181
|
-
return decoder.decode(plainObj);
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
/**
|
|
186
|
-
* Accepts objects where all values match the given decoder, and returns the
|
|
187
|
-
* result as a `Record<string, T>`.
|
|
188
|
-
*
|
|
189
|
-
* The main difference between `object()` and `dict()` is that you'd typically
|
|
190
|
-
* use `object()` if this is a record-like object, where all field names are
|
|
191
|
-
* known and the values are heterogeneous. Whereas with `dict()` the keys are
|
|
192
|
-
* typically dynamic and the values homogeneous, like in a dictionary,
|
|
193
|
-
* a lookup table, or a cache.
|
|
194
|
-
*/
|
|
195
|
-
export function dict<T>(decoder: Decoder<T>): Decoder<{ [string]: T }> {
|
|
196
|
-
return pojo.then((plainObj, ok, err) => {
|
|
197
|
-
let rv: { [key: string]: T } = {};
|
|
198
|
-
let errors: { [key: string]: Annotation } | null = null;
|
|
199
|
-
|
|
200
|
-
Object.keys(plainObj).forEach((key: string) => {
|
|
201
|
-
const value = plainObj[key];
|
|
202
|
-
const result = decoder.decode(value);
|
|
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
|
-
if (errors === null) {
|
|
210
|
-
errors = {};
|
|
211
|
-
}
|
|
212
|
-
errors[key] = result.error;
|
|
213
|
-
}
|
|
214
|
-
});
|
|
215
|
-
|
|
216
|
-
if (errors !== null) {
|
|
217
|
-
return err(merge(annotateObject(plainObj), errors));
|
|
218
|
-
} else {
|
|
219
|
-
return ok(rv);
|
|
220
|
-
}
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Similar to `dict()`, but returns the result as a `Map<string, T>` (an [ES6
|
|
226
|
-
* Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map))
|
|
227
|
-
* instead.
|
|
228
|
-
*/
|
|
229
|
-
export function mapping<T>(decoder: Decoder<T>): Decoder<Map<string, T>> {
|
|
230
|
-
return dict(decoder).transform(
|
|
231
|
-
(obj) =>
|
|
232
|
-
new Map(
|
|
233
|
-
// This is effectively Object.entries(obj), but in a way that Flow
|
|
234
|
-
// will know the types are okay
|
|
235
|
-
Object.keys(obj).map((key) => [key, obj[key]]),
|
|
236
|
-
),
|
|
237
|
-
);
|
|
238
|
-
}
|
package/lib/objects.mjs
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import { annotateObject, merge, updateText } from '../annotate.mjs'
|
|
2
|
-
import { define } from '../Decoder.mjs'
|
|
3
|
-
import { subtract } from '../_utils.mjs'
|
|
4
|
-
|
|
5
|
-
export var pojo = define(function (blob, ok, err) {
|
|
6
|
-
return blob !== null && blob !== undefined && typeof blob === 'object' && Object.prototype.toString.call(blob) === '[object Object]' ? ok(blob) : err('Must be an object')
|
|
7
|
-
})
|
|
8
|
-
|
|
9
|
-
export function object(decodersByKey) {
|
|
10
|
-
var knownKeys = new Set(Object.keys(decodersByKey))
|
|
11
|
-
return pojo.then(function (plainObj, ok, err) {
|
|
12
|
-
var actualKeys = new Set(Object.keys(plainObj))
|
|
13
|
-
var missingKeys = subtract(knownKeys, actualKeys)
|
|
14
|
-
var record = {}
|
|
15
|
-
var errors = null
|
|
16
|
-
Object.keys(decodersByKey).forEach(function (key) {
|
|
17
|
-
var decoder = decodersByKey[key]
|
|
18
|
-
var rawValue = plainObj[key]
|
|
19
|
-
var result = decoder.decode(rawValue)
|
|
20
|
-
if (result.ok) {
|
|
21
|
-
var value = result.value
|
|
22
|
-
if (value !== undefined) {
|
|
23
|
-
record[key] = value
|
|
24
|
-
}
|
|
25
|
-
missingKeys['delete'](key)
|
|
26
|
-
} else {
|
|
27
|
-
var ann = result.error
|
|
28
|
-
if (rawValue === undefined) {
|
|
29
|
-
missingKeys.add(key)
|
|
30
|
-
} else {
|
|
31
|
-
if (errors === null) {
|
|
32
|
-
errors = {}
|
|
33
|
-
}
|
|
34
|
-
errors[key] = ann
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
})
|
|
38
|
-
if (errors || missingKeys.size > 0) {
|
|
39
|
-
var objAnn = annotateObject(plainObj)
|
|
40
|
-
if (errors) {
|
|
41
|
-
objAnn = merge(objAnn, errors)
|
|
42
|
-
}
|
|
43
|
-
if (missingKeys.size > 0) {
|
|
44
|
-
var errMsg = Array.from(missingKeys)
|
|
45
|
-
.map(function (key) {
|
|
46
|
-
return '"' + key + '"'
|
|
47
|
-
})
|
|
48
|
-
.join(', ')
|
|
49
|
-
var pluralized = missingKeys.size > 1 ? 'keys' : 'key'
|
|
50
|
-
objAnn = updateText(objAnn, 'Missing ' + pluralized + ': ' + errMsg)
|
|
51
|
-
}
|
|
52
|
-
return err(objAnn)
|
|
53
|
-
}
|
|
54
|
-
return ok(record)
|
|
55
|
-
})
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export function exact(decodersByKey) {
|
|
59
|
-
var allowedKeys = new Set(Object.keys(decodersByKey))
|
|
60
|
-
var checked = pojo.reject(function (plainObj) {
|
|
61
|
-
var actualKeys = new Set(Object.keys(plainObj))
|
|
62
|
-
var extraKeys = subtract(actualKeys, allowedKeys)
|
|
63
|
-
return extraKeys.size > 0 ? 'Unexpected extra keys: ' + Array.from(extraKeys).join(', ') : null
|
|
64
|
-
})
|
|
65
|
-
return checked.then(object(decodersByKey).decode)
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export function inexact(decodersByKey) {
|
|
69
|
-
return pojo.then(function (plainObj) {
|
|
70
|
-
var allkeys = new Set(Object.keys(plainObj))
|
|
71
|
-
var decoder = object(decodersByKey).transform(function (safepart) {
|
|
72
|
-
var safekeys = new Set(Object.keys(decodersByKey))
|
|
73
|
-
safekeys.forEach(function (k) {
|
|
74
|
-
return allkeys.add(k)
|
|
75
|
-
})
|
|
76
|
-
var rv = {}
|
|
77
|
-
allkeys.forEach(function (k) {
|
|
78
|
-
if (safekeys.has(k)) {
|
|
79
|
-
var value = safepart[k]
|
|
80
|
-
if (value !== undefined) {
|
|
81
|
-
rv[k] = value
|
|
82
|
-
}
|
|
83
|
-
} else {
|
|
84
|
-
rv[k] = plainObj[k]
|
|
85
|
-
}
|
|
86
|
-
})
|
|
87
|
-
return rv
|
|
88
|
-
})
|
|
89
|
-
return decoder.decode(plainObj)
|
|
90
|
-
})
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export function dict(decoder) {
|
|
94
|
-
return pojo.then(function (plainObj, ok, err) {
|
|
95
|
-
var rv = {}
|
|
96
|
-
var errors = null
|
|
97
|
-
Object.keys(plainObj).forEach(function (key) {
|
|
98
|
-
var value = plainObj[key]
|
|
99
|
-
var result = decoder.decode(value)
|
|
100
|
-
if (result.ok) {
|
|
101
|
-
if (errors === null) {
|
|
102
|
-
rv[key] = result.value
|
|
103
|
-
}
|
|
104
|
-
} else {
|
|
105
|
-
rv = {}
|
|
106
|
-
if (errors === null) {
|
|
107
|
-
errors = {}
|
|
108
|
-
}
|
|
109
|
-
errors[key] = result.error
|
|
110
|
-
}
|
|
111
|
-
})
|
|
112
|
-
if (errors !== null) {
|
|
113
|
-
return err(merge(annotateObject(plainObj), errors))
|
|
114
|
-
} else {
|
|
115
|
-
return ok(rv)
|
|
116
|
-
}
|
|
117
|
-
})
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export function mapping(decoder) {
|
|
121
|
-
return dict(decoder).transform(function (obj) {
|
|
122
|
-
return new Map(
|
|
123
|
-
Object.keys(obj).map(function (key) {
|
|
124
|
-
return [key, obj[key]]
|
|
125
|
-
})
|
|
126
|
-
)
|
|
127
|
-
})
|
|
128
|
-
}
|
package/lib/strings.d.ts
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
/// <reference lib="dom" />
|
|
2
|
-
|
|
3
|
-
import { Decoder } from '../Decoder';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Accepts and returns strings.
|
|
7
|
-
*/
|
|
8
|
-
export const string: Decoder<string>;
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Like `string`, but will reject the empty string or strings containing only whitespace.
|
|
12
|
-
*/
|
|
13
|
-
export const nonEmptyString: Decoder<string>;
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Accepts and returns strings that match the given regular expression.
|
|
17
|
-
*/
|
|
18
|
-
export function regex(regex: RegExp, msg: string): Decoder<string>;
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* Accepts and returns strings that are syntactically valid email addresses.
|
|
22
|
-
* (This will not mean that the email address actually exist.)
|
|
23
|
-
*/
|
|
24
|
-
export const email: Decoder<string>;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Accepts strings that are valid URLs, returns the value as a URL instance.
|
|
28
|
-
*/
|
|
29
|
-
export const url: Decoder<URL>;
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Accepts strings that are valid URLs, but only HTTPS ones. Returns the value
|
|
33
|
-
* as a URL instance.
|
|
34
|
-
*/
|
|
35
|
-
export const httpsUrl: Decoder<URL>;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Accepts strings that are valid
|
|
39
|
-
* [UUIDs](https://en.wikipedia.org/wiki/universally_unique_identifier)
|
|
40
|
-
* (universally unique identifier).
|
|
41
|
-
*/
|
|
42
|
-
export const uuid: Decoder<string>;
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Like `uuid`, but only accepts
|
|
46
|
-
* [UUIDv1](https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_1_%28date-time_and_MAC_address%29)
|
|
47
|
-
* strings.
|
|
48
|
-
*/
|
|
49
|
-
export const uuidv1: Decoder<string>;
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Like `uuid`, but only accepts
|
|
53
|
-
* [UUIDv4](https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_%28random%29)
|
|
54
|
-
* strings.
|
|
55
|
-
*/
|
|
56
|
-
export const uuidv4: Decoder<string>;
|
package/lib/strings.js
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true
|
|
4
|
-
exports.nonEmptyString = exports.httpsUrl = exports.email = void 0
|
|
5
|
-
exports.regex = regex
|
|
6
|
-
exports.uuidv4 = exports.uuidv1 = exports.uuid = exports.url = exports.string = void 0
|
|
7
|
-
var _Decoder = require('../Decoder')
|
|
8
|
-
var _unions = require('./unions')
|
|
9
|
-
var _utilities = require('./utilities')
|
|
10
|
-
|
|
11
|
-
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]*)?)?$/
|
|
12
|
-
|
|
13
|
-
var string = (0, _Decoder.define)(function (blob, ok, err) {
|
|
14
|
-
return typeof blob === 'string' ? ok(blob) : err('Must be string')
|
|
15
|
-
})
|
|
16
|
-
|
|
17
|
-
exports.string = string
|
|
18
|
-
var nonEmptyString = regex(/\S/, 'Must be non-empty string')
|
|
19
|
-
|
|
20
|
-
exports.nonEmptyString = nonEmptyString
|
|
21
|
-
function regex(regex, msg) {
|
|
22
|
-
return string.refine(function (s) {
|
|
23
|
-
return regex.test(s)
|
|
24
|
-
}, msg)
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
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')
|
|
28
|
-
|
|
29
|
-
exports.email = email
|
|
30
|
-
var url = (0, _unions.either)(
|
|
31
|
-
regex(url_re, 'Must be URL').transform(function (value) {
|
|
32
|
-
return new URL(value)
|
|
33
|
-
}),
|
|
34
|
-
(0, _utilities.instanceOf)(URL)
|
|
35
|
-
)
|
|
36
|
-
|
|
37
|
-
exports.url = url
|
|
38
|
-
var httpsUrl = url.refine(function (value) {
|
|
39
|
-
return value.protocol === 'https:'
|
|
40
|
-
}, 'Must be an HTTPS URL')
|
|
41
|
-
|
|
42
|
-
exports.httpsUrl = httpsUrl
|
|
43
|
-
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')
|
|
44
|
-
|
|
45
|
-
exports.uuid = uuid
|
|
46
|
-
var uuidv1 = uuid.refine(function (value) {
|
|
47
|
-
return value[14] === '1'
|
|
48
|
-
}, 'Must be uuidv1')
|
|
49
|
-
|
|
50
|
-
exports.uuidv1 = uuidv1
|
|
51
|
-
var uuidv4 = uuid.refine(function (value) {
|
|
52
|
-
return value[14] === '4'
|
|
53
|
-
}, 'Must be uuidv4')
|
|
54
|
-
exports.uuidv4 = uuidv4
|