@nerd-bible/valio 0.0.11 → 0.1.0
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/.github/workflows/publish.yml +26 -0
- package/.github/workflows/test.yml +14 -0
- package/LICENSE.md +111 -0
- package/README.md +2 -2
- package/package.json +16 -17
- package/publish.ts +52 -0
- package/src/codecs.test.ts +4 -3
- package/src/codecs.ts +23 -24
- package/src/containers.test.ts +3 -2
- package/src/containers.ts +68 -39
- package/src/index.ts +4 -4
- package/src/pipe.ts +36 -22
- package/src/primitives.test.ts +5 -4
- package/src/primitives.ts +18 -12
- package/test/conllu.ts +226 -0
- package/test/gum.conllu +66 -0
- package/tsconfig.build.json +4 -0
- package/tsconfig.json +22 -0
- package/dist/codecs.d.ts +0 -12
- package/dist/codecs.d.ts.map +0 -1
- package/dist/codecs.js +0 -38
- package/dist/codecs.js.map +0 -1
- package/dist/containers.d.ts +0 -54
- package/dist/containers.d.ts.map +0 -1
- package/dist/containers.js +0 -193
- package/dist/containers.js.map +0 -1
- package/dist/index.d.ts +0 -5
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -5
- package/dist/index.js.map +0 -1
- package/dist/locales/en.d.ts +0 -2
- package/dist/locales/en.d.ts.map +0 -1
- package/dist/locales/en.js +0 -22
- package/dist/locales/en.js.map +0 -1
- package/dist/pipe.d.ts +0 -66
- package/dist/pipe.d.ts.map +0 -1
- package/dist/pipe.js +0 -150
- package/dist/pipe.js.map +0 -1
- package/dist/primitives.d.ts +0 -41
- package/dist/primitives.d.ts.map +0 -1
- package/dist/primitives.js +0 -89
- package/dist/primitives.js.map +0 -1
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
name: Publish
|
|
2
|
+
on:
|
|
3
|
+
push:
|
|
4
|
+
tags:
|
|
5
|
+
- v[0-9]+.[0-9]+.[0-9]+
|
|
6
|
+
workflow_dispatch:
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: read
|
|
10
|
+
id-token: write
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
publish:
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
|
|
16
|
+
steps:
|
|
17
|
+
- uses: actions/checkout@v4
|
|
18
|
+
with:
|
|
19
|
+
fetch-depth: 0
|
|
20
|
+
- uses: actions/setup-node@v6
|
|
21
|
+
with:
|
|
22
|
+
node-version: lts/*
|
|
23
|
+
registry-url: 'https://registry.npmjs.org'
|
|
24
|
+
- run: npm install
|
|
25
|
+
- run: npm test
|
|
26
|
+
- run: node ./publish.ts "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY"
|
package/LICENSE.md
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
Creative Commons Legal Code
|
|
2
|
+
|
|
3
|
+
CC0 1.0 Universal
|
|
4
|
+
|
|
5
|
+
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
|
|
6
|
+
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
|
|
7
|
+
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
|
|
8
|
+
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
|
|
9
|
+
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
|
|
10
|
+
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
|
|
11
|
+
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
|
|
12
|
+
HEREUNDER.
|
|
13
|
+
|
|
14
|
+
Statement of Purpose
|
|
15
|
+
|
|
16
|
+
The laws of most jurisdictions throughout the world automatically confer
|
|
17
|
+
exclusive Copyright and Related Rights (defined below) upon the creator and
|
|
18
|
+
subsequent owner(s) (each and all, an "owner") of an original work of authorship
|
|
19
|
+
and/or a database (each, a "Work").
|
|
20
|
+
|
|
21
|
+
Certain owners wish to permanently relinquish those rights to a Work for the
|
|
22
|
+
purpose of contributing to a commons of creative, cultural and scientific works
|
|
23
|
+
("Commons") that the public can reliably and without fear of later claims of
|
|
24
|
+
infringement build upon, modify, incorporate in other works, reuse and
|
|
25
|
+
redistribute as freely as possible in any form whatsoever and for any purposes,
|
|
26
|
+
including without limitation commercial purposes. These owners may contribute to
|
|
27
|
+
the Commons to promote the ideal of a free culture and the further production of
|
|
28
|
+
creative, cultural and scientific works, or to gain reputation or greater
|
|
29
|
+
distribution for their Work in part through the use and efforts of others.
|
|
30
|
+
|
|
31
|
+
For these and/or other purposes and motivations, and without any expectation of
|
|
32
|
+
additional consideration or compensation, the person associating CC0 with a Work
|
|
33
|
+
(the "Affirmer"), to the extent that he or she is an owner of Copyright and
|
|
34
|
+
Related Rights in the Work, voluntarily elects to apply CC0 to the Work and
|
|
35
|
+
publicly distribute the Work under its terms, with knowledge of his or her
|
|
36
|
+
Copyright and Related Rights in the Work and the meaning and intended legal
|
|
37
|
+
effect of CC0 on those rights.
|
|
38
|
+
|
|
39
|
+
1. Copyright and Related Rights. A Work made available under CC0 may be
|
|
40
|
+
protected by copyright and related or neighboring rights ("Copyright and
|
|
41
|
+
Related Rights"). Copyright and Related Rights include, but are not limited
|
|
42
|
+
to, the following:
|
|
43
|
+
|
|
44
|
+
i. the right to reproduce, adapt, distribute, perform, display, communicate, and
|
|
45
|
+
translate a Work; ii. moral rights retained by the original author(s) and/or
|
|
46
|
+
performer(s); iii. publicity and privacy rights pertaining to a person's image
|
|
47
|
+
or likeness depicted in a Work; iv. rights protecting against unfair competition
|
|
48
|
+
in regards to a Work, subject to the limitations in paragraph 4(a), below; v.
|
|
49
|
+
rights protecting the extraction, dissemination, use and reuse of data in a
|
|
50
|
+
Work; vi. database rights (such as those arising under Directive 96/9/EC of the
|
|
51
|
+
European Parliament and of the Council of 11 March 1996 on the legal protection
|
|
52
|
+
of databases, and under any national implementation thereof, including any
|
|
53
|
+
amended or successor version of such directive); and vii. other similar,
|
|
54
|
+
equivalent or corresponding rights throughout the world based on applicable law
|
|
55
|
+
or treaty, and any national implementations thereof.
|
|
56
|
+
|
|
57
|
+
2. Waiver. To the greatest extent permitted by, but not in contravention of,
|
|
58
|
+
applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and
|
|
59
|
+
unconditionally waives, abandons, and surrenders all of Affirmer's Copyright
|
|
60
|
+
and Related Rights and associated claims and causes of action, whether now
|
|
61
|
+
known or unknown (including existing as well as future claims and causes of
|
|
62
|
+
action), in the Work (i) in all territories worldwide, (ii) for the maximum
|
|
63
|
+
duration provided by applicable law or treaty (including future time
|
|
64
|
+
extensions), (iii) in any current or future medium and for any number of
|
|
65
|
+
copies, and (iv) for any purpose whatsoever, including without limitation
|
|
66
|
+
commercial, advertising or promotional purposes (the "Waiver"). Affirmer
|
|
67
|
+
makes the Waiver for the benefit of each member of the public at large and to
|
|
68
|
+
the detriment of Affirmer's heirs and successors, fully intending that such
|
|
69
|
+
Waiver shall not be subject to revocation, rescission, cancellation,
|
|
70
|
+
termination, or any other legal or equitable action to disrupt the quiet
|
|
71
|
+
enjoyment of the Work by the public as contemplated by Affirmer's express
|
|
72
|
+
Statement of Purpose.
|
|
73
|
+
|
|
74
|
+
3. Public License Fallback. Should any part of the Waiver for any reason be
|
|
75
|
+
judged legally invalid or ineffective under applicable law, then the Waiver
|
|
76
|
+
shall be preserved to the maximum extent permitted taking into account
|
|
77
|
+
Affirmer's express Statement of Purpose. In addition, to the extent the
|
|
78
|
+
Waiver is so judged Affirmer hereby grants to each affected person a
|
|
79
|
+
royalty-free, non transferable, non sublicensable, non exclusive, irrevocable
|
|
80
|
+
and unconditional license to exercise Affirmer's Copyright and Related Rights
|
|
81
|
+
in the Work (i) in all territories worldwide, (ii) for the maximum duration
|
|
82
|
+
provided by applicable law or treaty (including future time extensions),
|
|
83
|
+
(iii) in any current or future medium and for any number of copies, and (iv)
|
|
84
|
+
for any purpose whatsoever, including without limitation commercial,
|
|
85
|
+
advertising or promotional purposes (the "License"). The License shall be
|
|
86
|
+
deemed effective as of the date CC0 was applied by Affirmer to the Work.
|
|
87
|
+
Should any part of the License for any reason be judged legally invalid or
|
|
88
|
+
ineffective under applicable law, such partial invalidity or ineffectiveness
|
|
89
|
+
shall not invalidate the remainder of the License, and in such case Affirmer
|
|
90
|
+
hereby affirms that he or she will not (i) exercise any of his or her
|
|
91
|
+
remaining Copyright and Related Rights in the Work or (ii) assert any
|
|
92
|
+
associated claims and causes of action with respect to the Work, in either
|
|
93
|
+
case contrary to Affirmer's express Statement of Purpose.
|
|
94
|
+
|
|
95
|
+
4. Limitations and Disclaimers.
|
|
96
|
+
|
|
97
|
+
a. No trademark or patent rights held by Affirmer are waived, abandoned,
|
|
98
|
+
surrendered, licensed or otherwise affected by this document. b. Affirmer offers
|
|
99
|
+
the Work as-is and makes no representations or warranties of any kind concerning
|
|
100
|
+
the Work, express, implied, statutory or otherwise, including without limitation
|
|
101
|
+
warranties of title, merchantability, fitness for a particular purpose, non
|
|
102
|
+
infringement, or the absence of latent or other defects, accuracy, or the
|
|
103
|
+
present or absence of errors, whether or not discoverable, all to the greatest
|
|
104
|
+
extent permissible under applicable law. c. Affirmer disclaims responsibility
|
|
105
|
+
for clearing rights of other persons that may apply to the Work or any use
|
|
106
|
+
thereof, including without limitation any person's Copyright and Related Rights
|
|
107
|
+
in the Work. Further, Affirmer disclaims responsibility for obtaining any
|
|
108
|
+
necessary consents, permissions or other rights required for any use of the
|
|
109
|
+
Work. d. Affirmer understands and acknowledges that Creative Commons is not a
|
|
110
|
+
party to this document and has no duty or obligation with respect to this CC0 or
|
|
111
|
+
use of the Work.
|
package/README.md
CHANGED
|
@@ -35,8 +35,8 @@ const schema = v.number(); // Pipe<number, number>
|
|
|
35
35
|
|
|
36
36
|
expect(schema.decode(5)).toEqual({ success: true, output: 5 });
|
|
37
37
|
expect(schema.decodeAny("5")).toEqual({
|
|
38
|
-
success: false,
|
|
39
|
-
errors: { ".": [{ input: "5", message: "not type number" }] },
|
|
38
|
+
success: false,
|
|
39
|
+
errors: { ".": [{ input: "5", message: "not type number" }] },
|
|
40
40
|
});
|
|
41
41
|
```
|
|
42
42
|
|
package/package.json
CHANGED
|
@@ -1,27 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nerd-bible/valio",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
"exports": {
|
|
5
|
+
".": {
|
|
6
|
+
"import": {
|
|
7
|
+
"nbsource": "./src/index.ts",
|
|
8
|
+
"node": "./dist/index.js"
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
"./package.json": "./package.json"
|
|
9
12
|
},
|
|
10
13
|
"scripts": {
|
|
11
|
-
"build": "tsc",
|
|
12
|
-
"
|
|
13
|
-
"fmt-fix": "biome check --write --unsafe"
|
|
14
|
+
"build": "tsc -p ./tsconfig.build.json",
|
|
15
|
+
"test": "node --test"
|
|
14
16
|
},
|
|
15
|
-
"
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@types/node": "^25.3.3",
|
|
19
|
+
"expect": "^30.2.0",
|
|
16
20
|
"typescript": "^5.9.3"
|
|
17
21
|
},
|
|
18
|
-
"
|
|
19
|
-
"README.md",
|
|
20
|
-
"src",
|
|
21
|
-
"dist"
|
|
22
|
-
],
|
|
23
|
-
"version": "0.0.11",
|
|
22
|
+
"version": "0.1.0",
|
|
24
23
|
"repository": {
|
|
25
|
-
"url": "
|
|
24
|
+
"url": "https://github.com/nerd-bible/valio"
|
|
26
25
|
}
|
|
27
|
-
}
|
|
26
|
+
}
|
package/publish.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// This script allows using Git tags as the source of truth for versions rather
|
|
2
|
+
// than package.json.
|
|
3
|
+
import { execSync } from "node:child_process";
|
|
4
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
5
|
+
|
|
6
|
+
const repoUrl = process.argv.slice(2).find((a) => !a.startsWith("--"));
|
|
7
|
+
if (!repoUrl) throw Error("Required arg: repo URL");
|
|
8
|
+
const dry = process.argv.includes("--dry");
|
|
9
|
+
|
|
10
|
+
function execGit(args: string, allowFailure = false) {
|
|
11
|
+
try {
|
|
12
|
+
return execSync(`git ${args}`, {
|
|
13
|
+
encoding: "utf8",
|
|
14
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
15
|
+
}).trim();
|
|
16
|
+
} catch (err) {
|
|
17
|
+
if (!allowFailure) throw err;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
execGit("fetch --tags");
|
|
22
|
+
const tagCmd = "describe --tags --abbrev=0 --match='v[0-9]*.[0-9]*.[0-9]*'";
|
|
23
|
+
let version = execGit(`${tagCmd} --exact-match`, true);
|
|
24
|
+
|
|
25
|
+
if (!version) {
|
|
26
|
+
const lastVersion = execGit(tagCmd, true) ?? "v0.0.0";
|
|
27
|
+
console.log("No manual tag, bumping", lastVersion);
|
|
28
|
+
|
|
29
|
+
const split = lastVersion.split(".");
|
|
30
|
+
// Tags should only be missing for minor bumps.
|
|
31
|
+
const last = parseInt(split.pop()!) + 1;
|
|
32
|
+
version = [...split, last].join(".");
|
|
33
|
+
|
|
34
|
+
if (!dry) {
|
|
35
|
+
console.log("Tagging + pushing", version);
|
|
36
|
+
execGit(`tag ${version}`);
|
|
37
|
+
execGit("push --tags origin master");
|
|
38
|
+
}
|
|
39
|
+
} else {
|
|
40
|
+
console.log("Using manual tag", version);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (!dry) {
|
|
44
|
+
console.log("Writing temporary package.json");
|
|
45
|
+
const pkg = JSON.parse(readFileSync("package.json", "utf8"));
|
|
46
|
+
pkg.version = version.substring(1);
|
|
47
|
+
pkg.repository = { url: repoUrl };
|
|
48
|
+
writeFileSync("package.json", JSON.stringify(pkg, null, 2));
|
|
49
|
+
|
|
50
|
+
console.log("Publishing to NPM", version);
|
|
51
|
+
execSync("npm publish --access public");
|
|
52
|
+
}
|
package/src/codecs.test.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
1
|
+
import { test } from "node:test";
|
|
2
|
+
import expect from "expect";
|
|
3
|
+
import * as v from "./index.ts";
|
|
3
4
|
|
|
4
5
|
test("number codec", () => {
|
|
5
6
|
const schema = v.codecs.number();
|
|
@@ -65,7 +66,7 @@ test("array number codec", () => {
|
|
|
65
66
|
});
|
|
66
67
|
|
|
67
68
|
test("boolean codec", () => {
|
|
68
|
-
const schema = v.codecs.boolean({ true: [
|
|
69
|
+
const schema = v.codecs.boolean({ true: /yes|^[1-9]/, false: /no|^0?$/ });
|
|
69
70
|
|
|
70
71
|
expect(schema.decode("yes")).toEqual({ success: true, output: true });
|
|
71
72
|
expect(schema.decode("no")).toEqual({ success: true, output: false });
|
package/src/codecs.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import * as p from "./primitives";
|
|
1
|
+
import type { Context, Pipe, Result } from "./pipe.ts";
|
|
2
|
+
import * as p from "./primitives.ts";
|
|
4
3
|
|
|
5
4
|
export function custom<I, O>(
|
|
6
5
|
input: Pipe<I, any>,
|
|
@@ -20,37 +19,37 @@ export function custom<I, O>(
|
|
|
20
19
|
export function number(
|
|
21
20
|
parser = Number.parseFloat,
|
|
22
21
|
): p.Comparable<string | number | null | undefined, number> {
|
|
23
|
-
return custom(
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (typeof input == "number") return { success: true, output: input };
|
|
29
|
-
if (input == null || input.toLowerCase() == "nan")
|
|
30
|
-
return { success: true, output: Number.NaN };
|
|
22
|
+
return custom(p.any(), p.number(), {
|
|
23
|
+
decode(input, ctx) {
|
|
24
|
+
if (typeof input === "number") return { success: true, output: input };
|
|
25
|
+
if (!input || input.toLowerCase() === "nan")
|
|
26
|
+
return { success: true, output: Number.NaN };
|
|
31
27
|
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
const output = parser(input);
|
|
29
|
+
if (!Number.isNaN(output)) return { success: true, output };
|
|
34
30
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
},
|
|
31
|
+
ctx.pushErrorFmt("coerce", input, { expected: "number" });
|
|
32
|
+
return { success: false, errors: ctx.errors };
|
|
38
33
|
},
|
|
39
|
-
) as ReturnType<typeof number>;
|
|
34
|
+
}) as ReturnType<typeof number>;
|
|
40
35
|
}
|
|
41
36
|
|
|
42
37
|
export function boolean(opts: {
|
|
43
|
-
true?:
|
|
44
|
-
false?:
|
|
38
|
+
true?: RegExp;
|
|
39
|
+
false?: RegExp;
|
|
45
40
|
}): Pipe<any, boolean> {
|
|
46
41
|
return custom(p.any(), p.boolean(), {
|
|
47
|
-
decode(input) {
|
|
42
|
+
decode(input, ctx) {
|
|
43
|
+
if (typeof input === "boolean") return { success: true, output: input };
|
|
48
44
|
if (typeof input === "string") {
|
|
49
|
-
if (opts.true?.
|
|
50
|
-
if (opts.false?.
|
|
51
|
-
return { success: true, output: false };
|
|
45
|
+
if (opts.true?.test(input)) return { success: true, output: true };
|
|
46
|
+
if (opts.false?.test(input)) return { success: true, output: false };
|
|
52
47
|
}
|
|
53
|
-
|
|
48
|
+
if (typeof input === "number")
|
|
49
|
+
return { success: true, output: Boolean(input) };
|
|
50
|
+
|
|
51
|
+
ctx.pushErrorFmt("coerce", input, { expected: "boolean" });
|
|
52
|
+
return { success: false, errors: ctx.errors };
|
|
54
53
|
},
|
|
55
54
|
});
|
|
56
55
|
}
|
package/src/containers.test.ts
CHANGED
package/src/containers.ts
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
|
-
import type { Input, Output, Result } from "./pipe";
|
|
2
|
-
import {
|
|
3
|
-
import * as p from "./primitives";
|
|
1
|
+
import type { Input, Output, Result } from "./pipe.ts";
|
|
2
|
+
import { Context, HalfPipe, Pipe } from "./pipe.ts";
|
|
3
|
+
import * as p from "./primitives.ts";
|
|
4
4
|
|
|
5
5
|
class ValioArray<T> extends p.Arrayish<any[], T[]> {
|
|
6
|
-
|
|
6
|
+
element: Pipe<any, T>;
|
|
7
|
+
|
|
8
|
+
static typeCheck(v: any): v is any[] {
|
|
9
|
+
return Array.isArray(v);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
constructor(element: Pipe<any, T>) {
|
|
7
13
|
super(
|
|
8
14
|
new HalfPipe(
|
|
9
15
|
"array",
|
|
10
|
-
|
|
11
|
-
(input: any[], ctx: Context): Result<T[]>
|
|
16
|
+
ValioArray.typeCheck,
|
|
17
|
+
function parseAnyArr(input: any[], ctx: Context): Result<T[]> {
|
|
12
18
|
const output = new Array<T>(input.length);
|
|
13
19
|
let success = true;
|
|
14
20
|
|
|
@@ -25,12 +31,16 @@ class ValioArray<T> extends p.Arrayish<any[], T[]> {
|
|
|
25
31
|
return { success, output };
|
|
26
32
|
},
|
|
27
33
|
),
|
|
28
|
-
new HalfPipe(
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
34
|
+
new HalfPipe(
|
|
35
|
+
`array<${element.o.name}>`,
|
|
36
|
+
function isArrT(v: any): v is T[] {
|
|
37
|
+
if (!ValioArray.typeCheck(v)) return false;
|
|
38
|
+
for (const e of v) if (!element.o.typeCheck(e)) return false;
|
|
39
|
+
return true;
|
|
40
|
+
},
|
|
41
|
+
),
|
|
33
42
|
);
|
|
43
|
+
this.element = element;
|
|
34
44
|
}
|
|
35
45
|
}
|
|
36
46
|
export function array<T>(element: Pipe<any, T>): ValioArray<T> {
|
|
@@ -41,16 +51,22 @@ class ValioRecord<K extends PropertyKey, V> extends Pipe<
|
|
|
41
51
|
Record<any, any>,
|
|
42
52
|
Record<K, V>
|
|
43
53
|
> {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
) {
|
|
54
|
+
keyPipe: Pipe<any, K>;
|
|
55
|
+
valPipe: Pipe<any, V>;
|
|
56
|
+
|
|
57
|
+
static typeCheck(v: any): v is Record<any, any> {
|
|
58
|
+
return Object.prototype.toString.call(v) === "[object Object]";
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
constructor(keyPipe: Pipe<any, K>, valPipe: Pipe<any, V>) {
|
|
48
62
|
super(
|
|
49
63
|
new HalfPipe(
|
|
50
64
|
"object",
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
65
|
+
ValioRecord.typeCheck,
|
|
66
|
+
function anyToRecordKV(
|
|
67
|
+
input: Record<any, any>,
|
|
68
|
+
ctx: Context,
|
|
69
|
+
): Result<Record<K, V>> {
|
|
54
70
|
const output = {} as Record<K, V>;
|
|
55
71
|
|
|
56
72
|
let success = true;
|
|
@@ -77,9 +93,8 @@ class ValioRecord<K extends PropertyKey, V> extends Pipe<
|
|
|
77
93
|
),
|
|
78
94
|
new HalfPipe(
|
|
79
95
|
`record<${keyPipe.o.name},${valPipe.o.name}>`,
|
|
80
|
-
(v): v is Record<K, V>
|
|
81
|
-
if (
|
|
82
|
-
return false;
|
|
96
|
+
function recordCheckV(v): v is Record<K, V> {
|
|
97
|
+
if (!ValioRecord.typeCheck(v)) return false;
|
|
83
98
|
for (const k in v) {
|
|
84
99
|
// Keys will always be strings.
|
|
85
100
|
// if (!keyPipe.o.typeCheck(k)) return false;
|
|
@@ -89,6 +104,8 @@ class ValioRecord<K extends PropertyKey, V> extends Pipe<
|
|
|
89
104
|
},
|
|
90
105
|
),
|
|
91
106
|
);
|
|
107
|
+
this.keyPipe = keyPipe;
|
|
108
|
+
this.valPipe = valPipe;
|
|
92
109
|
}
|
|
93
110
|
}
|
|
94
111
|
export function record<K extends PropertyKey, V>(
|
|
@@ -102,32 +119,39 @@ class Union<T extends Readonly<Pipe[]>> extends Pipe<
|
|
|
102
119
|
Output<T[number]>,
|
|
103
120
|
Output<T[number]>
|
|
104
121
|
> {
|
|
105
|
-
|
|
122
|
+
options: T;
|
|
123
|
+
|
|
124
|
+
constructor(options: T) {
|
|
106
125
|
const name = options.map((o) => o.o.name).join("|");
|
|
107
126
|
type O = Output<T[number]>;
|
|
108
127
|
super(
|
|
109
128
|
new HalfPipe(
|
|
110
129
|
name,
|
|
111
|
-
(v: any): v is O
|
|
130
|
+
function isUnionType(v: any): v is O {
|
|
112
131
|
for (const f of options) if (f.i.typeCheck(v)) return true;
|
|
113
132
|
return false;
|
|
114
133
|
},
|
|
115
134
|
(data: O, ctx: Context): Result<O> => {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
135
|
+
// Throw away errors since we expect them.
|
|
136
|
+
const newCtx = new Context();
|
|
137
|
+
newCtx.pushErrorFmt = () => {};
|
|
138
|
+
newCtx.pushError = () => {};
|
|
139
|
+
for (const f of options) {
|
|
140
|
+
const decoded = f.decode(data, newCtx);
|
|
119
141
|
if (decoded.success) return decoded;
|
|
120
142
|
}
|
|
121
143
|
|
|
122
|
-
|
|
144
|
+
// Sad path -- do again with real ctx to gather errors.
|
|
145
|
+
for (const f of options) f.decode(data, ctx);
|
|
123
146
|
return { success: false, errors: ctx.errors };
|
|
124
147
|
},
|
|
125
148
|
),
|
|
126
|
-
new HalfPipe(name, (v: any): v is O
|
|
149
|
+
new HalfPipe(name, function isUnionType2(v: any): v is O {
|
|
127
150
|
for (const f of options) if (f.o.typeCheck(v)) return true;
|
|
128
151
|
return false;
|
|
129
152
|
}),
|
|
130
153
|
);
|
|
154
|
+
this.options = options;
|
|
131
155
|
}
|
|
132
156
|
}
|
|
133
157
|
export function union<T extends Readonly<Pipe[]>>(options: T): Union<T> {
|
|
@@ -151,19 +175,21 @@ type Extend<A extends Record<any, any>, B extends Record<any, any>> = Flatten<
|
|
|
151
175
|
}
|
|
152
176
|
>;
|
|
153
177
|
|
|
154
|
-
class ValioObject<
|
|
155
|
-
Record<any, any
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
178
|
+
export class ValioObject<
|
|
179
|
+
Shape extends Record<any, Pipe<any, any>>,
|
|
180
|
+
> extends Pipe<Record<any, any>, ObjectOutput<Shape>> {
|
|
181
|
+
shape: Shape;
|
|
182
|
+
isLoose: boolean;
|
|
183
|
+
|
|
184
|
+
static typeCheck(v: any): v is Record<any, any> {
|
|
185
|
+
return typeof v === "object";
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
constructor(shape: Shape, isLoose: boolean) {
|
|
162
189
|
super(
|
|
163
190
|
new HalfPipe(
|
|
164
191
|
"object",
|
|
165
|
-
|
|
166
|
-
Object.prototype.toString.call(v) == "[object Object]",
|
|
192
|
+
ValioObject.typeCheck,
|
|
167
193
|
(data, ctx) => this.transformInput(data, ctx),
|
|
168
194
|
),
|
|
169
195
|
new HalfPipe(
|
|
@@ -173,6 +199,9 @@ class ValioObject<Shape extends Record<any, Pipe<any, any>>> extends Pipe<
|
|
|
173
199
|
(v) => this.typeCheckOutput(v),
|
|
174
200
|
),
|
|
175
201
|
);
|
|
202
|
+
|
|
203
|
+
this.shape = shape;
|
|
204
|
+
this.isLoose = isLoose;
|
|
176
205
|
}
|
|
177
206
|
|
|
178
207
|
clone(): this {
|
|
@@ -204,7 +233,7 @@ class ValioObject<Shape extends Record<any, Pipe<any, any>>> extends Pipe<
|
|
|
204
233
|
}
|
|
205
234
|
|
|
206
235
|
protected typeCheckOutput(v: any): v is ObjectOutput<Shape> {
|
|
207
|
-
if (
|
|
236
|
+
if (!ValioObject.typeCheck(v)) return false;
|
|
208
237
|
for (const s in this.shape)
|
|
209
238
|
if (!this.shape[s]!.o.typeCheck(v[s])) return false;
|
|
210
239
|
return true;
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export * as codecs from "./codecs";
|
|
2
|
-
export * from "./containers";
|
|
3
|
-
export * from "./pipe";
|
|
4
|
-
export * from "./primitives";
|
|
1
|
+
export * as codecs from "./codecs.ts";
|
|
2
|
+
export * from "./containers.ts";
|
|
3
|
+
export * from "./pipe.ts";
|
|
4
|
+
export * from "./primitives.ts";
|
package/src/pipe.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import enFormat from "./locales/en";
|
|
1
|
+
import enFormat from "./locales/en.ts";
|
|
2
2
|
|
|
3
3
|
export type Error = { input: any; message: string };
|
|
4
4
|
export type Errors = { [inputPath: string]: Error[] };
|
|
@@ -20,21 +20,31 @@ interface Check<T> {
|
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
export class HalfPipe<I, O = never> {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
public typeCheck: (v: any) => v is I,
|
|
28
|
-
/** Optional transform for pipe to run at end. Useful for containers */
|
|
29
|
-
public transform?: (v: I, ctx: Context) => Result<O>,
|
|
30
|
-
) {}
|
|
31
|
-
/** The second checks to run */
|
|
23
|
+
name: string;
|
|
24
|
+
typeCheck: (v: any) => v is I;
|
|
25
|
+
transform?: (v: I, ctx: Context) => Result<O>;
|
|
26
|
+
/** Checks to run after type check */
|
|
32
27
|
checks: Check<I>[] = [];
|
|
33
28
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
29
|
+
constructor(
|
|
30
|
+
name: string,
|
|
31
|
+
typeCheck: (v: any) => v is I,
|
|
32
|
+
transform?: (v: I, ctx: Context) => Result<O>,
|
|
33
|
+
checks: Check<I>[] = [],
|
|
34
|
+
) {
|
|
35
|
+
this.name = name;
|
|
36
|
+
this.typeCheck = typeCheck;
|
|
37
|
+
this.transform = transform;
|
|
38
|
+
this.checks = checks;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
clone(): HalfPipe<I, O> {
|
|
42
|
+
return new HalfPipe(
|
|
43
|
+
this.name,
|
|
44
|
+
this.typeCheck,
|
|
45
|
+
this.transform,
|
|
46
|
+
this.checks.slice(),
|
|
47
|
+
);
|
|
38
48
|
}
|
|
39
49
|
}
|
|
40
50
|
|
|
@@ -48,9 +58,9 @@ export class Context {
|
|
|
48
58
|
}
|
|
49
59
|
|
|
50
60
|
clone(): Context {
|
|
51
|
-
const res =
|
|
52
|
-
res.jsonPath =
|
|
53
|
-
res.errors = { ...
|
|
61
|
+
const res = new Context();
|
|
62
|
+
res.jsonPath = this.jsonPath.slice();
|
|
63
|
+
res.errors = { ...this.errors };
|
|
54
64
|
return res;
|
|
55
65
|
}
|
|
56
66
|
|
|
@@ -83,13 +93,17 @@ export class Context {
|
|
|
83
93
|
}
|
|
84
94
|
|
|
85
95
|
export class Pipe<I = any, O = any> {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
) {
|
|
96
|
+
i: HalfPipe<I, O>;
|
|
97
|
+
o: HalfPipe<O, I>;
|
|
98
|
+
|
|
99
|
+
constructor(i: HalfPipe<I, O>, o: HalfPipe<O, I>) {
|
|
100
|
+
this.i = i;
|
|
101
|
+
this.o = o;
|
|
102
|
+
}
|
|
90
103
|
|
|
91
104
|
pipes: Pipe<any, any>[] = [];
|
|
92
105
|
registry: Record<PropertyKey, any> = {};
|
|
106
|
+
debug = false;
|
|
93
107
|
|
|
94
108
|
clone(): this {
|
|
95
109
|
const res = clone(this);
|
|
@@ -103,7 +117,7 @@ export class Pipe<I = any, O = any> {
|
|
|
103
117
|
refine(
|
|
104
118
|
valid: (data: O, ctx: Context) => boolean,
|
|
105
119
|
name: string,
|
|
106
|
-
props: Record<any, any
|
|
120
|
+
props: Record<any, any> = {},
|
|
107
121
|
): this {
|
|
108
122
|
const res = this.clone();
|
|
109
123
|
res.o.checks.push({ valid, name, props });
|