decoders 2.4.0 → 2.4.2
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/dist/index.cjs +13 -23
- package/dist/index.d.cts +5 -13
- package/dist/index.d.ts +5 -13
- package/dist/index.js +13 -23
- package/package.json +21 -19
package/dist/index.cjs
CHANGED
|
@@ -29,18 +29,12 @@ function makeObjectAnn(fields, text) {
|
|
|
29
29
|
function makeArrayAnn(items, text) {
|
|
30
30
|
return brand({ type: "array", items, text });
|
|
31
31
|
}
|
|
32
|
-
function
|
|
33
|
-
return brand({ type: "
|
|
34
|
-
}
|
|
35
|
-
function makeUnknownAnn(value, text) {
|
|
36
|
-
return brand({ type: "unknown", value, text });
|
|
32
|
+
function makeOpaqueAnn(value, text) {
|
|
33
|
+
return brand({ type: "opaque", value, text });
|
|
37
34
|
}
|
|
38
35
|
function makeScalarAnn(value, text) {
|
|
39
36
|
return brand({ type: "scalar", value, text });
|
|
40
37
|
}
|
|
41
|
-
function makeCircularRefAnn(text) {
|
|
42
|
-
return brand({ type: "circular-ref", text });
|
|
43
|
-
}
|
|
44
38
|
function updateText(annotation, text) {
|
|
45
39
|
if (text !== void 0) {
|
|
46
40
|
return brand({ ...annotation, text });
|
|
@@ -81,22 +75,22 @@ function annotate(value, text, seen) {
|
|
|
81
75
|
}
|
|
82
76
|
if (Array.isArray(value)) {
|
|
83
77
|
if (seen.has(value)) {
|
|
84
|
-
return
|
|
78
|
+
return makeOpaqueAnn("<circular ref>", text);
|
|
85
79
|
} else {
|
|
86
80
|
return annotateArray(value, text, seen);
|
|
87
81
|
}
|
|
88
82
|
}
|
|
89
83
|
if (isPojo(value)) {
|
|
90
84
|
if (seen.has(value)) {
|
|
91
|
-
return
|
|
85
|
+
return makeOpaqueAnn("<circular ref>", text);
|
|
92
86
|
} else {
|
|
93
87
|
return annotateObject(value, text, seen);
|
|
94
88
|
}
|
|
95
89
|
}
|
|
96
90
|
if (typeof value === "function") {
|
|
97
|
-
return
|
|
91
|
+
return makeOpaqueAnn("<function>", text);
|
|
98
92
|
}
|
|
99
|
-
return
|
|
93
|
+
return makeOpaqueAnn("???", text);
|
|
100
94
|
}
|
|
101
95
|
function public_annotate(value, text) {
|
|
102
96
|
return annotate(value, text, /* @__PURE__ */ new WeakSet());
|
|
@@ -221,14 +215,12 @@ function serializeAnnotation(ann, prefix = "") {
|
|
|
221
215
|
serialized = serializeArray(ann, prefix);
|
|
222
216
|
} else if (ann.type === "object") {
|
|
223
217
|
serialized = serializeObject(ann, prefix);
|
|
224
|
-
} else if (ann.type === "
|
|
225
|
-
serialized = "<function>";
|
|
226
|
-
} else if (ann.type === "circular-ref") {
|
|
227
|
-
serialized = "<circular ref>";
|
|
228
|
-
} else if (ann.type === "unknown") {
|
|
229
|
-
serialized = "???";
|
|
230
|
-
} else {
|
|
218
|
+
} else if (ann.type === "scalar") {
|
|
231
219
|
serialized = serializeValue(ann.value);
|
|
220
|
+
} else {
|
|
221
|
+
/* @__PURE__ */ ((_) => {
|
|
222
|
+
})(ann);
|
|
223
|
+
serialized = ann.value;
|
|
232
224
|
}
|
|
233
225
|
const text = ann.text;
|
|
234
226
|
if (text !== void 0) {
|
|
@@ -317,8 +309,7 @@ function define(fn) {
|
|
|
317
309
|
function then(next) {
|
|
318
310
|
return define((blob, ok2, err2) => {
|
|
319
311
|
const r1 = decode(blob);
|
|
320
|
-
if (!r1.ok)
|
|
321
|
-
return r1;
|
|
312
|
+
if (!r1.ok) return r1;
|
|
322
313
|
const r2 = isDecoder(next) ? next : next(r1.value, ok2, err2);
|
|
323
314
|
return isDecoder(r2) ? r2.decode(r1.value) : r2;
|
|
324
315
|
});
|
|
@@ -525,8 +516,7 @@ function inexact(decoders) {
|
|
|
525
516
|
const allkeys = new Set(Object.keys(plainObj));
|
|
526
517
|
return object(decoders).transform((safepart) => {
|
|
527
518
|
const safekeys = new Set(Object.keys(decoders));
|
|
528
|
-
for (const k of safekeys)
|
|
529
|
-
allkeys.add(k);
|
|
519
|
+
for (const k of safekeys) allkeys.add(k);
|
|
530
520
|
const rv = {};
|
|
531
521
|
for (const k of allkeys) {
|
|
532
522
|
if (safekeys.has(k)) {
|
package/dist/index.d.cts
CHANGED
|
@@ -13,20 +13,12 @@ interface ScalarAnnotation {
|
|
|
13
13
|
readonly value: unknown;
|
|
14
14
|
readonly text?: string;
|
|
15
15
|
}
|
|
16
|
-
interface
|
|
17
|
-
readonly type: '
|
|
16
|
+
interface OpaqueAnnotation {
|
|
17
|
+
readonly type: 'opaque';
|
|
18
|
+
readonly value: string;
|
|
18
19
|
readonly text?: string;
|
|
19
20
|
}
|
|
20
|
-
|
|
21
|
-
readonly type: 'circular-ref';
|
|
22
|
-
readonly text?: string;
|
|
23
|
-
}
|
|
24
|
-
interface UnknownAnnotation {
|
|
25
|
-
readonly type: 'unknown';
|
|
26
|
-
readonly value: unknown;
|
|
27
|
-
readonly text?: string;
|
|
28
|
-
}
|
|
29
|
-
type Annotation = ObjectAnnotation | ArrayAnnotation | ScalarAnnotation | FunctionAnnotation | CircularRefAnnotation | UnknownAnnotation;
|
|
21
|
+
type Annotation = ObjectAnnotation | ArrayAnnotation | ScalarAnnotation | OpaqueAnnotation;
|
|
30
22
|
|
|
31
23
|
/**
|
|
32
24
|
* Result <value> <error>
|
|
@@ -588,7 +580,7 @@ declare function enum_<TEnum extends Record<string, string | number>>(enumObj: T
|
|
|
588
580
|
* error messages and is more performant at runtime because it doesn't have to
|
|
589
581
|
* try all decoders one by one.
|
|
590
582
|
*/
|
|
591
|
-
declare function taggedUnion<O extends Record<string, Decoder<unknown
|
|
583
|
+
declare function taggedUnion<O extends Record<string, Decoder<unknown>>>(field: string, mapping: O): Decoder<DecoderType<O[keyof O]>>;
|
|
592
584
|
/**
|
|
593
585
|
* Briefly peek at a runtime input using a "scout" decoder first, then decide
|
|
594
586
|
* which decoder to run on the (original) input, based on the information that
|
package/dist/index.d.ts
CHANGED
|
@@ -13,20 +13,12 @@ interface ScalarAnnotation {
|
|
|
13
13
|
readonly value: unknown;
|
|
14
14
|
readonly text?: string;
|
|
15
15
|
}
|
|
16
|
-
interface
|
|
17
|
-
readonly type: '
|
|
16
|
+
interface OpaqueAnnotation {
|
|
17
|
+
readonly type: 'opaque';
|
|
18
|
+
readonly value: string;
|
|
18
19
|
readonly text?: string;
|
|
19
20
|
}
|
|
20
|
-
|
|
21
|
-
readonly type: 'circular-ref';
|
|
22
|
-
readonly text?: string;
|
|
23
|
-
}
|
|
24
|
-
interface UnknownAnnotation {
|
|
25
|
-
readonly type: 'unknown';
|
|
26
|
-
readonly value: unknown;
|
|
27
|
-
readonly text?: string;
|
|
28
|
-
}
|
|
29
|
-
type Annotation = ObjectAnnotation | ArrayAnnotation | ScalarAnnotation | FunctionAnnotation | CircularRefAnnotation | UnknownAnnotation;
|
|
21
|
+
type Annotation = ObjectAnnotation | ArrayAnnotation | ScalarAnnotation | OpaqueAnnotation;
|
|
30
22
|
|
|
31
23
|
/**
|
|
32
24
|
* Result <value> <error>
|
|
@@ -588,7 +580,7 @@ declare function enum_<TEnum extends Record<string, string | number>>(enumObj: T
|
|
|
588
580
|
* error messages and is more performant at runtime because it doesn't have to
|
|
589
581
|
* try all decoders one by one.
|
|
590
582
|
*/
|
|
591
|
-
declare function taggedUnion<O extends Record<string, Decoder<unknown
|
|
583
|
+
declare function taggedUnion<O extends Record<string, Decoder<unknown>>>(field: string, mapping: O): Decoder<DecoderType<O[keyof O]>>;
|
|
592
584
|
/**
|
|
593
585
|
* Briefly peek at a runtime input using a "scout" decoder first, then decide
|
|
594
586
|
* which decoder to run on the (original) input, based on the information that
|
package/dist/index.js
CHANGED
|
@@ -29,18 +29,12 @@ function makeObjectAnn(fields, text) {
|
|
|
29
29
|
function makeArrayAnn(items, text) {
|
|
30
30
|
return brand({ type: "array", items, text });
|
|
31
31
|
}
|
|
32
|
-
function
|
|
33
|
-
return brand({ type: "
|
|
34
|
-
}
|
|
35
|
-
function makeUnknownAnn(value, text) {
|
|
36
|
-
return brand({ type: "unknown", value, text });
|
|
32
|
+
function makeOpaqueAnn(value, text) {
|
|
33
|
+
return brand({ type: "opaque", value, text });
|
|
37
34
|
}
|
|
38
35
|
function makeScalarAnn(value, text) {
|
|
39
36
|
return brand({ type: "scalar", value, text });
|
|
40
37
|
}
|
|
41
|
-
function makeCircularRefAnn(text) {
|
|
42
|
-
return brand({ type: "circular-ref", text });
|
|
43
|
-
}
|
|
44
38
|
function updateText(annotation, text) {
|
|
45
39
|
if (text !== void 0) {
|
|
46
40
|
return brand({ ...annotation, text });
|
|
@@ -81,22 +75,22 @@ function annotate(value, text, seen) {
|
|
|
81
75
|
}
|
|
82
76
|
if (Array.isArray(value)) {
|
|
83
77
|
if (seen.has(value)) {
|
|
84
|
-
return
|
|
78
|
+
return makeOpaqueAnn("<circular ref>", text);
|
|
85
79
|
} else {
|
|
86
80
|
return annotateArray(value, text, seen);
|
|
87
81
|
}
|
|
88
82
|
}
|
|
89
83
|
if (isPojo(value)) {
|
|
90
84
|
if (seen.has(value)) {
|
|
91
|
-
return
|
|
85
|
+
return makeOpaqueAnn("<circular ref>", text);
|
|
92
86
|
} else {
|
|
93
87
|
return annotateObject(value, text, seen);
|
|
94
88
|
}
|
|
95
89
|
}
|
|
96
90
|
if (typeof value === "function") {
|
|
97
|
-
return
|
|
91
|
+
return makeOpaqueAnn("<function>", text);
|
|
98
92
|
}
|
|
99
|
-
return
|
|
93
|
+
return makeOpaqueAnn("???", text);
|
|
100
94
|
}
|
|
101
95
|
function public_annotate(value, text) {
|
|
102
96
|
return annotate(value, text, /* @__PURE__ */ new WeakSet());
|
|
@@ -221,14 +215,12 @@ function serializeAnnotation(ann, prefix = "") {
|
|
|
221
215
|
serialized = serializeArray(ann, prefix);
|
|
222
216
|
} else if (ann.type === "object") {
|
|
223
217
|
serialized = serializeObject(ann, prefix);
|
|
224
|
-
} else if (ann.type === "
|
|
225
|
-
serialized = "<function>";
|
|
226
|
-
} else if (ann.type === "circular-ref") {
|
|
227
|
-
serialized = "<circular ref>";
|
|
228
|
-
} else if (ann.type === "unknown") {
|
|
229
|
-
serialized = "???";
|
|
230
|
-
} else {
|
|
218
|
+
} else if (ann.type === "scalar") {
|
|
231
219
|
serialized = serializeValue(ann.value);
|
|
220
|
+
} else {
|
|
221
|
+
/* @__PURE__ */ ((_) => {
|
|
222
|
+
})(ann);
|
|
223
|
+
serialized = ann.value;
|
|
232
224
|
}
|
|
233
225
|
const text = ann.text;
|
|
234
226
|
if (text !== void 0) {
|
|
@@ -317,8 +309,7 @@ function define(fn) {
|
|
|
317
309
|
function then(next) {
|
|
318
310
|
return define((blob, ok2, err2) => {
|
|
319
311
|
const r1 = decode(blob);
|
|
320
|
-
if (!r1.ok)
|
|
321
|
-
return r1;
|
|
312
|
+
if (!r1.ok) return r1;
|
|
322
313
|
const r2 = isDecoder(next) ? next : next(r1.value, ok2, err2);
|
|
323
314
|
return isDecoder(r2) ? r2.decode(r1.value) : r2;
|
|
324
315
|
});
|
|
@@ -525,8 +516,7 @@ function inexact(decoders) {
|
|
|
525
516
|
const allkeys = new Set(Object.keys(plainObj));
|
|
526
517
|
return object(decoders).transform((safepart) => {
|
|
527
518
|
const safekeys = new Set(Object.keys(decoders));
|
|
528
|
-
for (const k of safekeys)
|
|
529
|
-
allkeys.add(k);
|
|
519
|
+
for (const k of safekeys) allkeys.add(k);
|
|
530
520
|
const rv = {};
|
|
531
521
|
for (const k of allkeys) {
|
|
532
522
|
if (safekeys.has(k)) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "decoders",
|
|
3
|
-
"version": "2.4.
|
|
3
|
+
"version": "2.4.2",
|
|
4
4
|
"description": "Elegant and battle-tested validation library for type-safe input data for TypeScript",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
"lint": "eslint --color --report-unused-disable-directives src/ test/ && prettier --list-different src/ test/ test-d/",
|
|
42
42
|
"lint:docs": "cog -c --check docs/*.md || (npm run docs; git diff; echo 'Error: docs not up-to-date, please re-run \"npm docs\" to update them.' && exit 1)",
|
|
43
43
|
"lint:package": "publint --strict && attw --pack",
|
|
44
|
-
"format": "eslint --color --report-unused-disable-directives --fix src/ test/
|
|
44
|
+
"format": "eslint --color --report-unused-disable-directives --fix src/ test/ ; prettier --write src/ test/ test-d/",
|
|
45
45
|
"test": "vitest run --coverage",
|
|
46
46
|
"test:completeness": "./bin/check.sh",
|
|
47
47
|
"test:typescript": "tsc --noEmit",
|
|
@@ -61,25 +61,27 @@
|
|
|
61
61
|
"verify"
|
|
62
62
|
],
|
|
63
63
|
"devDependencies": {
|
|
64
|
-
"@arethetypeswrong/cli": "^0.
|
|
64
|
+
"@arethetypeswrong/cli": "^0.15.3",
|
|
65
65
|
"@release-it/keep-a-changelog": "^5.0.0",
|
|
66
|
-
"@
|
|
67
|
-
"@typescript-eslint/
|
|
68
|
-
"@
|
|
69
|
-
"
|
|
66
|
+
"@types/eslint": "^8.56.10",
|
|
67
|
+
"@typescript-eslint/eslint-plugin": "^7.14.1",
|
|
68
|
+
"@typescript-eslint/parser": "^7.14.1",
|
|
69
|
+
"@vitest/coverage-istanbul": "^1.6.0",
|
|
70
|
+
"eslint": "^8.57.0",
|
|
70
71
|
"eslint-plugin-import": "^2.29.1",
|
|
71
|
-
"eslint-plugin-simple-import-sort": "^
|
|
72
|
-
"fast-check": "^3.
|
|
73
|
-
"itertools": "^2.2
|
|
74
|
-
"
|
|
75
|
-
"
|
|
76
|
-
"
|
|
77
|
-
"
|
|
78
|
-
"
|
|
79
|
-
"
|
|
80
|
-
"
|
|
81
|
-
"
|
|
82
|
-
"
|
|
72
|
+
"eslint-plugin-simple-import-sort": "^12.1.0",
|
|
73
|
+
"fast-check": "^3.19.0",
|
|
74
|
+
"itertools": "^2.3.2",
|
|
75
|
+
"pkg-pr-new": "^0.0.9",
|
|
76
|
+
"prettier": "^3.3.2",
|
|
77
|
+
"publint": "^0.2.8",
|
|
78
|
+
"release-it": "^17.4.0",
|
|
79
|
+
"ts-morph": "^23.0.0",
|
|
80
|
+
"tsd": "^0.31.1",
|
|
81
|
+
"tsup": "^8.1.0",
|
|
82
|
+
"typescript": "^5.5.2",
|
|
83
|
+
"vite-tsconfig-paths": "^4.3.2",
|
|
84
|
+
"vitest": "^1.6.0"
|
|
83
85
|
},
|
|
84
86
|
"githubUrl": "https://github.com/nvie/decoders",
|
|
85
87
|
"sideEffects": false
|