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,103 @@
|
|
|
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, DecodeResult } from '../_types';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Like a "Plain Old JavaScript Object", but for arrays: "Plain Old JavaScript
|
|
10
|
+
* Array" ^_^
|
|
11
|
+
*/
|
|
12
|
+
export const poja: Decoder<Array<mixed>> = (blob: mixed) => {
|
|
13
|
+
if (!Array.isArray(blob)) {
|
|
14
|
+
return Result.err(annotate(blob, 'Must be an array'));
|
|
15
|
+
}
|
|
16
|
+
return Result.ok(
|
|
17
|
+
// NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray<mixed>
|
|
18
|
+
// instead of Array<mixed>. For rationale, see
|
|
19
|
+
// https://github.com/facebook/flow/issues/7684. In this case, we
|
|
20
|
+
// don't want to output read-only types because it's up to the user of
|
|
21
|
+
// decoders to determine what they want to do with the decoded output.
|
|
22
|
+
// If they want to write items into the array, that's fine!
|
|
23
|
+
// The fastest way to turn a read-only array into a normal array in
|
|
24
|
+
// Javascript is to use .slice() on it, see this benchmark:
|
|
25
|
+
// http://jsben.ch/lO6C5
|
|
26
|
+
blob.slice(),
|
|
27
|
+
);
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Given an array of Result instances, loop over them all and return:
|
|
32
|
+
* - An [index, err] tuple, indicating the (index of the) first Err instance
|
|
33
|
+
* encountered; or
|
|
34
|
+
* - a new Ok with an array of all unwrapped Ok'ed values
|
|
35
|
+
*/
|
|
36
|
+
function all<T>(
|
|
37
|
+
items: $ReadOnlyArray<DecodeResult<T>>,
|
|
38
|
+
blobs: $ReadOnlyArray<mixed>,
|
|
39
|
+
): DecodeResult<Array<T>> {
|
|
40
|
+
const results: Array<T> = [];
|
|
41
|
+
for (let index = 0; index < items.length; ++index) {
|
|
42
|
+
const result = items[index];
|
|
43
|
+
if (result.type === 'ok') {
|
|
44
|
+
results.push(result.value);
|
|
45
|
+
} else {
|
|
46
|
+
const ann = result.error;
|
|
47
|
+
|
|
48
|
+
// Rewrite the annotation to include the index information, and inject it into the original blob
|
|
49
|
+
const clone = [...blobs];
|
|
50
|
+
clone.splice(
|
|
51
|
+
index,
|
|
52
|
+
1,
|
|
53
|
+
annotate(
|
|
54
|
+
ann,
|
|
55
|
+
ann.text ? `${ann.text} (at index ${index})` : `index ${index}`,
|
|
56
|
+
),
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
// const errValue = [];
|
|
60
|
+
// if (index > 0) {
|
|
61
|
+
// errValue.push('...'); // TODO: make special mark, not string!
|
|
62
|
+
// }
|
|
63
|
+
// errValue.push(
|
|
64
|
+
// );
|
|
65
|
+
// if (index < iterable.length - 1) {
|
|
66
|
+
// errValue.push('...'); // TODO: make special mark, not string!
|
|
67
|
+
// }
|
|
68
|
+
return Result.err(annotate(clone));
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return Result.ok(results);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Given a T, builds a decoder that assumes an array input and returns an
|
|
76
|
+
* Array<T>.
|
|
77
|
+
*/
|
|
78
|
+
function members<T>(decoder: Decoder<T>): Decoder<Array<T>, Array<mixed>> {
|
|
79
|
+
return (blobs: $ReadOnlyArray<mixed>) => {
|
|
80
|
+
const results = blobs.map(decoder);
|
|
81
|
+
const result = all(results, blobs);
|
|
82
|
+
return result;
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Builds a Decoder that returns Ok for values of `Array<T>`, given a Decoder
|
|
88
|
+
* for `T`. Err otherwise.
|
|
89
|
+
*/
|
|
90
|
+
export function array<T>(decoder: Decoder<T>): Decoder<Array<T>> {
|
|
91
|
+
return compose(poja, members(decoder));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Builds a Decoder that returns Ok for values of `Array<T>`, but will reject
|
|
96
|
+
* empty arrays.
|
|
97
|
+
*/
|
|
98
|
+
export function nonEmptyArray<T>(decoder: Decoder<T>): Decoder<Array<T>> {
|
|
99
|
+
return compose(
|
|
100
|
+
array(decoder),
|
|
101
|
+
predicate((arr) => arr.length > 0, 'Must be non-empty array'),
|
|
102
|
+
);
|
|
103
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
5
|
+
import { map } from './composition';
|
|
6
|
+
import { number } from './number';
|
|
7
|
+
import type { Decoder } from '../_types';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Decoder that only returns Ok for boolean inputs. Err otherwise.
|
|
11
|
+
*/
|
|
12
|
+
export const boolean: Decoder<boolean> = (blob: mixed) => {
|
|
13
|
+
return typeof blob === 'boolean'
|
|
14
|
+
? Result.ok(blob)
|
|
15
|
+
: Result.err(annotate(blob, 'Must be boolean'));
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Decoder that returns true for all truthy values, and false otherwise. Never fails.
|
|
20
|
+
*/
|
|
21
|
+
export const truthy: Decoder<boolean> = (blob: mixed) => {
|
|
22
|
+
return Result.ok(!!blob);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Decoder that only returns Ok for numeric input values representing booleans.
|
|
27
|
+
* Returns their boolean representation. Err otherwise.
|
|
28
|
+
*/
|
|
29
|
+
export const numericBoolean: Decoder<boolean> = map(number, (n) => !!n);
|
|
File without changes
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
5
|
+
import type { Decoder } from '../_types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Given a decoder T and a mapping function from T's to V's, returns a decoder
|
|
9
|
+
* for V's. This is useful to change the original input data.
|
|
10
|
+
*/
|
|
11
|
+
export function map<T, V>(decoder: Decoder<T>, mapper: (T) => V): Decoder<V> {
|
|
12
|
+
return compose(decoder, (x) => {
|
|
13
|
+
try {
|
|
14
|
+
return Result.ok(mapper(x));
|
|
15
|
+
} catch (e) {
|
|
16
|
+
return Result.err(annotate(x, e instanceof Error ? e.message : String(e)));
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Compose two decoders by passing the result of the first into the second.
|
|
23
|
+
* The second decoder may assume as its input type the output type of the first
|
|
24
|
+
* decoder (so it's not necessary to accept the typical "mixed"). This is
|
|
25
|
+
* useful for "narrowing down" the checks. For example, if you want to write
|
|
26
|
+
* a decoder for positive numbers, you can compose it from an existing decoder
|
|
27
|
+
* for any number, and a decoder that, assuming a number, checks if it's
|
|
28
|
+
* positive. Very often combined with the predicate() helper as the second
|
|
29
|
+
* argument.
|
|
30
|
+
*/
|
|
31
|
+
export function compose<T, V>(decoder: Decoder<T>, next: Decoder<V, T>): Decoder<V> {
|
|
32
|
+
return (blob: mixed) => Result.andThen(decoder(blob), next);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Factory function returning a Decoder<T>, given a predicate function that
|
|
37
|
+
* accepts/rejects the input of type T.
|
|
38
|
+
*/
|
|
39
|
+
export function predicate<T>(predicate: (T) => boolean, msg: string): Decoder<T, T> {
|
|
40
|
+
return (value: T) => {
|
|
41
|
+
return predicate(value) ? Result.ok(value) : Result.err(annotate(value, msg));
|
|
42
|
+
};
|
|
43
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
5
|
+
import type { Decoder, Scalar } from '../_types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Decoder that only returns Ok for `null` inputs. Err otherwise.
|
|
9
|
+
*/
|
|
10
|
+
export const null_: Decoder<null> = (blob: mixed) =>
|
|
11
|
+
blob === null ? Result.ok(blob) : Result.err(annotate(blob, 'Must be null'));
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Decoder that only returns Ok for `undefined` inputs. Err otherwise.
|
|
15
|
+
*/
|
|
16
|
+
export const undefined_: Decoder<void> = (blob: mixed) =>
|
|
17
|
+
blob === undefined
|
|
18
|
+
? Result.ok(blob)
|
|
19
|
+
: Result.err(annotate(blob, 'Must be undefined'));
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Decoder that only returns Ok for the given value constant. Err otherwise.
|
|
23
|
+
*/
|
|
24
|
+
export function constant<T: Scalar>(value: T): Decoder<T> {
|
|
25
|
+
return (blob: mixed) =>
|
|
26
|
+
blob === value
|
|
27
|
+
? Result.ok(value)
|
|
28
|
+
: Result.err(annotate(blob, `Must be constant ${String(value)}`));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Decoder that always returns Ok for the given hardcoded value, no matter what the input.
|
|
33
|
+
*/
|
|
34
|
+
export function hardcoded<T>(value: T): Decoder<T> {
|
|
35
|
+
return () => Result.ok(value);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Decoder that always returns Ok for the given hardcoded value, no matter what the input.
|
|
40
|
+
*/
|
|
41
|
+
export const unknown: Decoder<mixed> = (blob: mixed) => Result.ok(blob);
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Alias of unknown.
|
|
45
|
+
*/
|
|
46
|
+
export const mixed: Decoder<mixed> = unknown;
|
|
File without changes
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
5
|
+
import { isDate } from '../_utils';
|
|
6
|
+
import { map } from './composition';
|
|
7
|
+
import { regex } from './string';
|
|
8
|
+
import type { Decoder } from '../_types';
|
|
9
|
+
|
|
10
|
+
// $FlowFixMe[unclear-type] (not really an issue) - deliberate casting
|
|
11
|
+
type cast = any;
|
|
12
|
+
|
|
13
|
+
// Only matches the shape. This "over-matches" some values that still aren't
|
|
14
|
+
// valid dates (like 9999-99-99), but those will be caught by JS Date's
|
|
15
|
+
// internal validations
|
|
16
|
+
const iso8601_re =
|
|
17
|
+
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
|
|
18
|
+
|
|
19
|
+
export const date: Decoder<Date> = (value: mixed) =>
|
|
20
|
+
isDate(value)
|
|
21
|
+
? Result.ok(((value: cast): Date))
|
|
22
|
+
: Result.err(annotate(value, 'Must be a Date'));
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Decoder that only returns Ok for strings that are valid ISO8601 date
|
|
26
|
+
* strings. Err otherwise.
|
|
27
|
+
*/
|
|
28
|
+
export const iso8601: Decoder<Date> = map(
|
|
29
|
+
// Input itself needs to match the ISO8601 regex...
|
|
30
|
+
regex(iso8601_re, 'Must be ISO8601 format'),
|
|
31
|
+
|
|
32
|
+
// Make sure it is a _valid_ date
|
|
33
|
+
(value: string) => {
|
|
34
|
+
const date = new Date(value);
|
|
35
|
+
if (isNaN(date.getTime())) {
|
|
36
|
+
throw new Error('Must be valid date/time value');
|
|
37
|
+
}
|
|
38
|
+
return date;
|
|
39
|
+
},
|
|
40
|
+
);
|
|
File without changes
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
5
|
+
import type { Decoder } from '../_types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Wrap another decoder, and override the error message in case it fails. This
|
|
9
|
+
* is useful to "simplify" otherwise potentially complex error messages, or to
|
|
10
|
+
* use language in those error messages that can be relayed to end users (for
|
|
11
|
+
* example to show in form errors).
|
|
12
|
+
*/
|
|
13
|
+
export function describe<T>(decoder: Decoder<T>, message: string): Decoder<T> {
|
|
14
|
+
return (blob: mixed) => {
|
|
15
|
+
return Result.mapError(decoder(blob), (err) => annotate(err, message));
|
|
16
|
+
};
|
|
17
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { object } from './object';
|
|
5
|
+
import { oneOf } from './either';
|
|
6
|
+
import type { Decoder, DecoderType } from '../_types';
|
|
7
|
+
|
|
8
|
+
// $FlowFixMe[unclear-type] (not really an issue) - deliberate use of `any` - not sure how we should get rid of this
|
|
9
|
+
type anything = any;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Dispatches to one of several given decoders, based on the value found at
|
|
13
|
+
* runtime in the given field. For example, suppose you have these decoders:
|
|
14
|
+
*
|
|
15
|
+
* const rectangle = object({
|
|
16
|
+
* type: constant('rect'),
|
|
17
|
+
* x: number,
|
|
18
|
+
* y: number,
|
|
19
|
+
* width: number,
|
|
20
|
+
* height: number,
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* const circle = object({
|
|
24
|
+
* type: constant('circle'),
|
|
25
|
+
* cx: number,
|
|
26
|
+
* cy: number,
|
|
27
|
+
* r: number,
|
|
28
|
+
* });
|
|
29
|
+
*
|
|
30
|
+
* Then these two decoders are equivalent:
|
|
31
|
+
*
|
|
32
|
+
* const shape = either(rectangle, circle)
|
|
33
|
+
* const shape = dispatch('type', { rectangle, circle })
|
|
34
|
+
*
|
|
35
|
+
* Will be of type Decoder<Rectangle | Circle>.
|
|
36
|
+
*
|
|
37
|
+
* But the dispatch version will typically be more runtime-efficient. The
|
|
38
|
+
* reason is that it will first do minimal work to "look ahead" into the `type`
|
|
39
|
+
* field here, and based on that value, pick the decoder to invoke.
|
|
40
|
+
*
|
|
41
|
+
* The `either` version will simply try to invoke each decoder, until it finds
|
|
42
|
+
* one that matches.
|
|
43
|
+
*
|
|
44
|
+
* Also, the error messages will be less ambiguous using `dispatch()`.
|
|
45
|
+
*/
|
|
46
|
+
export function dispatch<O: { +[field: string]: Decoder<anything>, ... }>(
|
|
47
|
+
field: string,
|
|
48
|
+
mapping: O,
|
|
49
|
+
): Decoder<$Values<$ObjMap<O, DecoderType>>> {
|
|
50
|
+
const base = object({ [field]: oneOf(Object.keys(mapping)) });
|
|
51
|
+
return (blob: mixed) => {
|
|
52
|
+
return Result.andThen(base(blob), (baseObj) => {
|
|
53
|
+
const decoderName = baseObj[field];
|
|
54
|
+
const decoder = mapping[decoderName];
|
|
55
|
+
return decoder(blob);
|
|
56
|
+
});
|
|
57
|
+
};
|
|
58
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
5
|
+
import { indent, summarize } from '../_utils';
|
|
6
|
+
import type { Decoder, Scalar } from '../_types';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Indents and adds a dash in front of this (potentially multiline) string.
|
|
10
|
+
*/
|
|
11
|
+
function itemize(s: string): string {
|
|
12
|
+
return '-' + indent(s).substring(1);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Nests another error as an item under a new-to-be-created "Either error". If
|
|
17
|
+
* the given subitem already is an "Either error" of itself, don't indent, but
|
|
18
|
+
* just "inject" its items at the same error level, for nicely flattened either
|
|
19
|
+
* expressions.
|
|
20
|
+
*
|
|
21
|
+
* Avoids:
|
|
22
|
+
*
|
|
23
|
+
* Either:
|
|
24
|
+
* - Either:
|
|
25
|
+
* - Must be P
|
|
26
|
+
* - Either:
|
|
27
|
+
* - Must be Q
|
|
28
|
+
* - Must be R
|
|
29
|
+
* - Must be S
|
|
30
|
+
*
|
|
31
|
+
* And "flattens" these to:
|
|
32
|
+
*
|
|
33
|
+
* Either:
|
|
34
|
+
* - Must be P
|
|
35
|
+
* - Must be Q
|
|
36
|
+
* - Must be R
|
|
37
|
+
* - Must be S
|
|
38
|
+
*
|
|
39
|
+
*/
|
|
40
|
+
function nest(errText: string): string {
|
|
41
|
+
const EITHER_PREFIX = 'Either:\n';
|
|
42
|
+
return errText.startsWith(EITHER_PREFIX)
|
|
43
|
+
? errText.substr(EITHER_PREFIX.length)
|
|
44
|
+
: itemize(errText);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function either<T1, T2>(d1: Decoder<T1>, d2: Decoder<T2>): Decoder<T1 | T2> {
|
|
48
|
+
return (blob: mixed) =>
|
|
49
|
+
Result.orElse(d1(blob), (err1) =>
|
|
50
|
+
Result.orElse(d2(blob), (err2) => {
|
|
51
|
+
const serr1 = summarize(err1).join('\n');
|
|
52
|
+
const serr2 = summarize(err2).join('\n');
|
|
53
|
+
const text = ['Either:', nest(serr1), nest(serr2)].join('\n');
|
|
54
|
+
return Result.err(annotate(blob, text));
|
|
55
|
+
}),
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function either3<T1, T2, T3>(
|
|
60
|
+
d1: Decoder<T1>,
|
|
61
|
+
d2: Decoder<T2>,
|
|
62
|
+
d3: Decoder<T3>,
|
|
63
|
+
): Decoder<T1 | T2 | T3> {
|
|
64
|
+
return either(d1, either(d2, d3));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function either4<T1, T2, T3, T4>(
|
|
68
|
+
d1: Decoder<T1>,
|
|
69
|
+
d2: Decoder<T2>,
|
|
70
|
+
d3: Decoder<T3>,
|
|
71
|
+
d4: Decoder<T4>,
|
|
72
|
+
): Decoder<T1 | T2 | T3 | T4> {
|
|
73
|
+
return either(d1, either3(d2, d3, d4));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export function either5<T1, T2, T3, T4, T5>(
|
|
77
|
+
d1: Decoder<T1>,
|
|
78
|
+
d2: Decoder<T2>,
|
|
79
|
+
d3: Decoder<T3>,
|
|
80
|
+
d4: Decoder<T4>,
|
|
81
|
+
d5: Decoder<T5>,
|
|
82
|
+
): Decoder<T1 | T2 | T3 | T4 | T5> {
|
|
83
|
+
return either(d1, either4(d2, d3, d4, d5));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function either6<T1, T2, T3, T4, T5, T6>(
|
|
87
|
+
d1: Decoder<T1>,
|
|
88
|
+
d2: Decoder<T2>,
|
|
89
|
+
d3: Decoder<T3>,
|
|
90
|
+
d4: Decoder<T4>,
|
|
91
|
+
d5: Decoder<T5>,
|
|
92
|
+
d6: Decoder<T6>,
|
|
93
|
+
): Decoder<T1 | T2 | T3 | T4 | T5 | T6> {
|
|
94
|
+
return either(d1, either5(d2, d3, d4, d5, d6));
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function either7<T1, T2, T3, T4, T5, T6, T7>(
|
|
98
|
+
d1: Decoder<T1>,
|
|
99
|
+
d2: Decoder<T2>,
|
|
100
|
+
d3: Decoder<T3>,
|
|
101
|
+
d4: Decoder<T4>,
|
|
102
|
+
d5: Decoder<T5>,
|
|
103
|
+
d6: Decoder<T6>,
|
|
104
|
+
d7: Decoder<T7>,
|
|
105
|
+
): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7> {
|
|
106
|
+
return either(d1, either6(d2, d3, d4, d5, d6, d7));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function either8<T1, T2, T3, T4, T5, T6, T7, T8>(
|
|
110
|
+
d1: Decoder<T1>,
|
|
111
|
+
d2: Decoder<T2>,
|
|
112
|
+
d3: Decoder<T3>,
|
|
113
|
+
d4: Decoder<T4>,
|
|
114
|
+
d5: Decoder<T5>,
|
|
115
|
+
d6: Decoder<T6>,
|
|
116
|
+
d7: Decoder<T7>,
|
|
117
|
+
d8: Decoder<T8>,
|
|
118
|
+
): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8> {
|
|
119
|
+
return either(d1, either7(d2, d3, d4, d5, d6, d7, d8));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function either9<T1, T2, T3, T4, T5, T6, T7, T8, T9>(
|
|
123
|
+
d1: Decoder<T1>,
|
|
124
|
+
d2: Decoder<T2>,
|
|
125
|
+
d3: Decoder<T3>,
|
|
126
|
+
d4: Decoder<T4>,
|
|
127
|
+
d5: Decoder<T5>,
|
|
128
|
+
d6: Decoder<T6>,
|
|
129
|
+
d7: Decoder<T7>,
|
|
130
|
+
d8: Decoder<T8>,
|
|
131
|
+
d9: Decoder<T9>,
|
|
132
|
+
): Decoder<T1 | T2 | T3 | T4 | T5 | T6 | T7 | T8 | T9> {
|
|
133
|
+
return either(d1, either8(d2, d3, d4, d5, d6, d7, d8, d9));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function oneOf<T: Scalar>(constants: $ReadOnlyArray<T>): Decoder<T> {
|
|
137
|
+
return (blob: mixed) => {
|
|
138
|
+
const winner = constants.find((c) => c === blob);
|
|
139
|
+
if (winner !== undefined) {
|
|
140
|
+
return Result.ok(winner);
|
|
141
|
+
}
|
|
142
|
+
return Result.err(
|
|
143
|
+
annotate(
|
|
144
|
+
blob,
|
|
145
|
+
`Must be one of ${constants
|
|
146
|
+
.map((value) => JSON.stringify(value))
|
|
147
|
+
.join(', ')}`,
|
|
148
|
+
),
|
|
149
|
+
);
|
|
150
|
+
};
|
|
151
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
5
|
+
import type { Decoder } from '../_types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Decoder that always fails with the given error message, no matter what the input.
|
|
9
|
+
*/
|
|
10
|
+
export function fail(msg: string): Decoder<empty> {
|
|
11
|
+
return (blob: mixed) => Result.err(annotate(blob, msg));
|
|
12
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotate } from '../annotate';
|
|
5
|
+
import type { Decoder } from '../_types';
|
|
6
|
+
|
|
7
|
+
export function instanceOf<T>(klass: Class<T>): Decoder<T> {
|
|
8
|
+
return (blob: mixed) =>
|
|
9
|
+
blob instanceof klass
|
|
10
|
+
? Result.ok(blob)
|
|
11
|
+
: Result.err(
|
|
12
|
+
annotate(
|
|
13
|
+
blob,
|
|
14
|
+
`Must be ${
|
|
15
|
+
// $FlowFixMe[incompatible-use] - klass.name is fine?
|
|
16
|
+
klass.name
|
|
17
|
+
} instance`,
|
|
18
|
+
),
|
|
19
|
+
);
|
|
20
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import { array } from './array';
|
|
4
|
+
import { boolean } from './boolean';
|
|
5
|
+
import { dict } from './mapping';
|
|
6
|
+
import { either6 } from './either';
|
|
7
|
+
import { lazy } from './lazy';
|
|
8
|
+
import { null_ } from './constants';
|
|
9
|
+
import { number } from './number';
|
|
10
|
+
import { string } from './string';
|
|
11
|
+
import type { Decoder } from '../_types';
|
|
12
|
+
|
|
13
|
+
export type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
|
|
14
|
+
export type JSONObject = { [string]: JSONValue };
|
|
15
|
+
export type JSONArray = Array<JSONValue>;
|
|
16
|
+
|
|
17
|
+
export const jsonObject: Decoder<JSONObject> = lazy(() => dict(json));
|
|
18
|
+
|
|
19
|
+
export const jsonArray: Decoder<JSONArray> = lazy(() => array(json));
|
|
20
|
+
|
|
21
|
+
export const json: Decoder<JSONValue> = either6(
|
|
22
|
+
null_,
|
|
23
|
+
string,
|
|
24
|
+
number,
|
|
25
|
+
boolean,
|
|
26
|
+
jsonObject,
|
|
27
|
+
jsonArray,
|
|
28
|
+
);
|
|
File without changes
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import type { Decoder } from '../_types';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Given an function returning a Decoder, will use that decoder to decode the
|
|
7
|
+
* value. This is typically used to build decoders for recursive or
|
|
8
|
+
* self-referential types.
|
|
9
|
+
*/
|
|
10
|
+
export function lazy<T>(decoderFn: () => Decoder<T>): Decoder<T> {
|
|
11
|
+
return (blob: mixed) => {
|
|
12
|
+
const decoder = decoderFn();
|
|
13
|
+
return decoder(blob);
|
|
14
|
+
};
|
|
15
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// @flow strict
|
|
2
|
+
|
|
3
|
+
import * as Result from '../result';
|
|
4
|
+
import { annotateObject } from '../annotate';
|
|
5
|
+
import { compose, map } from './composition';
|
|
6
|
+
import { merge } from '../annotate';
|
|
7
|
+
import { pojo } from './object';
|
|
8
|
+
import type { Annotation } from '../annotate';
|
|
9
|
+
import type { Decoder } from '../_types';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Given an object, will decode a Map of string keys to whatever values.
|
|
13
|
+
*
|
|
14
|
+
* For example, given a decoder for a Person, we can verify a Person lookup
|
|
15
|
+
* table structure (of type Map<string, Person>) like so:
|
|
16
|
+
*
|
|
17
|
+
* mapping(person)
|
|
18
|
+
*
|
|
19
|
+
*/
|
|
20
|
+
export function mapping<T>(decoder: Decoder<T>): Decoder<Map<string, T>> {
|
|
21
|
+
return compose(pojo, (blob: { +[key: string]: mixed }) => {
|
|
22
|
+
let tuples: Array<[string, T]> = [];
|
|
23
|
+
let errors: { [key: string]: Annotation } | null = null;
|
|
24
|
+
|
|
25
|
+
Object.keys(blob).forEach((key: string) => {
|
|
26
|
+
const value = blob[key];
|
|
27
|
+
const result = decoder(value);
|
|
28
|
+
if (result.type === 'ok') {
|
|
29
|
+
if (errors === null) {
|
|
30
|
+
tuples.push([key, result.value]);
|
|
31
|
+
}
|
|
32
|
+
} else {
|
|
33
|
+
tuples.length = 0; // Clear the tuples array
|
|
34
|
+
if (errors === null) {
|
|
35
|
+
errors = {};
|
|
36
|
+
}
|
|
37
|
+
errors[key] = result.error;
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
if (errors !== null) {
|
|
42
|
+
return Result.err(merge(annotateObject(blob), errors));
|
|
43
|
+
} else {
|
|
44
|
+
return Result.ok(new Map(tuples));
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Like mapping(), but returns an object rather than a Map instance.
|
|
51
|
+
*/
|
|
52
|
+
export function dict<T>(decoder: Decoder<T>): Decoder<{ [string]: T }> {
|
|
53
|
+
return map(mapping(decoder), (m) => Object.fromEntries(m));
|
|
54
|
+
}
|
|
File without changes
|