decoders 2.1.0 → 2.2.0-test2
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/annotate-0PUmWHxH.d.ts +33 -0
- package/chunk-BPSZE2VX.js +13 -0
- package/chunk-RUMDX66L.js +179 -0
- package/dist/annotate-0PUmWHxH.d.mts +33 -0
- package/dist/annotate-0PUmWHxH.d.ts +33 -0
- package/dist/chunk-BPSZE2VX.js +13 -0
- package/dist/chunk-BPSZE2VX.js.map +1 -0
- package/dist/chunk-HBFFQIIN.mjs +13 -0
- package/dist/chunk-HBFFQIIN.mjs.map +1 -0
- package/dist/chunk-RUMDX66L.js +179 -0
- package/dist/chunk-RUMDX66L.js.map +1 -0
- package/dist/chunk-ZTKFAKRL.mjs +179 -0
- package/dist/chunk-ZTKFAKRL.mjs.map +1 -0
- package/dist/format.d.mts +9 -0
- package/dist/format.d.ts +9 -0
- package/dist/format.js +13 -0
- package/dist/format.js.map +1 -0
- package/dist/format.mjs +13 -0
- package/dist/format.mjs.map +1 -0
- package/dist/index.d.mts +454 -0
- package/dist/index.d.ts +454 -0
- package/dist/index.js +659 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +659 -0
- package/dist/index.mjs.map +1 -0
- package/dist/result.d.mts +26 -0
- package/dist/result.d.ts +26 -0
- package/dist/result.js +9 -0
- package/dist/result.js.map +1 -0
- package/dist/result.mjs +9 -0
- package/dist/result.mjs.map +1 -0
- package/format.d.ts +7 -4
- package/format.js +12 -116
- package/index.d.ts +454 -40
- package/index.js +659 -67
- package/package.json +163 -100
- package/result.d.ts +25 -15
- package/result.js +6 -18
- package/CHANGELOG.md +0 -670
- 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.js.flow +0 -128
- package/format.mjs +0 -110
- 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 -54
- 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.js.flow +0 -26
- package/result.mjs +0 -15
package/lib/dates.js.flow
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { asDate } from '../_utils';
|
|
4
|
-
import { define } from '../Decoder';
|
|
5
|
-
import { regex } from './strings';
|
|
6
|
-
import type { Decoder } from '../Decoder';
|
|
7
|
-
|
|
8
|
-
// Only matches the shape. This "over-matches" some values that still aren't
|
|
9
|
-
// valid dates (like 9999-99-99), but those will be caught by JS Date's
|
|
10
|
-
// internal validations
|
|
11
|
-
const iso8601_re =
|
|
12
|
-
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Accepts and returns `Date` instances.
|
|
16
|
-
*/
|
|
17
|
-
export const date: Decoder<Date> = define((blob, ok, err) => {
|
|
18
|
-
const date = asDate(blob);
|
|
19
|
-
return date !== null ? ok(date) : err('Must be a Date');
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Accepts [ISO8601](https://en.wikipedia.org/wiki/ISO_8601)-formatted strings,
|
|
24
|
-
* returns them as `Date` instances.
|
|
25
|
-
*
|
|
26
|
-
* This is very useful for working with dates in APIs: serialize them as
|
|
27
|
-
* `.toISOString()` when sending, decode them with `iso8601` when receiving.
|
|
28
|
-
*/
|
|
29
|
-
export const iso8601: Decoder<Date> =
|
|
30
|
-
// Input itself needs to match the ISO8601 regex...
|
|
31
|
-
regex(iso8601_re, 'Must be ISO8601 format').transform(
|
|
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
|
-
);
|
package/lib/dates.mjs
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import { asDate } from '../_utils.mjs'
|
|
2
|
-
import { define } from '../Decoder.mjs'
|
|
3
|
-
import { regex } from './strings.mjs'
|
|
4
|
-
var iso8601_re = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:[.]\d+)?(?:Z|[+-]\d{2}:?\d{2})$/
|
|
5
|
-
|
|
6
|
-
export var date = define(function (blob, ok, err) {
|
|
7
|
-
var date = asDate(blob)
|
|
8
|
-
return date !== null ? ok(date) : err('Must be a Date')
|
|
9
|
-
})
|
|
10
|
-
|
|
11
|
-
export var iso8601 = regex(iso8601_re, 'Must be ISO8601 format').transform(function (value) {
|
|
12
|
-
var date = new Date(value)
|
|
13
|
-
if (isNaN(date.getTime())) {
|
|
14
|
-
throw new Error('Must be valid date/time value')
|
|
15
|
-
}
|
|
16
|
-
return date
|
|
17
|
-
})
|
package/lib/json.d.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import { Decoder } from '../Decoder';
|
|
2
|
-
|
|
3
|
-
export type JSONValue = null | string | number | boolean | JSONObject | JSONArray;
|
|
4
|
-
export interface JSONObject {
|
|
5
|
-
[key: string]: JSONValue | undefined;
|
|
6
|
-
}
|
|
7
|
-
export type JSONArray = JSONValue[];
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* Accepts any value that's a valid JSON value.
|
|
11
|
-
*
|
|
12
|
-
* In other words: any value returned by `JSON.parse()` should decode without
|
|
13
|
-
* failure.
|
|
14
|
-
*
|
|
15
|
-
* ```typescript
|
|
16
|
-
* type JSONValue =
|
|
17
|
-
* | null
|
|
18
|
-
* | string
|
|
19
|
-
* | number
|
|
20
|
-
* | boolean
|
|
21
|
-
* | { [string]: JSONValue }
|
|
22
|
-
* | JSONValue[]
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
25
|
-
export const json: Decoder<JSONValue>;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Accepts arrays that contain only valid JSON values.
|
|
29
|
-
*/
|
|
30
|
-
export const jsonArray: Decoder<JSONArray>;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Accepts objects that contain only valid JSON values.
|
|
34
|
-
*/
|
|
35
|
-
export const jsonObject: Decoder<JSONObject>;
|
package/lib/json.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true
|
|
4
|
-
exports.jsonObject = exports.jsonArray = exports.json = void 0
|
|
5
|
-
var _arrays = require('./arrays')
|
|
6
|
-
var _booleans = require('./booleans')
|
|
7
|
-
var _objects = require('./objects')
|
|
8
|
-
var _unions = require('./unions')
|
|
9
|
-
var _utilities = require('./utilities')
|
|
10
|
-
var _basics = require('./basics')
|
|
11
|
-
var _numbers = require('./numbers')
|
|
12
|
-
var _strings = require('./strings')
|
|
13
|
-
|
|
14
|
-
var jsonObject = (0, _utilities.lazy)(function () {
|
|
15
|
-
return (0, _objects.dict)(json)
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
exports.jsonObject = jsonObject
|
|
19
|
-
var jsonArray = (0, _utilities.lazy)(function () {
|
|
20
|
-
return (0, _arrays.array)(json)
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
exports.jsonArray = jsonArray
|
|
24
|
-
var json = (0, _unions.either)(_basics.null_, _strings.string, _numbers.number, _booleans['boolean'], jsonObject, jsonArray).describe('Must be valid JSON value')
|
|
25
|
-
exports.json = json
|
package/lib/json.js.flow
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { array } from './arrays';
|
|
4
|
-
import { boolean } from './booleans';
|
|
5
|
-
import { dict } from './objects';
|
|
6
|
-
import { either } from './unions';
|
|
7
|
-
import { lazy } from './utilities';
|
|
8
|
-
import { null_ } from './basics';
|
|
9
|
-
import { number } from './numbers';
|
|
10
|
-
import { string } from './strings';
|
|
11
|
-
import type { Decoder } from '../Decoder';
|
|
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
|
-
/**
|
|
18
|
-
* Accepts objects that contain only valid JSON values.
|
|
19
|
-
*/
|
|
20
|
-
export const jsonObject: Decoder<JSONObject> = lazy(() => dict(json));
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Accepts arrays that contain only valid JSON values.
|
|
24
|
-
*/
|
|
25
|
-
export const jsonArray: Decoder<JSONArray> = lazy(() => array(json));
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Accepts any value that's a valid JSON value.
|
|
29
|
-
*
|
|
30
|
-
* In other words: any value returned by `JSON.parse()` should decode without
|
|
31
|
-
* failure.
|
|
32
|
-
*
|
|
33
|
-
* ```typescript
|
|
34
|
-
* type JSONValue =
|
|
35
|
-
* | null
|
|
36
|
-
* | string
|
|
37
|
-
* | number
|
|
38
|
-
* | boolean
|
|
39
|
-
* | { [string]: JSONValue }
|
|
40
|
-
* | JSONValue[]
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
|
-
export const json: Decoder<JSONValue> = either(
|
|
44
|
-
null_,
|
|
45
|
-
string,
|
|
46
|
-
number,
|
|
47
|
-
boolean,
|
|
48
|
-
jsonObject,
|
|
49
|
-
jsonArray,
|
|
50
|
-
).describe('Must be valid JSON value');
|
package/lib/json.mjs
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { array } from './arrays.mjs'
|
|
2
|
-
import { boolean as _boolean } from './booleans.mjs'
|
|
3
|
-
import { dict } from './objects.mjs'
|
|
4
|
-
import { either } from './unions.mjs'
|
|
5
|
-
import { lazy } from './utilities.mjs'
|
|
6
|
-
import { null_ } from './basics.mjs'
|
|
7
|
-
import { number } from './numbers.mjs'
|
|
8
|
-
import { string } from './strings.mjs'
|
|
9
|
-
|
|
10
|
-
export var jsonObject = lazy(function () {
|
|
11
|
-
return dict(json)
|
|
12
|
-
})
|
|
13
|
-
|
|
14
|
-
export var jsonArray = lazy(function () {
|
|
15
|
-
return array(json)
|
|
16
|
-
})
|
|
17
|
-
|
|
18
|
-
export var json = either(null_, string, number, _boolean, jsonObject, jsonArray).describe('Must be valid JSON value')
|
package/lib/numbers.d.ts
DELETED
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import { Decoder } from '../Decoder';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Accepts any valid ``number`` value.
|
|
5
|
-
*
|
|
6
|
-
* This also accepts special values like `NaN` and `Infinity`. Unless you
|
|
7
|
-
* want to deliberately accept those, you'll likely want to use the
|
|
8
|
-
* `number` decoder instead.
|
|
9
|
-
*/
|
|
10
|
-
export const anyNumber: Decoder<number>;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Accepts finite numbers (can be integer or float values). Values `NaN`,
|
|
14
|
-
* or positive and negative `Infinity` will get rejected.
|
|
15
|
-
*/
|
|
16
|
-
export const number: Decoder<number>;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Accepts only finite whole numbers.
|
|
20
|
-
*/
|
|
21
|
-
export const integer: Decoder<number>;
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Accepts only non-negative (zero or positive) finite numbers.
|
|
25
|
-
*/
|
|
26
|
-
export const positiveNumber: Decoder<number>;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Accepts only non-negative (zero or positive) finite whole numbers.
|
|
30
|
-
*/
|
|
31
|
-
export const positiveInteger: Decoder<number>;
|
package/lib/numbers.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
exports.__esModule = true
|
|
4
|
-
exports.positiveNumber = exports.positiveInteger = exports.number = exports.integer = exports.anyNumber = void 0
|
|
5
|
-
var _Decoder = require('../Decoder')
|
|
6
|
-
|
|
7
|
-
var anyNumber = (0, _Decoder.define)(function (blob, ok, err) {
|
|
8
|
-
return typeof blob === 'number' ? ok(blob) : err('Must be number')
|
|
9
|
-
})
|
|
10
|
-
|
|
11
|
-
exports.anyNumber = anyNumber
|
|
12
|
-
var number = anyNumber.refine(function (n) {
|
|
13
|
-
return Number.isFinite(n)
|
|
14
|
-
}, 'Number must be finite')
|
|
15
|
-
|
|
16
|
-
exports.number = number
|
|
17
|
-
var integer = number.refine(function (n) {
|
|
18
|
-
return Number.isInteger(n)
|
|
19
|
-
}, 'Number must be an integer')
|
|
20
|
-
|
|
21
|
-
exports.integer = integer
|
|
22
|
-
var positiveNumber = number
|
|
23
|
-
.refine(function (n) {
|
|
24
|
-
return n >= 0
|
|
25
|
-
}, 'Number must be positive')
|
|
26
|
-
.transform(Math.abs)
|
|
27
|
-
|
|
28
|
-
exports.positiveNumber = positiveNumber
|
|
29
|
-
var positiveInteger = integer
|
|
30
|
-
.refine(function (n) {
|
|
31
|
-
return n >= 0
|
|
32
|
-
}, 'Number must be positive')
|
|
33
|
-
.transform(Math.abs)
|
|
34
|
-
exports.positiveInteger = positiveInteger
|
package/lib/numbers.js.flow
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
// @flow strict
|
|
2
|
-
|
|
3
|
-
import { define } from '../Decoder';
|
|
4
|
-
import type { Decoder } from '../Decoder';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Accepts any valid ``number`` value.
|
|
8
|
-
*
|
|
9
|
-
* This also accepts special values like `NaN` and `Infinity`. Unless you
|
|
10
|
-
* want to deliberately accept those, you'll likely want to use the
|
|
11
|
-
* `number` decoder instead.
|
|
12
|
-
*/
|
|
13
|
-
export const anyNumber: Decoder<number> = define((blob, ok, err) =>
|
|
14
|
-
typeof blob === 'number' ? ok(blob) : err('Must be number'),
|
|
15
|
-
);
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Accepts finite numbers (can be integer or float values). Values `NaN`,
|
|
19
|
-
* or positive and negative `Infinity` will get rejected.
|
|
20
|
-
*/
|
|
21
|
-
export const number: Decoder<number> = anyNumber.refine(
|
|
22
|
-
(n) => Number.isFinite(n),
|
|
23
|
-
'Number must be finite',
|
|
24
|
-
);
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Accepts only finite whole numbers.
|
|
28
|
-
*/
|
|
29
|
-
export const integer: Decoder<number> = number.refine(
|
|
30
|
-
(n) => Number.isInteger(n),
|
|
31
|
-
'Number must be an integer',
|
|
32
|
-
);
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Accepts only non-negative (zero or positive) finite numbers.
|
|
36
|
-
*/
|
|
37
|
-
export const positiveNumber: Decoder<number> = number
|
|
38
|
-
.refine((n) => n >= 0, 'Number must be positive')
|
|
39
|
-
.transform(Math.abs); // Just here to handle the -0 case
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Accepts only non-negative (zero or positive) finite whole numbers.
|
|
43
|
-
*/
|
|
44
|
-
export const positiveInteger: Decoder<number> = integer
|
|
45
|
-
.refine((n) => n >= 0, 'Number must be positive')
|
|
46
|
-
.transform(Math.abs); // Just here to handle the -0 case
|
package/lib/numbers.mjs
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { define } from '../Decoder.mjs'
|
|
2
|
-
|
|
3
|
-
export var anyNumber = define(function (blob, ok, err) {
|
|
4
|
-
return typeof blob === 'number' ? ok(blob) : err('Must be number')
|
|
5
|
-
})
|
|
6
|
-
|
|
7
|
-
export var number = anyNumber.refine(function (n) {
|
|
8
|
-
return Number.isFinite(n)
|
|
9
|
-
}, 'Number must be finite')
|
|
10
|
-
|
|
11
|
-
export var integer = number.refine(function (n) {
|
|
12
|
-
return Number.isInteger(n)
|
|
13
|
-
}, 'Number must be an integer')
|
|
14
|
-
|
|
15
|
-
export var positiveNumber = number
|
|
16
|
-
.refine(function (n) {
|
|
17
|
-
return n >= 0
|
|
18
|
-
}, 'Number must be positive')
|
|
19
|
-
.transform(Math.abs)
|
|
20
|
-
|
|
21
|
-
export var positiveInteger = integer
|
|
22
|
-
.refine(function (n) {
|
|
23
|
-
return n >= 0
|
|
24
|
-
}, 'Number must be positive')
|
|
25
|
-
.transform(Math.abs)
|
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
|
-
}
|