decoders 1.25.5 → 2.0.0-beta2
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 +23 -3
- package/_esm/_guard.js +15 -0
- package/_esm/_guard.js.flow +20 -0
- package/_esm/_types.js +0 -0
- package/_esm/_types.js.flow +20 -0
- package/_esm/_utils.js +93 -0
- package/_esm/_utils.js.flow +97 -0
- package/_esm/annotate.js +144 -0
- package/_esm/annotate.js.flow +218 -0
- package/_esm/core/array.js +91 -0
- package/{array.js.flow → _esm/core/array.js.flow} +22 -25
- package/_esm/core/boolean.js +28 -0
- package/{boolean.js.flow → _esm/core/boolean.js.flow} +8 -7
- package/_esm/core/composition.js +42 -0
- package/{utils.js.flow → _esm/core/composition.js.flow} +7 -22
- package/_esm/core/constants.js +46 -0
- package/{constants.js.flow → _esm/core/constants.js.flow} +13 -12
- package/_esm/core/date.js +28 -0
- package/{date.js.flow → _esm/core/date.js.flow} +9 -7
- package/{describe.js → _esm/core/describe.js} +5 -11
- package/{describe.js.flow → _esm/core/describe.js.flow} +4 -4
- package/{dispatch.js → _esm/core/dispatch.js} +8 -15
- package/{dispatch.js.flow → _esm/core/dispatch.js.flow} +6 -5
- package/_esm/core/either.js +90 -0
- package/{either.js.flow → _esm/core/either.js.flow} +55 -35
- package/_esm/core/fail.js +11 -0
- package/_esm/core/fail.js.flow +12 -0
- package/_esm/core/instanceOf.js +8 -0
- package/{instanceOf.js.flow → _esm/core/instanceOf.js.flow} +7 -8
- package/_esm/core/json.js +15 -0
- package/{json.js.flow → _esm/core/json.js.flow} +4 -4
- package/_esm/core/lazy.js +11 -0
- package/{lazy.js.flow → _esm/core/lazy.js.flow} +1 -1
- package/_esm/core/mapping.js +54 -0
- package/_esm/core/mapping.js.flow +54 -0
- package/_esm/core/number.js +25 -0
- package/{number.js.flow → _esm/core/number.js.flow} +10 -11
- package/_esm/core/object.js +175 -0
- package/{object.js.flow → _esm/core/object.js.flow} +44 -52
- package/_esm/core/optional.js +38 -0
- package/{optional.js.flow → _esm/core/optional.js.flow} +6 -7
- package/{string.js → _esm/core/string.js} +18 -35
- package/{string.js.flow → _esm/core/string.js.flow} +13 -12
- package/_esm/core/tuple.js +155 -0
- package/{tuple.js.flow → _esm/core/tuple.js.flow} +62 -63
- package/_esm/format/index.js +2 -0
- package/_esm/format/index.js.flow +4 -0
- package/_esm/format/inline.js +137 -0
- package/_esm/format/inline.js.flow +122 -0
- package/_esm/format/short.js +4 -0
- package/_esm/format/short.js.flow +8 -0
- package/_esm/index.js +37 -0
- package/_esm/index.js.flow +56 -0
- package/_esm/result.js +139 -0
- package/_esm/result.js.flow +166 -0
- package/_guard.js +26 -0
- package/_guard.js.flow +20 -0
- package/_types.js +1 -0
- package/_types.js.flow +20 -0
- package/_utils.js +108 -0
- package/_utils.js.flow +97 -0
- package/annotate.js +161 -0
- package/annotate.js.flow +218 -0
- package/core/array.js +108 -0
- package/core/array.js.flow +103 -0
- package/core/boolean.js +44 -0
- package/core/boolean.js.flow +29 -0
- package/core/composition.js +56 -0
- package/core/composition.js.flow +43 -0
- package/core/constants.js +69 -0
- package/core/constants.js.flow +46 -0
- package/core/date.js +46 -0
- package/core/date.js.flow +40 -0
- package/core/describe.js +26 -0
- package/core/describe.js.flow +17 -0
- package/core/dispatch.js +62 -0
- package/core/dispatch.js.flow +58 -0
- package/core/either.js +117 -0
- package/core/either.js.flow +151 -0
- package/core/fail.js +21 -0
- package/core/fail.js.flow +12 -0
- package/core/instanceOf.js +19 -0
- package/core/instanceOf.js.flow +20 -0
- package/{json.js → core/json.js} +3 -5
- package/core/json.js.flow +28 -0
- package/{lazy.js → core/lazy.js} +1 -3
- package/core/lazy.js.flow +15 -0
- package/core/mapping.js +67 -0
- package/core/mapping.js.flow +54 -0
- package/core/number.js +40 -0
- package/core/number.js.flow +34 -0
- package/core/object.js +194 -0
- package/core/object.js.flow +203 -0
- package/core/optional.js +54 -0
- package/core/optional.js.flow +41 -0
- package/core/string.js +98 -0
- package/core/string.js.flow +82 -0
- package/core/tuple.js +173 -0
- package/core/tuple.js.flow +220 -0
- package/format/index.js +12 -0
- package/format/index.js.flow +4 -0
- package/format/inline.js +146 -0
- package/format/inline.js.flow +122 -0
- package/format/short.js +10 -0
- package/format/short.js.flow +8 -0
- package/index.js +118 -395
- package/index.js.flow +24 -24
- package/package.json +2 -6
- package/result.js +172 -0
- package/result.js.flow +166 -0
- package/array.d.ts +0 -5
- package/array.js +0 -133
- package/boolean.d.ts +0 -5
- package/boolean.js +0 -42
- package/constants.d.ts +0 -11
- package/constants.js +0 -67
- package/date.d.ts +0 -4
- package/date.js +0 -42
- package/describe.d.ts +0 -3
- package/dispatch.d.ts +0 -8
- package/either.d.ts +0 -61
- package/either.js +0 -85
- package/fail.d.ts +0 -3
- package/fail.js +0 -19
- package/fail.js.flow +0 -13
- package/guard.d.ts +0 -7
- package/guard.js +0 -30
- package/guard.js.flow +0 -36
- package/helpers.d.ts +0 -79
- package/index.d.ts +0 -38
- package/instanceOf.d.ts +0 -3
- package/instanceOf.js +0 -17
- package/json.d.ts +0 -11
- package/lazy.d.ts +0 -3
- package/mapping.d.ts +0 -4
- package/mapping.js +0 -113
- package/mapping.js.flow +0 -71
- package/number.d.ts +0 -6
- package/number.js +0 -38
- package/object.d.ts +0 -33
- package/object.js +0 -254
- package/optional.d.ts +0 -5
- package/optional.js +0 -52
- package/string.d.ts +0 -7
- package/tuple.d.ts +0 -30
- package/tuple.js +0 -199
- package/types.d.ts +0 -18
- package/types.js +0 -5
- package/types.js.flow +0 -26
- package/utils.d.ts +0 -13
- package/utils.js +0 -70
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
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); }
|
|
2
|
+
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate, annotateObject, merge, updateText } from '../annotate';
|
|
5
|
+
import { compose, map } from './composition';
|
|
6
|
+
|
|
7
|
+
function isPojo(o) {
|
|
8
|
+
return o !== null && o !== undefined && typeof o === 'object' && // This still seems to be the only reliable way to determine whether
|
|
9
|
+
// something is a pojo... ¯\_(ツ)_/¯
|
|
10
|
+
// $FlowFixMe[method-unbinding]
|
|
11
|
+
Object.prototype.toString.call(o) === '[object Object]';
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function subtract(xs, ys) {
|
|
15
|
+
var result = new Set();
|
|
16
|
+
xs.forEach(function (x) {
|
|
17
|
+
if (!ys.has(x)) {
|
|
18
|
+
result.add(x);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export var pojo = function pojo(blob) {
|
|
25
|
+
return isPojo(blob) ? Result.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)) : Result.err(annotate(blob, 'Must be an object'));
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Given a mapping of fields-to-decoders, builds a decoder for an object type.
|
|
43
|
+
*
|
|
44
|
+
* For example, given decoders for a number and a string, we can construct an
|
|
45
|
+
* "object description" like so:
|
|
46
|
+
*
|
|
47
|
+
* { id: number, name: string }
|
|
48
|
+
*
|
|
49
|
+
* Which is of type:
|
|
50
|
+
*
|
|
51
|
+
* { id: Decoder<number>, name: Decoder<string> }
|
|
52
|
+
*
|
|
53
|
+
* Passing this to object() will produce the following return type:
|
|
54
|
+
*
|
|
55
|
+
* Decoder<{ id: number, name: string }>
|
|
56
|
+
*
|
|
57
|
+
* Put simply: it'll "peel off" all of the nested Decoders, puts them together
|
|
58
|
+
* in an object, and wraps it in a Decoder<...>.
|
|
59
|
+
*/
|
|
60
|
+
|
|
61
|
+
export function object(mapping) {
|
|
62
|
+
var known = new Set(Object.keys(mapping));
|
|
63
|
+
return compose(pojo, function (blob) {
|
|
64
|
+
var actual = new Set(Object.keys(blob)); // At this point, "missing" will also include all fields that may
|
|
65
|
+
// validly be optional. We'll let the underlying decoder decide and
|
|
66
|
+
// remove the key from this missing set if the decoder accepts the
|
|
67
|
+
// value.
|
|
68
|
+
|
|
69
|
+
var missing = subtract(known, actual);
|
|
70
|
+
var record = {};
|
|
71
|
+
var errors = null;
|
|
72
|
+
Object.keys(mapping).forEach(function (key) {
|
|
73
|
+
var decoder = mapping[key];
|
|
74
|
+
var rawValue = blob[key];
|
|
75
|
+
var result = decoder(rawValue);
|
|
76
|
+
|
|
77
|
+
if (result.type === 'ok') {
|
|
78
|
+
var value = result.value;
|
|
79
|
+
|
|
80
|
+
if (value !== undefined) {
|
|
81
|
+
record[key] = value;
|
|
82
|
+
} // If this succeeded, remove the key from the missing keys
|
|
83
|
+
// tracker
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
missing["delete"](key);
|
|
87
|
+
} else {
|
|
88
|
+
var ann = result.error; // Keep track of the annotation, but don't return just yet. We
|
|
89
|
+
// want to collect more error information.
|
|
90
|
+
|
|
91
|
+
if (rawValue === undefined) {
|
|
92
|
+
// Explicitly add it to the missing set if the value is
|
|
93
|
+
// undefined. This covers explicit undefineds to be
|
|
94
|
+
// treated the same as implicit undefineds (aka missing
|
|
95
|
+
// keys).
|
|
96
|
+
missing.add(key);
|
|
97
|
+
} else {
|
|
98
|
+
if (errors === null) {
|
|
99
|
+
errors = {};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
errors[key] = ann;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}); // Deal with errors now. There are two classes of errors we want to
|
|
106
|
+
// report. First of all, we want to report any inline errors in this
|
|
107
|
+
// object. Lastly, any fields that are missing should be annotated on
|
|
108
|
+
// the outer object itself.
|
|
109
|
+
|
|
110
|
+
if (errors || missing.size > 0) {
|
|
111
|
+
var objAnn = annotateObject(blob);
|
|
112
|
+
|
|
113
|
+
if (errors) {
|
|
114
|
+
objAnn = merge(objAnn, errors);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (missing.size > 0) {
|
|
118
|
+
var errMsg = Array.from(missing).map(function (key) {
|
|
119
|
+
return "\"" + key + "\"";
|
|
120
|
+
}).join(', ');
|
|
121
|
+
var pluralized = missing.size > 1 ? 'keys' : 'key';
|
|
122
|
+
objAnn = updateText(objAnn, "Missing " + pluralized + ": " + errMsg);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
return Result.err(objAnn);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return Result.ok(record);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
export function exact(mapping) {
|
|
132
|
+
// Check the inputted object for any superfluous keys
|
|
133
|
+
var allowed = new Set(Object.keys(mapping));
|
|
134
|
+
var checked = compose(pojo, function (blob) {
|
|
135
|
+
var actual = new Set(Object.keys(blob));
|
|
136
|
+
var superfluous = subtract(actual, allowed);
|
|
137
|
+
|
|
138
|
+
if (superfluous.size > 0) {
|
|
139
|
+
return Result.err(annotate(blob, "Superfluous keys: " + Array.from(superfluous).join(', ')));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return Result.ok(blob);
|
|
143
|
+
}); // Defer to the "object" decoder for doing the real decoding work. Since
|
|
144
|
+
// we made sure there are no superfluous keys in this structure, it's now
|
|
145
|
+
// safe to force-cast it to an $Exact<> type.
|
|
146
|
+
|
|
147
|
+
var decoder = object(mapping);
|
|
148
|
+
return compose(checked, decoder);
|
|
149
|
+
}
|
|
150
|
+
export function inexact(mapping) {
|
|
151
|
+
return compose(pojo, function (blob) {
|
|
152
|
+
var allkeys = new Set(Object.keys(blob));
|
|
153
|
+
var decoder = map(object(mapping), function (safepart) {
|
|
154
|
+
var safekeys = new Set(Object.keys(mapping)); // To account for hard-coded keys that aren't part of the input
|
|
155
|
+
|
|
156
|
+
safekeys.forEach(function (k) {
|
|
157
|
+
return allkeys.add(k);
|
|
158
|
+
});
|
|
159
|
+
var rv = {};
|
|
160
|
+
allkeys.forEach(function (k) {
|
|
161
|
+
if (safekeys.has(k)) {
|
|
162
|
+
var value = safepart[k];
|
|
163
|
+
|
|
164
|
+
if (value !== undefined) {
|
|
165
|
+
rv[k] = value;
|
|
166
|
+
}
|
|
167
|
+
} else {
|
|
168
|
+
rv[k] = blob[k];
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
return rv;
|
|
172
|
+
});
|
|
173
|
+
return decoder(blob);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
// @flow strict
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
import type {
|
|
8
|
-
import { compose, map } from './utils';
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate, annotateObject, merge, updateText } from '../annotate';
|
|
5
|
+
import { compose, map } from './composition';
|
|
6
|
+
import type { Annotation } from '../annotate';
|
|
7
|
+
import type { Decoder, DecoderType } from '../_types';
|
|
9
8
|
|
|
10
9
|
// $FlowFixMe[unclear-type] (not really an issue) - deliberate use of `any` - not sure how we should get rid of this
|
|
11
10
|
type AnyDecoder = any;
|
|
@@ -27,17 +26,17 @@ function isPojo(o: mixed): boolean %checks {
|
|
|
27
26
|
|
|
28
27
|
function subtract(xs: Set<string>, ys: Set<string>): Set<string> {
|
|
29
28
|
const result = new Set();
|
|
30
|
-
|
|
29
|
+
xs.forEach((x) => {
|
|
31
30
|
if (!ys.has(x)) {
|
|
32
31
|
result.add(x);
|
|
33
32
|
}
|
|
34
|
-
}
|
|
33
|
+
});
|
|
35
34
|
return result;
|
|
36
35
|
}
|
|
37
36
|
|
|
38
37
|
export const pojo: Decoder<{| [string]: mixed |}> = (blob: mixed) => {
|
|
39
38
|
return isPojo(blob)
|
|
40
|
-
?
|
|
39
|
+
? Result.ok(
|
|
41
40
|
// NOTE:
|
|
42
41
|
// Since Flow 0.98, typeof o === 'object' refines to
|
|
43
42
|
// {| +[string]: mixed |}
|
|
@@ -52,9 +51,9 @@ export const pojo: Decoder<{| [string]: mixed |}> = (blob: mixed) => {
|
|
|
52
51
|
// way to turn a read-only Object to a writeable one in ES6 seems
|
|
53
52
|
// to be to use object-spread. (Going off this benchmark:
|
|
54
53
|
// https://thecodebarbarian.com/object-assign-vs-object-spread.html)
|
|
55
|
-
{ ...blob }
|
|
54
|
+
{ ...blob },
|
|
56
55
|
)
|
|
57
|
-
:
|
|
56
|
+
: Result.err(annotate(blob, 'Must be an object'));
|
|
58
57
|
};
|
|
59
58
|
|
|
60
59
|
/**
|
|
@@ -77,10 +76,10 @@ export const pojo: Decoder<{| [string]: mixed |}> = (blob: mixed) => {
|
|
|
77
76
|
* in an object, and wraps it in a Decoder<...>.
|
|
78
77
|
*/
|
|
79
78
|
export function object<O: { +[field: string]: AnyDecoder, ... }>(
|
|
80
|
-
mapping: O
|
|
81
|
-
): Decoder<$ObjMap<O,
|
|
79
|
+
mapping: O,
|
|
80
|
+
): Decoder<$ObjMap<O, DecoderType>> {
|
|
82
81
|
const known = new Set(Object.keys(mapping));
|
|
83
|
-
return compose(pojo, (blob: {
|
|
82
|
+
return compose(pojo, (blob: { +[string]: mixed }) => {
|
|
84
83
|
const actual = new Set(Object.keys(blob));
|
|
85
84
|
|
|
86
85
|
// At this point, "missing" will also include all fields that may
|
|
@@ -90,16 +89,15 @@ export function object<O: { +[field: string]: AnyDecoder, ... }>(
|
|
|
90
89
|
const missing = subtract(known, actual);
|
|
91
90
|
|
|
92
91
|
let record = {};
|
|
93
|
-
|
|
92
|
+
let errors: { [key: string]: Annotation } | null = null;
|
|
94
93
|
|
|
95
|
-
|
|
96
|
-
// will type the value part as "mixed"
|
|
97
|
-
for (const key of Object.keys(mapping)) {
|
|
94
|
+
Object.keys(mapping).forEach((key) => {
|
|
98
95
|
const decoder = mapping[key];
|
|
99
96
|
const rawValue = blob[key];
|
|
100
97
|
const result = decoder(rawValue);
|
|
101
|
-
|
|
102
|
-
|
|
98
|
+
|
|
99
|
+
if (result.type === 'ok') {
|
|
100
|
+
const value = result.value;
|
|
103
101
|
if (value !== undefined) {
|
|
104
102
|
record[key] = value;
|
|
105
103
|
}
|
|
@@ -107,11 +105,8 @@ export function object<O: { +[field: string]: AnyDecoder, ... }>(
|
|
|
107
105
|
// If this succeeded, remove the key from the missing keys
|
|
108
106
|
// tracker
|
|
109
107
|
missing.delete(key);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
if (!isAnnotation(ann)) {
|
|
113
|
-
throw ann;
|
|
114
|
-
}
|
|
108
|
+
} else {
|
|
109
|
+
const ann = result.error;
|
|
115
110
|
|
|
116
111
|
// Keep track of the annotation, but don't return just yet. We
|
|
117
112
|
// want to collect more error information.
|
|
@@ -122,24 +117,23 @@ export function object<O: { +[field: string]: AnyDecoder, ... }>(
|
|
|
122
117
|
// keys).
|
|
123
118
|
missing.add(key);
|
|
124
119
|
} else {
|
|
125
|
-
|
|
120
|
+
if (errors === null) {
|
|
121
|
+
errors = {};
|
|
122
|
+
}
|
|
123
|
+
errors[key] = ann;
|
|
126
124
|
}
|
|
127
125
|
}
|
|
128
|
-
}
|
|
126
|
+
});
|
|
129
127
|
|
|
130
128
|
// Deal with errors now. There are two classes of errors we want to
|
|
131
129
|
// report. First of all, we want to report any inline errors in this
|
|
132
130
|
// object. Lastly, any fields that are missing should be annotated on
|
|
133
131
|
// the outer object itself.
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
let err;
|
|
132
|
+
if (errors || missing.size > 0) {
|
|
133
|
+
let objAnn = annotateObject(blob);
|
|
137
134
|
|
|
138
|
-
if (
|
|
139
|
-
|
|
140
|
-
err = annotateFields(blob, errorlist);
|
|
141
|
-
} else {
|
|
142
|
-
err = annotate(blob);
|
|
135
|
+
if (errors) {
|
|
136
|
+
objAnn = merge(objAnn, errors);
|
|
143
137
|
}
|
|
144
138
|
|
|
145
139
|
if (missing.size > 0) {
|
|
@@ -147,54 +141,52 @@ export function object<O: { +[field: string]: AnyDecoder, ... }>(
|
|
|
147
141
|
.map((key) => `"${key}"`)
|
|
148
142
|
.join(', ');
|
|
149
143
|
const pluralized = missing.size > 1 ? 'keys' : 'key';
|
|
150
|
-
|
|
144
|
+
objAnn = updateText(objAnn, `Missing ${pluralized}: ${errMsg}`);
|
|
151
145
|
}
|
|
152
146
|
|
|
153
|
-
return
|
|
147
|
+
return Result.err(objAnn);
|
|
154
148
|
}
|
|
155
149
|
|
|
156
|
-
return
|
|
150
|
+
return Result.ok(record);
|
|
157
151
|
});
|
|
158
152
|
}
|
|
159
153
|
|
|
160
154
|
export function exact<O: { +[field: string]: AnyDecoder, ... }>(
|
|
161
|
-
mapping: O
|
|
162
|
-
): Decoder<$ObjMap<$Exact<O>,
|
|
155
|
+
mapping: O,
|
|
156
|
+
): Decoder<$ObjMap<$Exact<O>, DecoderType>> {
|
|
163
157
|
// Check the inputted object for any superfluous keys
|
|
164
158
|
const allowed = new Set(Object.keys(mapping));
|
|
165
159
|
const checked = compose(pojo, (blob: {| [string]: mixed |}) => {
|
|
166
160
|
const actual = new Set(Object.keys(blob));
|
|
167
161
|
const superfluous = subtract(actual, allowed);
|
|
168
162
|
if (superfluous.size > 0) {
|
|
169
|
-
return
|
|
170
|
-
annotate(blob, `Superfluous keys: ${Array.from(superfluous).join(', ')}`)
|
|
163
|
+
return Result.err(
|
|
164
|
+
annotate(blob, `Superfluous keys: ${Array.from(superfluous).join(', ')}`),
|
|
171
165
|
);
|
|
172
166
|
}
|
|
173
|
-
return
|
|
167
|
+
return Result.ok(blob);
|
|
174
168
|
});
|
|
175
169
|
|
|
176
170
|
// Defer to the "object" decoder for doing the real decoding work. Since
|
|
177
171
|
// we made sure there are no superfluous keys in this structure, it's now
|
|
178
172
|
// safe to force-cast it to an $Exact<> type.
|
|
179
|
-
const decoder = ((object(mapping): cast): Decoder<$ObjMap<$Exact<O>,
|
|
173
|
+
const decoder = ((object(mapping): cast): Decoder<$ObjMap<$Exact<O>, DecoderType>>);
|
|
180
174
|
return compose(checked, decoder);
|
|
181
175
|
}
|
|
182
176
|
|
|
183
177
|
export function inexact<O: { +[field: string]: AnyDecoder }>(
|
|
184
|
-
mapping: O
|
|
185
|
-
): Decoder<$ObjMap<O,
|
|
178
|
+
mapping: O,
|
|
179
|
+
): Decoder<$ObjMap<O, DecoderType> & { +[string]: mixed }> {
|
|
186
180
|
return compose(pojo, (blob: {| [string]: mixed |}) => {
|
|
187
181
|
const allkeys = new Set(Object.keys(blob));
|
|
188
|
-
const decoder = map(object(mapping), (safepart: $ObjMap<O,
|
|
182
|
+
const decoder = map(object(mapping), (safepart: $ObjMap<O, DecoderType>) => {
|
|
189
183
|
const safekeys = new Set(Object.keys(mapping));
|
|
190
184
|
|
|
191
185
|
// To account for hard-coded keys that aren't part of the input
|
|
192
|
-
|
|
193
|
-
allkeys.add(k);
|
|
194
|
-
}
|
|
186
|
+
safekeys.forEach((k) => allkeys.add(k));
|
|
195
187
|
|
|
196
188
|
const rv = {};
|
|
197
|
-
|
|
189
|
+
allkeys.forEach((k) => {
|
|
198
190
|
if (safekeys.has(k)) {
|
|
199
191
|
const value = safepart[k];
|
|
200
192
|
if (value !== undefined) {
|
|
@@ -203,7 +195,7 @@ export function inexact<O: { +[field: string]: AnyDecoder }>(
|
|
|
203
195
|
} else {
|
|
204
196
|
rv[k] = blob[k];
|
|
205
197
|
}
|
|
206
|
-
}
|
|
198
|
+
});
|
|
207
199
|
return rv;
|
|
208
200
|
});
|
|
209
201
|
return decoder(blob);
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import * as Result from '../result';
|
|
2
|
+
import { annotate } from '../annotate';
|
|
3
|
+
import { either } from './either';
|
|
4
|
+
import { null_, undefined_ } from './constants';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Builds a Decoder that returns Ok for either `undefined` or `T` values,
|
|
8
|
+
* given a Decoder for `T`. Err otherwise.
|
|
9
|
+
*/
|
|
10
|
+
export function optional(decoder) {
|
|
11
|
+
return either(undefined_, decoder);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Builds a Decoder that returns Ok for either `null` or `T` values,
|
|
15
|
+
* given a Decoder for `T`. Err otherwise.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export function nullable(decoder) {
|
|
19
|
+
return either(null_, decoder);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Decoder that only returns Ok for `null` or `undefined` inputs.
|
|
23
|
+
* This is effectively equivalent to either(null_, undefined_), but combines
|
|
24
|
+
* their error message output into a single line for convenience.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
var undefined_or_null = function undefined_or_null(blob) {
|
|
28
|
+
return blob === undefined || blob === null ? Result.ok(blob) : // Combine error message into a single line
|
|
29
|
+
Result.err(annotate(blob, 'Must be undefined or null'));
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Decoder that only returns Ok for `null` or `undefined` inputs.
|
|
33
|
+
*/
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
export function maybe(decoder) {
|
|
37
|
+
return either(undefined_or_null, decoder);
|
|
38
|
+
}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
// @flow strict
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
import { null_, undefined_ } from './constants';
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
7
5
|
import { either } from './either';
|
|
8
|
-
import
|
|
6
|
+
import { null_, undefined_ } from './constants';
|
|
7
|
+
import type { Decoder } from '../_types';
|
|
9
8
|
|
|
10
9
|
/**
|
|
11
10
|
* Builds a Decoder that returns Ok for either `undefined` or `T` values,
|
|
@@ -30,9 +29,9 @@ export function nullable<T>(decoder: Decoder<T>): Decoder<null | T> {
|
|
|
30
29
|
*/
|
|
31
30
|
const undefined_or_null: Decoder<null | void> = (blob: mixed) =>
|
|
32
31
|
blob === undefined || blob === null
|
|
33
|
-
?
|
|
32
|
+
? Result.ok(blob)
|
|
34
33
|
: // Combine error message into a single line
|
|
35
|
-
|
|
34
|
+
Result.err(annotate(blob, 'Must be undefined or null'));
|
|
36
35
|
|
|
37
36
|
/**
|
|
38
37
|
* Decoder that only returns Ok for `null` or `undefined` inputs.
|
|
@@ -1,17 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.nonEmptyString = exports.email = void 0;
|
|
7
|
-
exports.regex = regex;
|
|
8
|
-
exports.url = exports.string = void 0;
|
|
9
|
-
|
|
10
|
-
var _debrief = require("debrief");
|
|
11
|
-
|
|
12
|
-
var _Result = require("lemons/Result");
|
|
13
|
-
|
|
14
|
-
var _utils = require("./utils");
|
|
1
|
+
import * as Result from '../result';
|
|
2
|
+
import { annotate } from '../annotate';
|
|
3
|
+
import { compose, predicate } from './composition';
|
|
15
4
|
|
|
16
5
|
/** Match groups in this regex:
|
|
17
6
|
* \1 - the scheme
|
|
@@ -27,26 +16,22 @@ var DEFAULT_SCHEMES = ['https'];
|
|
|
27
16
|
* Decoder that only returns Ok for string inputs. Err otherwise.
|
|
28
17
|
*/
|
|
29
18
|
|
|
30
|
-
var string = function string(blob) {
|
|
31
|
-
return typeof blob === 'string' ?
|
|
19
|
+
export var string = function string(blob) {
|
|
20
|
+
return typeof blob === 'string' ? Result.ok(blob) : Result.err(annotate(blob, 'Must be string'));
|
|
32
21
|
};
|
|
33
22
|
/**
|
|
34
23
|
* Decoder that only returns Ok for non-empty string inputs. Err otherwise.
|
|
35
24
|
*/
|
|
36
25
|
|
|
37
|
-
|
|
38
|
-
exports.string = string;
|
|
39
|
-
var nonEmptyString = regex(/\S/, 'Must be non-empty string');
|
|
26
|
+
export var nonEmptyString = regex(/\S/, 'Must be non-empty string');
|
|
40
27
|
/**
|
|
41
28
|
* Decoder that only returns Ok for string inputs that match the regular
|
|
42
29
|
* expression. Err otherwise. Will always validate that the input is a string
|
|
43
30
|
* before testing the regex.
|
|
44
31
|
*/
|
|
45
32
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
function regex(regex, msg) {
|
|
49
|
-
return (0, _utils.compose)(string, (0, _utils.predicate)(function (s) {
|
|
33
|
+
export function regex(regex, msg) {
|
|
34
|
+
return compose(string, predicate(function (s) {
|
|
50
35
|
return regex.test(s);
|
|
51
36
|
}, msg));
|
|
52
37
|
}
|
|
@@ -55,8 +40,7 @@ function regex(regex, msg) {
|
|
|
55
40
|
* email regex, taken from http://emailregex.com. Err otherwise.
|
|
56
41
|
*/
|
|
57
42
|
|
|
58
|
-
|
|
59
|
-
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');
|
|
43
|
+
export var email = regex(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, 'Must be email');
|
|
60
44
|
/**
|
|
61
45
|
* Decoder that only returns Ok for string inputs that match URLs of the
|
|
62
46
|
* expected scheme. Defaults to only accept HTTPS URLs. Err otherwise.
|
|
@@ -69,25 +53,24 @@ var email = regex(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@(
|
|
|
69
53
|
* - url(['https', 'git+ssh']) accepts both https:// and git+ssh:// URLs
|
|
70
54
|
*/
|
|
71
55
|
|
|
72
|
-
|
|
56
|
+
export var url = function url(schemes) {
|
|
57
|
+
if (schemes === void 0) {
|
|
58
|
+
schemes = DEFAULT_SCHEMES;
|
|
59
|
+
}
|
|
73
60
|
|
|
74
|
-
|
|
75
|
-
var schemes = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : DEFAULT_SCHEMES;
|
|
76
|
-
return (0, _utils.compose)(string, function (value) {
|
|
61
|
+
return compose(string, function (value) {
|
|
77
62
|
var matches = value.match(url_re);
|
|
78
63
|
|
|
79
64
|
if (!matches) {
|
|
80
|
-
return
|
|
65
|
+
return Result.err(annotate(value, 'Must be URL'));
|
|
81
66
|
} else {
|
|
82
67
|
var scheme = matches[1];
|
|
83
68
|
|
|
84
69
|
if (schemes.length === 0 || schemes.includes(scheme.toLowerCase())) {
|
|
85
|
-
return
|
|
70
|
+
return Result.ok(value);
|
|
86
71
|
} else {
|
|
87
|
-
return
|
|
72
|
+
return Result.err(annotate(value, "URL scheme must be any of: " + schemes.join(', ')));
|
|
88
73
|
}
|
|
89
74
|
}
|
|
90
75
|
});
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
exports.url = url;
|
|
76
|
+
};
|
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
// @flow strict
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
import type { Decoder } from '
|
|
7
|
-
import { compose, predicate } from './utils';
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
5
|
+
import { compose, predicate } from './composition';
|
|
6
|
+
import type { Decoder } from '../_types';
|
|
8
7
|
|
|
9
8
|
/** Match groups in this regex:
|
|
10
9
|
* \1 - the scheme
|
|
@@ -23,7 +22,9 @@ const DEFAULT_SCHEMES = ['https'];
|
|
|
23
22
|
* Decoder that only returns Ok for string inputs. Err otherwise.
|
|
24
23
|
*/
|
|
25
24
|
export const string: Decoder<string> = (blob: mixed) => {
|
|
26
|
-
return typeof blob === 'string'
|
|
25
|
+
return typeof blob === 'string'
|
|
26
|
+
? Result.ok(blob)
|
|
27
|
+
: Result.err(annotate(blob, 'Must be string'));
|
|
27
28
|
};
|
|
28
29
|
|
|
29
30
|
/**
|
|
@@ -39,7 +40,7 @@ export const nonEmptyString: Decoder<string> = regex(/\S/, 'Must be non-empty st
|
|
|
39
40
|
export function regex(regex: RegExp, msg: string): Decoder<string> {
|
|
40
41
|
return compose(
|
|
41
42
|
string,
|
|
42
|
-
predicate((s) => regex.test(s), msg)
|
|
43
|
+
predicate((s) => regex.test(s), msg),
|
|
43
44
|
);
|
|
44
45
|
}
|
|
45
46
|
|
|
@@ -49,7 +50,7 @@ export function regex(regex: RegExp, msg: string): Decoder<string> {
|
|
|
49
50
|
*/
|
|
50
51
|
export const email: Decoder<string> = regex(
|
|
51
52
|
/^(([^<>()[\]\\.,;:\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,}))$/,
|
|
52
|
-
'Must be email'
|
|
53
|
+
'Must be email',
|
|
53
54
|
);
|
|
54
55
|
|
|
55
56
|
/**
|
|
@@ -67,14 +68,14 @@ export const url = (schemes: $ReadOnlyArray<string> = DEFAULT_SCHEMES): Decoder<
|
|
|
67
68
|
compose(string, (value: string) => {
|
|
68
69
|
const matches = value.match(url_re);
|
|
69
70
|
if (!matches) {
|
|
70
|
-
return
|
|
71
|
+
return Result.err(annotate(value, 'Must be URL'));
|
|
71
72
|
} else {
|
|
72
73
|
const scheme = matches[1];
|
|
73
74
|
if (schemes.length === 0 || schemes.includes(scheme.toLowerCase())) {
|
|
74
|
-
return
|
|
75
|
+
return Result.ok(value);
|
|
75
76
|
} else {
|
|
76
|
-
return
|
|
77
|
-
annotate(value, `URL scheme must be any of: ${schemes.join(', ')}`)
|
|
77
|
+
return Result.err(
|
|
78
|
+
annotate(value, `URL scheme must be any of: ${schemes.join(', ')}`),
|
|
78
79
|
);
|
|
79
80
|
}
|
|
80
81
|
}
|