decoders 2.0.0-beta1 → 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/{es → _esm}/_guard.js +0 -0
- package/{cjs → _esm}/_guard.js.flow +0 -0
- package/{es → _esm}/_types.js +0 -0
- package/{cjs → _esm}/_types.js.flow +0 -0
- package/{es → _esm}/_utils.js +0 -0
- package/{cjs → _esm}/_utils.js.flow +0 -0
- package/{es → _esm}/annotate.js +0 -0
- package/{cjs → _esm}/annotate.js.flow +0 -0
- package/{es/stdlib → _esm/core}/array.js +0 -0
- package/{cjs/stdlib → _esm/core}/array.js.flow +0 -0
- package/{es/stdlib → _esm/core}/boolean.js +0 -0
- package/{cjs/stdlib → _esm/core}/boolean.js.flow +0 -0
- package/{es/stdlib → _esm/core}/composition.js +0 -0
- package/{cjs/stdlib → _esm/core}/composition.js.flow +0 -0
- package/{es/stdlib → _esm/core}/constants.js +0 -0
- package/{cjs/stdlib → _esm/core}/constants.js.flow +0 -0
- package/{es/stdlib → _esm/core}/date.js +0 -0
- package/{cjs/stdlib → _esm/core}/date.js.flow +0 -0
- package/{es/stdlib → _esm/core}/describe.js +0 -0
- package/{cjs/stdlib → _esm/core}/describe.js.flow +0 -0
- package/{es/stdlib → _esm/core}/dispatch.js +0 -0
- package/{cjs/stdlib → _esm/core}/dispatch.js.flow +0 -0
- package/{es/stdlib → _esm/core}/either.js +0 -0
- package/{cjs/stdlib → _esm/core}/either.js.flow +0 -0
- package/{es/stdlib → _esm/core}/fail.js +0 -0
- package/{cjs/stdlib → _esm/core}/fail.js.flow +0 -0
- package/{es/stdlib → _esm/core}/instanceOf.js +0 -0
- package/{cjs/stdlib → _esm/core}/instanceOf.js.flow +0 -0
- package/{es/stdlib → _esm/core}/json.js +0 -0
- package/{cjs/stdlib → _esm/core}/json.js.flow +0 -0
- package/{es/stdlib → _esm/core}/lazy.js +0 -0
- package/{cjs/stdlib → _esm/core}/lazy.js.flow +0 -0
- package/{es/stdlib → _esm/core}/mapping.js +0 -0
- package/{cjs/stdlib → _esm/core}/mapping.js.flow +0 -0
- package/{es/stdlib → _esm/core}/number.js +0 -0
- package/{cjs/stdlib → _esm/core}/number.js.flow +0 -0
- package/{es/stdlib → _esm/core}/object.js +0 -0
- package/{cjs/stdlib → _esm/core}/object.js.flow +0 -0
- package/{es/stdlib → _esm/core}/optional.js +0 -0
- package/{cjs/stdlib → _esm/core}/optional.js.flow +0 -0
- package/{es/stdlib → _esm/core}/string.js +0 -0
- package/{cjs/stdlib → _esm/core}/string.js.flow +0 -0
- package/{es/stdlib → _esm/core}/tuple.js +0 -0
- package/{cjs/stdlib → _esm/core}/tuple.js.flow +0 -0
- package/{es → _esm}/format/index.js +0 -0
- package/{cjs → _esm}/format/index.js.flow +0 -0
- package/{es → _esm}/format/inline.js +0 -0
- package/{cjs → _esm}/format/inline.js.flow +0 -0
- package/{es → _esm}/format/short.js +0 -0
- package/{cjs → _esm}/format/short.js.flow +0 -0
- package/{es → _esm}/index.js +18 -18
- package/{cjs → _esm}/index.js.flow +19 -26
- package/{es → _esm}/result.js +0 -0
- package/{cjs → _esm}/result.js.flow +0 -0
- package/{cjs/_guard.js → _guard.js} +0 -0
- package/_guard.js.flow +20 -0
- package/{cjs/_types.js → _types.js} +0 -0
- package/_types.js.flow +20 -0
- package/{cjs/_utils.js → _utils.js} +0 -0
- package/_utils.js.flow +97 -0
- package/{cjs/annotate.js → annotate.js} +0 -0
- package/annotate.js.flow +218 -0
- package/{cjs/stdlib → core}/array.js +0 -0
- package/core/array.js.flow +103 -0
- package/{cjs/stdlib → core}/boolean.js +0 -0
- package/core/boolean.js.flow +29 -0
- package/{cjs/stdlib → core}/composition.js +0 -0
- package/core/composition.js.flow +43 -0
- package/{cjs/stdlib → core}/constants.js +0 -0
- package/core/constants.js.flow +46 -0
- package/{cjs/stdlib → core}/date.js +0 -0
- package/core/date.js.flow +40 -0
- package/{cjs/stdlib → core}/describe.js +0 -0
- package/core/describe.js.flow +17 -0
- package/{cjs/stdlib → core}/dispatch.js +0 -0
- package/core/dispatch.js.flow +58 -0
- package/{cjs/stdlib → core}/either.js +0 -0
- package/core/either.js.flow +151 -0
- package/{cjs/stdlib → core}/fail.js +0 -0
- package/core/fail.js.flow +12 -0
- package/{cjs/stdlib → core}/instanceOf.js +0 -0
- package/core/instanceOf.js.flow +20 -0
- package/{cjs/stdlib → core}/json.js +0 -0
- package/core/json.js.flow +28 -0
- package/{cjs/stdlib → core}/lazy.js +0 -0
- package/core/lazy.js.flow +15 -0
- package/{cjs/stdlib → core}/mapping.js +0 -0
- package/core/mapping.js.flow +54 -0
- package/{cjs/stdlib → core}/number.js +0 -0
- package/core/number.js.flow +34 -0
- package/{cjs/stdlib → core}/object.js +0 -0
- package/core/object.js.flow +203 -0
- package/{cjs/stdlib → core}/optional.js +0 -0
- package/core/optional.js.flow +41 -0
- package/{cjs/stdlib → core}/string.js +0 -0
- package/core/string.js.flow +82 -0
- package/{cjs/stdlib → core}/tuple.js +0 -0
- package/core/tuple.js.flow +220 -0
- package/{cjs/format → format}/index.js +0 -0
- package/format/index.js.flow +4 -0
- package/{cjs/format → format}/inline.js +0 -0
- package/format/inline.js.flow +122 -0
- package/{cjs/format → format}/short.js +0 -0
- package/format/short.js.flow +8 -0
- package/{cjs/index.js → index.js} +18 -18
- package/index.js.flow +56 -0
- package/package.json +3 -12
- package/{cjs/result.js → result.js} +0 -0
- package/result.js.flow +166 -0
- package/ts/_guard.d.ts +0 -7
- package/ts/_helpers.d.ts +0 -79
- package/ts/_types.d.ts +0 -16
- package/ts/_utils.d.ts +0 -13
- package/ts/annotate.d.ts +0 -58
- package/ts/array.d.ts +0 -5
- package/ts/boolean.d.ts +0 -5
- package/ts/constants.d.ts +0 -11
- package/ts/date.d.ts +0 -4
- package/ts/describe.d.ts +0 -3
- package/ts/dispatch.d.ts +0 -8
- package/ts/either.d.ts +0 -61
- package/ts/fail.d.ts +0 -3
- package/ts/index.d.ts +0 -42
- package/ts/inline.d.ts +0 -3
- package/ts/instanceOf.d.ts +0 -3
- package/ts/json.d.ts +0 -11
- package/ts/lazy.d.ts +0 -3
- package/ts/mapping.d.ts +0 -4
- package/ts/number.d.ts +0 -6
- package/ts/object.d.ts +0 -33
- package/ts/optional.d.ts +0 -5
- package/ts/result.d.ts +0 -39
- package/ts/short.d.ts +0 -3
- package/ts/string.d.ts +0 -7
- package/ts/tuple.d.ts +0 -30
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
5
|
+
import { compose, 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
|
+
? Result.ok(blob)
|
|
11
|
+
: Result.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> = compose(
|
|
18
|
+
anyNumber,
|
|
19
|
+
predicate(isFinite, 'Number must be finite'),
|
|
20
|
+
);
|
|
21
|
+
export const positiveNumber: Decoder<number> = compose(
|
|
22
|
+
number,
|
|
23
|
+
predicate((n) => n >= 0, 'Number must be positive'),
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
// Integers
|
|
27
|
+
export const integer: Decoder<number> = compose(
|
|
28
|
+
number,
|
|
29
|
+
predicate(isInteger, 'Number must be an integer'),
|
|
30
|
+
);
|
|
31
|
+
export const positiveInteger: Decoder<number> = compose(
|
|
32
|
+
integer,
|
|
33
|
+
predicate((n) => n >= 0, 'Number must be positive'),
|
|
34
|
+
);
|
|
File without changes
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
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';
|
|
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
|
+
? Result.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
|
+
: Result.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 Result.err(objAnn);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return Result.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 Result.err(
|
|
164
|
+
annotate(blob, `Superfluous keys: ${Array.from(superfluous).join(', ')}`),
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
return Result.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
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
5
|
+
import { either } from './either';
|
|
6
|
+
import { null_, undefined_ } from './constants';
|
|
7
|
+
import type { Decoder } from '../_types';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Builds a Decoder that returns Ok for either `undefined` or `T` values,
|
|
11
|
+
* given a Decoder for `T`. Err otherwise.
|
|
12
|
+
*/
|
|
13
|
+
export function optional<T>(decoder: Decoder<T>): Decoder<void | T> {
|
|
14
|
+
return either(undefined_, decoder);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Builds a Decoder that returns Ok for either `null` or `T` values,
|
|
19
|
+
* given a Decoder for `T`. Err otherwise.
|
|
20
|
+
*/
|
|
21
|
+
export function nullable<T>(decoder: Decoder<T>): Decoder<null | T> {
|
|
22
|
+
return either(null_, decoder);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Decoder that only returns Ok for `null` or `undefined` inputs.
|
|
27
|
+
* This is effectively equivalent to either(null_, undefined_), but combines
|
|
28
|
+
* their error message output into a single line for convenience.
|
|
29
|
+
*/
|
|
30
|
+
const undefined_or_null: Decoder<null | void> = (blob: mixed) =>
|
|
31
|
+
blob === undefined || blob === null
|
|
32
|
+
? Result.ok(blob)
|
|
33
|
+
: // Combine error message into a single line
|
|
34
|
+
Result.err(annotate(blob, 'Must be undefined or null'));
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Decoder that only returns Ok for `null` or `undefined` inputs.
|
|
38
|
+
*/
|
|
39
|
+
export function maybe<T>(decoder: Decoder<T>): Decoder<?T> {
|
|
40
|
+
return either(undefined_or_null, decoder);
|
|
41
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
5
|
+
import { compose, predicate } from './composition';
|
|
6
|
+
import type { Decoder } from '../_types';
|
|
7
|
+
|
|
8
|
+
/** Match groups in this regex:
|
|
9
|
+
* \1 - the scheme
|
|
10
|
+
* \2 - the username/password (optional)
|
|
11
|
+
* \3 - the host
|
|
12
|
+
* \4 - the port (optional)
|
|
13
|
+
* \5 - the path (optional)
|
|
14
|
+
*/
|
|
15
|
+
const url_re =
|
|
16
|
+
/^([A-Za-z]{3,9}(?:[+][A-Za-z]{3,9})?):\/\/(?:([-;:&=+$,\w]+)@)?(?:([A-Za-z0-9.-]+)(?::([0-9]{2,5}))?)(\/(?:[-+~%/.,\w]*)?(?:\?[-+=&;%@.,\w]*)?(?:#[.,!/\w]*)?)?$/;
|
|
17
|
+
|
|
18
|
+
// The URL schemes the url() decoder accepts by default
|
|
19
|
+
const DEFAULT_SCHEMES = ['https'];
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Decoder that only returns Ok for string inputs. Err otherwise.
|
|
23
|
+
*/
|
|
24
|
+
export const string: Decoder<string> = (blob: mixed) => {
|
|
25
|
+
return typeof blob === 'string'
|
|
26
|
+
? Result.ok(blob)
|
|
27
|
+
: Result.err(annotate(blob, 'Must be string'));
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Decoder that only returns Ok for non-empty string inputs. Err otherwise.
|
|
32
|
+
*/
|
|
33
|
+
export const nonEmptyString: Decoder<string> = regex(/\S/, 'Must be non-empty string');
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Decoder that only returns Ok for string inputs that match the regular
|
|
37
|
+
* expression. Err otherwise. Will always validate that the input is a string
|
|
38
|
+
* before testing the regex.
|
|
39
|
+
*/
|
|
40
|
+
export function regex(regex: RegExp, msg: string): Decoder<string> {
|
|
41
|
+
return compose(
|
|
42
|
+
string,
|
|
43
|
+
predicate((s) => regex.test(s), msg),
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Decoder that only returns Ok for string inputs that match the almost perfect
|
|
49
|
+
* email regex, taken from http://emailregex.com. Err otherwise.
|
|
50
|
+
*/
|
|
51
|
+
export const email: Decoder<string> = regex(
|
|
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,}))$/,
|
|
53
|
+
'Must be email',
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Decoder that only returns Ok for string inputs that match URLs of the
|
|
58
|
+
* expected scheme. Defaults to only accept HTTPS URLs. Err otherwise.
|
|
59
|
+
*
|
|
60
|
+
* Variants that can be used:
|
|
61
|
+
*
|
|
62
|
+
* - url() accepts only https:// URLs
|
|
63
|
+
* - url([]) accepts any URL scheme
|
|
64
|
+
* - url(['http']) accepts only HTTP
|
|
65
|
+
* - url(['https', 'git+ssh']) accepts both https:// and git+ssh:// URLs
|
|
66
|
+
*/
|
|
67
|
+
export const url = (schemes: $ReadOnlyArray<string> = DEFAULT_SCHEMES): Decoder<string> =>
|
|
68
|
+
compose(string, (value: string) => {
|
|
69
|
+
const matches = value.match(url_re);
|
|
70
|
+
if (!matches) {
|
|
71
|
+
return Result.err(annotate(value, 'Must be URL'));
|
|
72
|
+
} else {
|
|
73
|
+
const scheme = matches[1];
|
|
74
|
+
if (schemes.length === 0 || schemes.includes(scheme.toLowerCase())) {
|
|
75
|
+
return Result.ok(value);
|
|
76
|
+
} else {
|
|
77
|
+
return Result.err(
|
|
78
|
+
annotate(value, `URL scheme must be any of: ${schemes.join(', ')}`),
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
File without changes
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import { annotate } from '../annotate';
|
|
4
|
+
import { compose, predicate } from './composition';
|
|
5
|
+
import { err, errValue, isErr, ok, unwrap, value } from '../result';
|
|
6
|
+
import { poja } from './array';
|
|
7
|
+
import type { Decoder } from '../_types';
|
|
8
|
+
|
|
9
|
+
const ntuple = (n: number) =>
|
|
10
|
+
compose(
|
|
11
|
+
poja,
|
|
12
|
+
predicate((arr) => arr.length === n, `Must be a ${n}-tuple`),
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Builds a Decoder that returns Ok for 1-tuple of [T], given a Decoder for T.
|
|
17
|
+
* Err otherwise.
|
|
18
|
+
*/
|
|
19
|
+
export function tuple1<T>(decoder1: Decoder<T>): Decoder<[T]> {
|
|
20
|
+
return compose(ntuple(1), (blobs: $ReadOnlyArray<mixed>) => {
|
|
21
|
+
const [blob1] = blobs;
|
|
22
|
+
|
|
23
|
+
const result1 = decoder1(blob1);
|
|
24
|
+
try {
|
|
25
|
+
return ok([unwrap(result1)]);
|
|
26
|
+
} catch (e) {
|
|
27
|
+
// If a decoder error has happened while unwrapping all the
|
|
28
|
+
// results, try to construct a good error message
|
|
29
|
+
return err(annotate(errValue(result1)));
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Builds a Decoder that returns Ok for 2-tuples of [T1, T2], given Decoders
|
|
36
|
+
* for T1 and T2. Err otherwise.
|
|
37
|
+
*/
|
|
38
|
+
export function tuple2<T1, T2>(
|
|
39
|
+
decoder1: Decoder<T1>,
|
|
40
|
+
decoder2: Decoder<T2>,
|
|
41
|
+
): Decoder<[T1, T2]> {
|
|
42
|
+
return compose(ntuple(2), (blobs: $ReadOnlyArray<mixed>) => {
|
|
43
|
+
const [blob1, blob2] = blobs;
|
|
44
|
+
|
|
45
|
+
const result1 = decoder1(blob1);
|
|
46
|
+
const result2 = decoder2(blob2);
|
|
47
|
+
try {
|
|
48
|
+
return ok([unwrap(result1), unwrap(result2)]);
|
|
49
|
+
} catch (e) {
|
|
50
|
+
// If a decoder error has happened while unwrapping all the
|
|
51
|
+
// results, try to construct a good error message
|
|
52
|
+
return err(
|
|
53
|
+
annotate([
|
|
54
|
+
isErr(result1) ? errValue(result1) : value(result1),
|
|
55
|
+
isErr(result2) ? errValue(result2) : value(result2),
|
|
56
|
+
]),
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Builds a Decoder that returns Ok for 3-tuples of [T1, T2, T3], given
|
|
64
|
+
* Decoders for T1, T2, and T3. Err otherwise.
|
|
65
|
+
*/
|
|
66
|
+
export function tuple3<T1, T2, T3>(
|
|
67
|
+
decoder1: Decoder<T1>,
|
|
68
|
+
decoder2: Decoder<T2>,
|
|
69
|
+
decoder3: Decoder<T3>,
|
|
70
|
+
): Decoder<[T1, T2, T3]> {
|
|
71
|
+
return compose(ntuple(3), (blobs: $ReadOnlyArray<mixed>) => {
|
|
72
|
+
const [blob1, blob2, blob3] = blobs;
|
|
73
|
+
|
|
74
|
+
const result1 = decoder1(blob1);
|
|
75
|
+
const result2 = decoder2(blob2);
|
|
76
|
+
const result3 = decoder3(blob3);
|
|
77
|
+
try {
|
|
78
|
+
return ok([unwrap(result1), unwrap(result2), unwrap(result3)]);
|
|
79
|
+
} catch (e) {
|
|
80
|
+
// If a decoder error has happened while unwrapping all the
|
|
81
|
+
// results, try to construct a good error message
|
|
82
|
+
return err(
|
|
83
|
+
annotate([
|
|
84
|
+
isErr(result1) ? errValue(result1) : value(result1),
|
|
85
|
+
isErr(result2) ? errValue(result2) : value(result2),
|
|
86
|
+
isErr(result3) ? errValue(result3) : value(result3),
|
|
87
|
+
]),
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Builds a Decoder that returns Ok for 4-tuples of [T1, T2, T3, T4], given
|
|
95
|
+
* Decoders for T1, T2, T3, and T4. Err otherwise.
|
|
96
|
+
*/
|
|
97
|
+
export function tuple4<T1, T2, T3, T4>(
|
|
98
|
+
decoder1: Decoder<T1>,
|
|
99
|
+
decoder2: Decoder<T2>,
|
|
100
|
+
decoder3: Decoder<T3>,
|
|
101
|
+
decoder4: Decoder<T4>,
|
|
102
|
+
): Decoder<[T1, T2, T3, T4]> {
|
|
103
|
+
return compose(ntuple(4), (blobs: $ReadOnlyArray<mixed>) => {
|
|
104
|
+
const [blob1, blob2, blob3, blob4] = blobs;
|
|
105
|
+
|
|
106
|
+
const result1 = decoder1(blob1);
|
|
107
|
+
const result2 = decoder2(blob2);
|
|
108
|
+
const result3 = decoder3(blob3);
|
|
109
|
+
const result4 = decoder4(blob4);
|
|
110
|
+
try {
|
|
111
|
+
return ok([
|
|
112
|
+
unwrap(result1),
|
|
113
|
+
unwrap(result2),
|
|
114
|
+
unwrap(result3),
|
|
115
|
+
unwrap(result4),
|
|
116
|
+
]);
|
|
117
|
+
} catch (e) {
|
|
118
|
+
// If a decoder error has happened while unwrapping all the
|
|
119
|
+
// results, try to construct a good error message
|
|
120
|
+
return err(
|
|
121
|
+
annotate([
|
|
122
|
+
isErr(result1) ? errValue(result1) : value(result1),
|
|
123
|
+
isErr(result2) ? errValue(result2) : value(result2),
|
|
124
|
+
isErr(result3) ? errValue(result3) : value(result3),
|
|
125
|
+
isErr(result4) ? errValue(result4) : value(result4),
|
|
126
|
+
]),
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
|
|
134
|
+
* Decoders for T1, T2, T3, T4, and T5. Err otherwise.
|
|
135
|
+
*/
|
|
136
|
+
export function tuple5<T1, T2, T3, T4, T5>(
|
|
137
|
+
decoder1: Decoder<T1>,
|
|
138
|
+
decoder2: Decoder<T2>,
|
|
139
|
+
decoder3: Decoder<T3>,
|
|
140
|
+
decoder4: Decoder<T4>,
|
|
141
|
+
decoder5: Decoder<T5>,
|
|
142
|
+
): Decoder<[T1, T2, T3, T4, T5]> {
|
|
143
|
+
return compose(ntuple(5), (blobs: $ReadOnlyArray<mixed>) => {
|
|
144
|
+
const [blob1, blob2, blob3, blob4, blob5] = blobs;
|
|
145
|
+
|
|
146
|
+
const result1 = decoder1(blob1);
|
|
147
|
+
const result2 = decoder2(blob2);
|
|
148
|
+
const result3 = decoder3(blob3);
|
|
149
|
+
const result4 = decoder4(blob4);
|
|
150
|
+
const result5 = decoder5(blob5);
|
|
151
|
+
try {
|
|
152
|
+
return ok([
|
|
153
|
+
unwrap(result1),
|
|
154
|
+
unwrap(result2),
|
|
155
|
+
unwrap(result3),
|
|
156
|
+
unwrap(result4),
|
|
157
|
+
unwrap(result5),
|
|
158
|
+
]);
|
|
159
|
+
} catch (e) {
|
|
160
|
+
// If a decoder error has happened while unwrapping all the
|
|
161
|
+
// results, try to construct a good error message
|
|
162
|
+
return err(
|
|
163
|
+
annotate([
|
|
164
|
+
isErr(result1) ? errValue(result1) : value(result1),
|
|
165
|
+
isErr(result2) ? errValue(result2) : value(result2),
|
|
166
|
+
isErr(result3) ? errValue(result3) : value(result3),
|
|
167
|
+
isErr(result4) ? errValue(result4) : value(result4),
|
|
168
|
+
isErr(result5) ? errValue(result5) : value(result5),
|
|
169
|
+
]),
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Builds a Decoder that returns Ok for 5-tuples of [T1, T2, T3, T4, T5], given
|
|
177
|
+
* Decoders for T1, T2, T3, T4, T5, and T6. Err otherwise.
|
|
178
|
+
*/
|
|
179
|
+
export function tuple6<T1, T2, T3, T4, T5, T6>(
|
|
180
|
+
decoder1: Decoder<T1>,
|
|
181
|
+
decoder2: Decoder<T2>,
|
|
182
|
+
decoder3: Decoder<T3>,
|
|
183
|
+
decoder4: Decoder<T4>,
|
|
184
|
+
decoder5: Decoder<T5>,
|
|
185
|
+
decoder6: Decoder<T6>,
|
|
186
|
+
): Decoder<[T1, T2, T3, T4, T5, T6]> {
|
|
187
|
+
return compose(ntuple(6), (blobs: $ReadOnlyArray<mixed>) => {
|
|
188
|
+
const [blob1, blob2, blob3, blob4, blob5, blob6] = blobs;
|
|
189
|
+
|
|
190
|
+
const result1 = decoder1(blob1);
|
|
191
|
+
const result2 = decoder2(blob2);
|
|
192
|
+
const result3 = decoder3(blob3);
|
|
193
|
+
const result4 = decoder4(blob4);
|
|
194
|
+
const result5 = decoder5(blob5);
|
|
195
|
+
const result6 = decoder6(blob6);
|
|
196
|
+
try {
|
|
197
|
+
return ok([
|
|
198
|
+
unwrap(result1),
|
|
199
|
+
unwrap(result2),
|
|
200
|
+
unwrap(result3),
|
|
201
|
+
unwrap(result4),
|
|
202
|
+
unwrap(result5),
|
|
203
|
+
unwrap(result6),
|
|
204
|
+
]);
|
|
205
|
+
} catch (e) {
|
|
206
|
+
// If a decoder error has happened while unwrapping all the
|
|
207
|
+
// results, try to construct a good error message
|
|
208
|
+
return err(
|
|
209
|
+
annotate([
|
|
210
|
+
isErr(result1) ? errValue(result1) : value(result1),
|
|
211
|
+
isErr(result2) ? errValue(result2) : value(result2),
|
|
212
|
+
isErr(result3) ? errValue(result3) : value(result3),
|
|
213
|
+
isErr(result4) ? errValue(result4) : value(result4),
|
|
214
|
+
isErr(result5) ? errValue(result5) : value(result5),
|
|
215
|
+
isErr(result6) ? errValue(result6) : value(result6),
|
|
216
|
+
]),
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
}
|
|
File without changes
|
|
File without changes
|