@draht/schema 0.0.0-stage → 2026.11.5-1
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/LICENSES/typebox-LICENSE +23 -0
- package/dist/annotations.d.ts +44 -0
- package/dist/annotations.d.ts.map +1 -0
- package/dist/annotations.js +193 -0
- package/dist/annotations.js.map +1 -0
- package/dist/builders.d.ts +114 -0
- package/dist/builders.d.ts.map +1 -0
- package/dist/builders.js +98 -0
- package/dist/builders.js.map +1 -0
- package/dist/checks.d.ts +96 -0
- package/dist/checks.d.ts.map +1 -0
- package/dist/checks.js +540 -0
- package/dist/checks.js.map +1 -0
- package/dist/coerce.d.ts +53 -0
- package/dist/coerce.d.ts.map +1 -0
- package/dist/coerce.js +585 -0
- package/dist/coerce.js.map +1 -0
- package/dist/compiler.d.ts +53 -0
- package/dist/compiler.d.ts.map +1 -0
- package/dist/compiler.js +546 -0
- package/dist/compiler.js.map +1 -0
- package/dist/explain.d.ts +29 -0
- package/dist/explain.d.ts.map +1 -0
- package/dist/explain.js +382 -0
- package/dist/explain.js.map +1 -0
- package/dist/formats/index.d.ts +7 -0
- package/dist/formats/index.d.ts.map +1 -0
- package/dist/formats/index.js +51 -0
- package/dist/formats/index.js.map +1 -0
- package/dist/formats/typebox/date.d.ts +7 -0
- package/dist/formats/typebox/date.d.ts.map +1 -0
- package/dist/formats/typebox/date.js +47 -0
- package/dist/formats/typebox/date.js.map +1 -0
- package/dist/formats/typebox/date_time.d.ts +7 -0
- package/dist/formats/typebox/date_time.d.ts.map +1 -0
- package/dist/formats/typebox/date_time.js +39 -0
- package/dist/formats/typebox/date_time.js.map +1 -0
- package/dist/formats/typebox/duration.d.ts +6 -0
- package/dist/formats/typebox/duration.d.ts.map +1 -0
- package/dist/formats/typebox/duration.js +36 -0
- package/dist/formats/typebox/duration.js.map +1 -0
- package/dist/formats/typebox/email.d.ts +6 -0
- package/dist/formats/typebox/email.d.ts.map +1 -0
- package/dist/formats/typebox/email.js +36 -0
- package/dist/formats/typebox/email.js.map +1 -0
- package/dist/formats/typebox/hostname.d.ts +8 -0
- package/dist/formats/typebox/hostname.d.ts.map +1 -0
- package/dist/formats/typebox/hostname.js +38 -0
- package/dist/formats/typebox/hostname.js.map +1 -0
- package/dist/formats/typebox/idn_email.d.ts +6 -0
- package/dist/formats/typebox/idn_email.d.ts.map +1 -0
- package/dist/formats/typebox/idn_email.js +36 -0
- package/dist/formats/typebox/idn_email.js.map +1 -0
- package/dist/formats/typebox/idn_hostname.d.ts +9 -0
- package/dist/formats/typebox/idn_hostname.d.ts.map +1 -0
- package/dist/formats/typebox/idn_hostname.js +39 -0
- package/dist/formats/typebox/idn_hostname.js.map +1 -0
- package/dist/formats/typebox/idna/format/bidi.d.ts +4 -0
- package/dist/formats/typebox/idna/format/bidi.d.ts.map +1 -0
- package/dist/formats/typebox/idna/format/bidi.js +132 -0
- package/dist/formats/typebox/idna/format/bidi.js.map +1 -0
- package/dist/formats/typebox/idna/format/puny.d.ts +4 -0
- package/dist/formats/typebox/idna/format/puny.d.ts.map +1 -0
- package/dist/formats/typebox/idna/format/puny.js +172 -0
- package/dist/formats/typebox/idna/format/puny.js.map +1 -0
- package/dist/formats/typebox/idna/hostname.d.ts +2 -0
- package/dist/formats/typebox/idna/hostname.d.ts.map +1 -0
- package/dist/formats/typebox/idna/hostname.js +43 -0
- package/dist/formats/typebox/idna/hostname.js.map +1 -0
- package/dist/formats/typebox/idna/idn-hostname.d.ts +2 -0
- package/dist/formats/typebox/idna/idn-hostname.d.ts.map +1 -0
- package/dist/formats/typebox/idna/idn-hostname.js +71 -0
- package/dist/formats/typebox/idna/idn-hostname.js.map +1 -0
- package/dist/formats/typebox/idna/index.d.ts +3 -0
- package/dist/formats/typebox/idna/index.d.ts.map +1 -0
- package/dist/formats/typebox/idna/index.js +30 -0
- package/dist/formats/typebox/idna/index.js.map +1 -0
- package/dist/formats/typebox/idna/label/ascii.d.ts +2 -0
- package/dist/formats/typebox/idna/label/ascii.d.ts.map +1 -0
- package/dist/formats/typebox/idna/label/ascii.js +37 -0
- package/dist/formats/typebox/idna/label/ascii.js.map +1 -0
- package/dist/formats/typebox/idna/label/puny.d.ts +2 -0
- package/dist/formats/typebox/idna/label/puny.d.ts.map +1 -0
- package/dist/formats/typebox/idna/label/puny.js +56 -0
- package/dist/formats/typebox/idna/label/puny.js.map +1 -0
- package/dist/formats/typebox/idna/label/unicode.d.ts +2 -0
- package/dist/formats/typebox/idna/label/unicode.d.ts.map +1 -0
- package/dist/formats/typebox/idna/label/unicode.js +134 -0
- package/dist/formats/typebox/idna/label/unicode.js.map +1 -0
- package/dist/formats/typebox/idna/pattern/pattern.d.ts +31 -0
- package/dist/formats/typebox/idna/pattern/pattern.d.ts.map +1 -0
- package/dist/formats/typebox/idna/pattern/pattern.js +98 -0
- package/dist/formats/typebox/idna/pattern/pattern.js.map +1 -0
- package/dist/formats/typebox/ipv4.d.ts +6 -0
- package/dist/formats/typebox/ipv4.d.ts.map +1 -0
- package/dist/formats/typebox/ipv4.js +36 -0
- package/dist/formats/typebox/ipv4.js.map +1 -0
- package/dist/formats/typebox/ipv6.d.ts +6 -0
- package/dist/formats/typebox/ipv6.d.ts.map +1 -0
- package/dist/formats/typebox/ipv6.js +36 -0
- package/dist/formats/typebox/ipv6.js.map +1 -0
- package/dist/formats/typebox/iri.d.ts +6 -0
- package/dist/formats/typebox/iri.d.ts.map +1 -0
- package/dist/formats/typebox/iri.js +63 -0
- package/dist/formats/typebox/iri.js.map +1 -0
- package/dist/formats/typebox/iri_reference.d.ts +6 -0
- package/dist/formats/typebox/iri_reference.d.ts.map +1 -0
- package/dist/formats/typebox/iri_reference.js +40 -0
- package/dist/formats/typebox/iri_reference.js.map +1 -0
- package/dist/formats/typebox/json_pointer.d.ts +7 -0
- package/dist/formats/typebox/json_pointer.d.ts.map +1 -0
- package/dist/formats/typebox/json_pointer.js +37 -0
- package/dist/formats/typebox/json_pointer.js.map +1 -0
- package/dist/formats/typebox/json_pointer_uri_fragment.d.ts +7 -0
- package/dist/formats/typebox/json_pointer_uri_fragment.d.ts.map +1 -0
- package/dist/formats/typebox/json_pointer_uri_fragment.js +37 -0
- package/dist/formats/typebox/json_pointer_uri_fragment.js.map +1 -0
- package/dist/formats/typebox/regex.d.ts +6 -0
- package/dist/formats/typebox/regex.d.ts.map +1 -0
- package/dist/formats/typebox/regex.js +41 -0
- package/dist/formats/typebox/regex.js.map +1 -0
- package/dist/formats/typebox/relative_json_pointer.d.ts +7 -0
- package/dist/formats/typebox/relative_json_pointer.d.ts.map +1 -0
- package/dist/formats/typebox/relative_json_pointer.js +37 -0
- package/dist/formats/typebox/relative_json_pointer.js.map +1 -0
- package/dist/formats/typebox/time.d.ts +6 -0
- package/dist/formats/typebox/time.d.ts.map +1 -0
- package/dist/formats/typebox/time.js +60 -0
- package/dist/formats/typebox/time.js.map +1 -0
- package/dist/formats/typebox/uri.d.ts +6 -0
- package/dist/formats/typebox/uri.d.ts.map +1 -0
- package/dist/formats/typebox/uri.js +36 -0
- package/dist/formats/typebox/uri.js.map +1 -0
- package/dist/formats/typebox/uri_reference.d.ts +6 -0
- package/dist/formats/typebox/uri_reference.d.ts.map +1 -0
- package/dist/formats/typebox/uri_reference.js +36 -0
- package/dist/formats/typebox/uri_reference.js.map +1 -0
- package/dist/formats/typebox/uri_template.d.ts +6 -0
- package/dist/formats/typebox/uri_template.d.ts.map +1 -0
- package/dist/formats/typebox/uri_template.js +37 -0
- package/dist/formats/typebox/uri_template.js.map +1 -0
- package/dist/formats/typebox/url.d.ts +7 -0
- package/dist/formats/typebox/url.d.ts.map +1 -0
- package/dist/formats/typebox/url.js +36 -0
- package/dist/formats/typebox/url.js.map +1 -0
- package/dist/formats/typebox/uuid.d.ts +7 -0
- package/dist/formats/typebox/uuid.d.ts.map +1 -0
- package/dist/formats/typebox/uuid.js +37 -0
- package/dist/formats/typebox/uuid.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/resolver.d.ts +58 -0
- package/dist/resolver.d.ts.map +1 -0
- package/dist/resolver.js +209 -0
- package/dist/resolver.js.map +1 -0
- package/dist/shared.d.ts +9 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +33 -0
- package/dist/shared.js.map +1 -0
- package/dist/typebox-static.d.ts +48 -0
- package/dist/typebox-static.d.ts.map +1 -0
- package/dist/typebox-static.js +5 -0
- package/dist/typebox-static.js.map +1 -0
- package/dist/types.d.ts +48 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/validator.d.ts +39 -0
- package/dist/validator.d.ts.map +1 -0
- package/dist/validator.js +71 -0
- package/dist/validator.js.map +1 -0
- package/package.json +62 -5
- package/README.md +0 -3
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
TypeBox
|
|
2
|
+
|
|
3
|
+
The MIT License (MIT)
|
|
4
|
+
|
|
5
|
+
Copyright (c) 2017-2026 Haydn Paterson
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in
|
|
15
|
+
all copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
23
|
+
THE SOFTWARE.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { Check } from "./types.ts";
|
|
2
|
+
/** What a schema evaluated: property names (or all of them), the evaluated item prefix, items matched by `contains`. */
|
|
3
|
+
export interface Annotations {
|
|
4
|
+
props: Set<string> | null;
|
|
5
|
+
allProps: boolean;
|
|
6
|
+
items: number;
|
|
7
|
+
allItems: boolean;
|
|
8
|
+
indexes: Set<number> | null;
|
|
9
|
+
}
|
|
10
|
+
/** A check that records what it evaluated into `a` when it passes. */
|
|
11
|
+
export type AnnotatingCheck = (value: unknown, a: Annotations) => boolean;
|
|
12
|
+
/** Empty annotations. */
|
|
13
|
+
export declare function newAnnotations(): Annotations;
|
|
14
|
+
export declare const ANNOTATING_TRUE: AnnotatingCheck;
|
|
15
|
+
export declare const ANNOTATING_FALSE: AnnotatingCheck;
|
|
16
|
+
/** The indirection a recursive reference goes through. */
|
|
17
|
+
export declare function indirectAnnotating(slot: {
|
|
18
|
+
check: AnnotatingCheck | undefined;
|
|
19
|
+
}): AnnotatingCheck;
|
|
20
|
+
/** A plain check in annotating position: it evaluates nothing that unevaluated* sees. */
|
|
21
|
+
export declare function ignoreAnnotations(check: Check): AnnotatingCheck;
|
|
22
|
+
/** An annotating check run as a plain check, with fresh annotations. */
|
|
23
|
+
export declare function withFreshAnnotations(check: AnnotatingCheck): Check;
|
|
24
|
+
/** `check` with `uri` pushed onto the dynamic scope while it runs. */
|
|
25
|
+
export declare function scopedAnnotating(scope: string[], uri: string, check: AnnotatingCheck): AnnotatingCheck;
|
|
26
|
+
/** unevaluated* sees only the annotations of its own schema, never those of its siblings in the parent. */
|
|
27
|
+
export declare function isolateAnnotations(check: AnnotatingCheck): AnnotatingCheck;
|
|
28
|
+
/** The non-annotating keywords, then each annotating part in order. */
|
|
29
|
+
export declare function annotateAll(plain: Check, parts: readonly AnnotatingCheck[]): AnnotatingCheck;
|
|
30
|
+
/** `properties`, `patternProperties` and `additionalProperties`. */
|
|
31
|
+
export declare function annotateObject(props: readonly (readonly [string, Check])[], patterns: readonly (readonly [RegExp, Check])[], additional: Check | undefined): AnnotatingCheck;
|
|
32
|
+
/** `prefixItems`/`items`/`additionalItems` and `contains` with its bounds. */
|
|
33
|
+
export declare function annotateArray(prefix: readonly Check[], rest: Check | undefined, contains: Check | undefined, minContains: number, maxContains: number): AnnotatingCheck;
|
|
34
|
+
/** `anyOf` (or `oneOf` when `exactlyOne`): every passing branch contributes its annotations. */
|
|
35
|
+
export declare function annotateAny(branches: readonly AnnotatingCheck[], exactlyOne: boolean): AnnotatingCheck;
|
|
36
|
+
/** `if`, `then` and `else`: a passing `if` contributes its annotations even without `then`. */
|
|
37
|
+
export declare function annotateIf(cond: AnnotatingCheck, then: AnnotatingCheck | undefined, otherwise: AnnotatingCheck | undefined): AnnotatingCheck;
|
|
38
|
+
/** One `dependentSchemas` entry. */
|
|
39
|
+
export declare function annotateDependent(key: string, check: AnnotatingCheck): AnnotatingCheck;
|
|
40
|
+
/** `unevaluatedProperties`: every property no other keyword evaluated passes `check`. */
|
|
41
|
+
export declare function unevaluatedProperties(check: Check): AnnotatingCheck;
|
|
42
|
+
/** `unevaluatedItems`: every item no other keyword evaluated passes `check`. */
|
|
43
|
+
export declare function unevaluatedItems(check: Check): AnnotatingCheck;
|
|
44
|
+
//# sourceMappingURL=annotations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"annotations.d.ts","sourceRoot":"","sources":["../src/annotations.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAExC,wHAAwH;AACxH,MAAM,WAAW,WAA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AC,SAAS,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,EAAE,EAC/C,UAAU,EAAE,KAAK,GAAG,SAAS,GAC3B,eAAe,CA2BjB;AAED,8EAA8E;AAC9E,wBAAgB,aAAa,CAC5B,MAAM,EAAE,SAAS,KAAK,EAAE,EACxB,IAAI,EAAE,KAAK,GAAG,SAAS,EACvB,QAAQ,EAAE,KAAK,GAAG,SAAS,EAC3B,WAAW,EAAE,MAAM,EACnB,WAAW,EAAE,MAAM,GACjB,eAAe,CAsBjB;AAED,gGAAgG;AAChG,wBAAgB,WAAW,CAAC,QAAQ,EAAE,SAAS,eAAe,EAAE,EAAE,UAAU,EAAE,OAAO,GAAG,eAAe,CAYtG;AAED,+FAA+F;AAC/F,wBAAgB,UAAU,CACzB,IAAI,EAAE,eAAe,EACrB,IAAI,EAAE,eAAe,GAAG,SAAS,EACjC,SAAS,EAAE,eAAe,GAAG,SAAS,GACpC,eAAe,CASjB;AAED,oCAAoC;AACpC,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,eAAe,GAAG,eAAe,CAEtF;AAED,yFAAyF;AACzF,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,KAAK,GAAG,eAAe,CAOnE;AAED,gFAAgF;AAChF,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,KAAK,GAAG,eAAe,CAO9D"}
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
// Annotation tracking for `unevaluatedProperties` and `unevaluatedItems`: annotating checks record which
|
|
2
|
+
// properties and items a schema evaluated. Only schemas that use those keywords compile into these closures.
|
|
3
|
+
import { isObject } from "./checks.js";
|
|
4
|
+
/** Empty annotations. */
|
|
5
|
+
export function newAnnotations() {
|
|
6
|
+
return { props: null, allProps: false, items: 0, allItems: false, indexes: null };
|
|
7
|
+
}
|
|
8
|
+
function merge(a, b) {
|
|
9
|
+
if (b.props !== null)
|
|
10
|
+
for (const k of b.props)
|
|
11
|
+
addProp(a, k);
|
|
12
|
+
a.allProps ||= b.allProps;
|
|
13
|
+
if (b.items > a.items)
|
|
14
|
+
a.items = b.items;
|
|
15
|
+
a.allItems ||= b.allItems;
|
|
16
|
+
if (b.indexes !== null)
|
|
17
|
+
for (const i of b.indexes)
|
|
18
|
+
addIndex(a, i);
|
|
19
|
+
}
|
|
20
|
+
function addProp(a, key) {
|
|
21
|
+
if (a.props === null)
|
|
22
|
+
a.props = new Set();
|
|
23
|
+
a.props.add(key);
|
|
24
|
+
}
|
|
25
|
+
function addIndex(a, index) {
|
|
26
|
+
if (a.indexes === null)
|
|
27
|
+
a.indexes = new Set();
|
|
28
|
+
a.indexes.add(index);
|
|
29
|
+
}
|
|
30
|
+
export const ANNOTATING_TRUE = () => true;
|
|
31
|
+
export const ANNOTATING_FALSE = () => false;
|
|
32
|
+
/** The indirection a recursive reference goes through. */
|
|
33
|
+
export function indirectAnnotating(slot) {
|
|
34
|
+
return (v, a) => slot.check(v, a);
|
|
35
|
+
}
|
|
36
|
+
/** A plain check in annotating position: it evaluates nothing that unevaluated* sees. */
|
|
37
|
+
export function ignoreAnnotations(check) {
|
|
38
|
+
return (v) => check(v);
|
|
39
|
+
}
|
|
40
|
+
/** An annotating check run as a plain check, with fresh annotations. */
|
|
41
|
+
export function withFreshAnnotations(check) {
|
|
42
|
+
return (v) => check(v, newAnnotations());
|
|
43
|
+
}
|
|
44
|
+
/** `check` with `uri` pushed onto the dynamic scope while it runs. */
|
|
45
|
+
export function scopedAnnotating(scope, uri, check) {
|
|
46
|
+
return (v, a) => {
|
|
47
|
+
scope.push(uri);
|
|
48
|
+
const ok = check(v, a);
|
|
49
|
+
scope.pop();
|
|
50
|
+
return ok;
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/** unevaluated* sees only the annotations of its own schema, never those of its siblings in the parent. */
|
|
54
|
+
export function isolateAnnotations(check) {
|
|
55
|
+
return (v, a) => {
|
|
56
|
+
const b = newAnnotations();
|
|
57
|
+
if (!check(v, b))
|
|
58
|
+
return false;
|
|
59
|
+
merge(a, b);
|
|
60
|
+
return true;
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** The non-annotating keywords, then each annotating part in order. */
|
|
64
|
+
export function annotateAll(plain, parts) {
|
|
65
|
+
return (v, a) => {
|
|
66
|
+
if (!plain(v))
|
|
67
|
+
return false;
|
|
68
|
+
for (let i = 0; i < parts.length; i++)
|
|
69
|
+
if (!parts[i](v, a))
|
|
70
|
+
return false;
|
|
71
|
+
return true;
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/** `properties`, `patternProperties` and `additionalProperties`. */
|
|
75
|
+
export function annotateObject(props, patterns, additional) {
|
|
76
|
+
const declared = new Set(props.map(([k]) => k));
|
|
77
|
+
return (v, a) => {
|
|
78
|
+
if (!isObject(v))
|
|
79
|
+
return true;
|
|
80
|
+
const seen = [];
|
|
81
|
+
for (const [k, check] of props) {
|
|
82
|
+
if (!Object.hasOwn(v, k))
|
|
83
|
+
continue;
|
|
84
|
+
if (!check(v[k]))
|
|
85
|
+
return false;
|
|
86
|
+
seen.push(k);
|
|
87
|
+
}
|
|
88
|
+
for (const k of Object.keys(v)) {
|
|
89
|
+
let matched = declared.has(k);
|
|
90
|
+
for (const [re, check] of patterns) {
|
|
91
|
+
if (re.test(k)) {
|
|
92
|
+
if (!check(v[k]))
|
|
93
|
+
return false;
|
|
94
|
+
matched = true;
|
|
95
|
+
seen.push(k);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (!matched && additional !== undefined) {
|
|
99
|
+
if (!additional(v[k]))
|
|
100
|
+
return false;
|
|
101
|
+
seen.push(k);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
for (const k of seen)
|
|
105
|
+
addProp(a, k);
|
|
106
|
+
return true;
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/** `prefixItems`/`items`/`additionalItems` and `contains` with its bounds. */
|
|
110
|
+
export function annotateArray(prefix, rest, contains, minContains, maxContains) {
|
|
111
|
+
return (v, a) => {
|
|
112
|
+
if (!Array.isArray(v))
|
|
113
|
+
return true;
|
|
114
|
+
const m = Math.min(v.length, prefix.length);
|
|
115
|
+
for (let i = 0; i < m; i++)
|
|
116
|
+
if (!prefix[i](v[i]))
|
|
117
|
+
return false;
|
|
118
|
+
if (rest !== undefined) {
|
|
119
|
+
for (let i = prefix.length; i < v.length; i++)
|
|
120
|
+
if (!rest(v[i]))
|
|
121
|
+
return false;
|
|
122
|
+
a.allItems = true;
|
|
123
|
+
}
|
|
124
|
+
if (m > a.items)
|
|
125
|
+
a.items = m;
|
|
126
|
+
if (contains !== undefined) {
|
|
127
|
+
let n = 0;
|
|
128
|
+
for (let i = 0; i < v.length; i++) {
|
|
129
|
+
if (contains(v[i])) {
|
|
130
|
+
n++;
|
|
131
|
+
addIndex(a, i);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
if (n < minContains || n > maxContains)
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
return true;
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
/** `anyOf` (or `oneOf` when `exactlyOne`): every passing branch contributes its annotations. */
|
|
141
|
+
export function annotateAny(branches, exactlyOne) {
|
|
142
|
+
return (v, a) => {
|
|
143
|
+
let n = 0;
|
|
144
|
+
for (const branch of branches) {
|
|
145
|
+
const b = newAnnotations();
|
|
146
|
+
if (branch(v, b)) {
|
|
147
|
+
n++;
|
|
148
|
+
merge(a, b);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return exactlyOne ? n === 1 : n > 0;
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/** `if`, `then` and `else`: a passing `if` contributes its annotations even without `then`. */
|
|
155
|
+
export function annotateIf(cond, then, otherwise) {
|
|
156
|
+
return (v, a) => {
|
|
157
|
+
const b = newAnnotations();
|
|
158
|
+
if (cond(v, b)) {
|
|
159
|
+
merge(a, b);
|
|
160
|
+
return then === undefined || then(v, a);
|
|
161
|
+
}
|
|
162
|
+
return otherwise === undefined || otherwise(v, a);
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
/** One `dependentSchemas` entry. */
|
|
166
|
+
export function annotateDependent(key, check) {
|
|
167
|
+
return (v, a) => !isObject(v) || !Object.hasOwn(v, key) || check(v, a);
|
|
168
|
+
}
|
|
169
|
+
/** `unevaluatedProperties`: every property no other keyword evaluated passes `check`. */
|
|
170
|
+
export function unevaluatedProperties(check) {
|
|
171
|
+
return (v, a) => {
|
|
172
|
+
if (!isObject(v) || a.allProps)
|
|
173
|
+
return true;
|
|
174
|
+
for (const k of Object.keys(v))
|
|
175
|
+
if (!(a.props?.has(k) ?? false) && !check(v[k]))
|
|
176
|
+
return false;
|
|
177
|
+
a.allProps = true;
|
|
178
|
+
return true;
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
/** `unevaluatedItems`: every item no other keyword evaluated passes `check`. */
|
|
182
|
+
export function unevaluatedItems(check) {
|
|
183
|
+
return (v, a) => {
|
|
184
|
+
if (!Array.isArray(v) || a.allItems)
|
|
185
|
+
return true;
|
|
186
|
+
for (let i = a.items; i < v.length; i++)
|
|
187
|
+
if (!(a.indexes?.has(i) ?? false) && !check(v[i]))
|
|
188
|
+
return false;
|
|
189
|
+
a.allItems = true;
|
|
190
|
+
return true;
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
//# sourceMappingURL=annotations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"annotations.js","sourceRoot":"","sources":["../src/annotations.ts"],"names":[],"mappings":"AAAA,yGAAyG;AACzG,6GAA6G;AAE7G,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAevC,yBAAyB;AACzB,MAAM,UAAU,cAAc;IAC7B,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE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AC5C,OAAO,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;QACf,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,QAAQ;YAAE,OAAO,IAAI,CAAC;QACjD,KAAK,IAAI,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE;YAAE,IAAI,CAAC,CAAC,CAAC,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;gBAAE,OAAO,KAAK,CAAC;QACzG,CAAC,CAAC,QAAQ,GAAG,IAAI,CAAC;QAClB,OAAO,IAAI,CAAC;IACb,CAAC,CAAC;AACH,CAAC","sourcesContent":["// Annotation tracking for `unevaluatedProperties` and `unevaluatedItems`: annotating checks record which\n// properties and items a schema evaluated. Only schemas that use those keywords compile into these closures.\n\nimport { isObject } from \"./checks.ts\";\nimport type { Check } from \"./types.ts\";\n\n/** What a schema evaluated: property names (or all of them), the evaluated item prefix, items matched by `contains`. */\nexport interface Annotations {\n\tprops: Set<string> | null;\n\tallProps: boolean;\n\titems: number;\n\tallItems: boolean;\n\tindexes: Set<number> | null;\n}\n\n/** A check that records what it evaluated into `a` when it passes. */\nexport type AnnotatingCheck = (value: unknown, a: Annotations) => boolean;\n\n/** Empty annotations. */\nexport function newAnnotations(): Annotations {\n\treturn { props: null, allProps: false, items: 0, allItems: false, indexes: null };\n}\n\nfunction merge(a: Annotations, b: Annotations): void {\n\tif (b.props !== null) for (const k of b.props) addProp(a, k);\n\ta.allProps ||= b.allProps;\n\tif (b.items > a.items) a.items = b.items;\n\ta.allItems ||= b.allItems;\n\tif (b.indexes !== null) for (const i of b.indexes) addIndex(a, i);\n}\n\nfunction addProp(a: Annotations, key: string): void {\n\tif (a.props === null) a.props = new Set();\n\ta.props.add(key);\n}\n\nfunction addIndex(a: Annotations, index: number): void {\n\tif (a.indexes === null) a.indexes = new Set();\n\ta.indexes.add(index);\n}\n\nexport const ANNOTATING_TRUE: AnnotatingCheck = () => true;\nexport const ANNOTATING_FALSE: AnnotatingCheck = () => false;\n\n/** The indirection a recursive reference goes through. */\nexport function indirectAnnotating(slot: { check: AnnotatingCheck | undefined }): AnnotatingCheck {\n\treturn (v, a) => (slot.check as AnnotatingCheck)(v, a);\n}\n\n/** A plain check in annotating position: it evaluates nothing that unevaluated* sees. */\nexport function ignoreAnnotations(check: Check): AnnotatingCheck {\n\treturn (v) => check(v);\n}\n\n/** An annotating check run as a plain check, with fresh annotations. */\nexport function withFreshAnnotations(check: AnnotatingCheck): Check {\n\treturn (v) => check(v, newAnnotations());\n}\n\n/** `check` with `uri` pushed onto the dynamic scope while it runs. */\nexport function scopedAnnotating(scope: string[], uri: string, check: AnnotatingCheck): AnnotatingCheck {\n\treturn (v, a) => {\n\t\tscope.push(uri);\n\t\tconst ok = check(v, a);\n\t\tscope.pop();\n\t\treturn ok;\n\t};\n}\n\n/** unevaluated* sees only the annotations of its own schema, never those of its siblings in the parent. */\nexport function isolateAnnotations(check: AnnotatingCheck): AnnotatingCheck {\n\treturn (v, a) => {\n\t\tconst b = newAnnotations();\n\t\tif (!check(v, b)) return false;\n\t\tmerge(a, b);\n\t\treturn true;\n\t};\n}\n\n/** The non-annotating keywords, then each annotating part in order. */\nexport function annotateAll(plain: Check, parts: readonly AnnotatingCheck[]): AnnotatingCheck {\n\treturn (v, a) => {\n\t\tif (!plain(v)) return false;\n\t\tfor (let i = 0; i < parts.length; i++) if (!parts[i](v, a)) return false;\n\t\treturn true;\n\t};\n}\n\n/** `properties`, `patternProperties` and `additionalProperties`. */\nexport function annotateObject(\n\tprops: readonly (readonly [string, Check])[],\n\tpatterns: readonly (readonly [RegExp, Check])[],\n\tadditional: Check | undefined,\n): AnnotatingCheck {\n\tconst declared = new Set(props.map(([k]) => k));\n\treturn (v, a) => {\n\t\tif (!isObject(v)) return true;\n\t\tconst seen: string[] = [];\n\t\tfor (const [k, check] of props) {\n\t\t\tif (!Object.hasOwn(v, k)) continue;\n\t\t\tif (!check(v[k])) return false;\n\t\t\tseen.push(k);\n\t\t}\n\t\tfor (const k of Object.keys(v)) {\n\t\t\tlet matched = declared.has(k);\n\t\t\tfor (const [re, check] of patterns) {\n\t\t\t\tif (re.test(k)) {\n\t\t\t\t\tif (!check(v[k])) return false;\n\t\t\t\t\tmatched = true;\n\t\t\t\t\tseen.push(k);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (!matched && additional !== undefined) {\n\t\t\t\tif (!additional(v[k])) return false;\n\t\t\t\tseen.push(k);\n\t\t\t}\n\t\t}\n\t\tfor (const k of seen) addProp(a, k);\n\t\treturn true;\n\t};\n}\n\n/** `prefixItems`/`items`/`additionalItems` and `contains` with its bounds. */\nexport function annotateArray(\n\tprefix: readonly Check[],\n\trest: Check | undefined,\n\tcontains: Check | undefined,\n\tminContains: number,\n\tmaxContains: number,\n): AnnotatingCheck {\n\treturn (v, a) => {\n\t\tif (!Array.isArray(v)) return true;\n\t\tconst m = Math.min(v.length, prefix.length);\n\t\tfor (let i = 0; i < m; i++) if (!prefix[i](v[i])) return false;\n\t\tif (rest !== undefined) {\n\t\t\tfor (let i = prefix.length; i < v.length; i++) if (!rest(v[i])) return false;\n\t\t\ta.allItems = true;\n\t\t}\n\t\tif (m > a.items) a.items = m;\n\t\tif (contains !== undefined) {\n\t\t\tlet n = 0;\n\t\t\tfor (let i = 0; i < v.length; i++) {\n\t\t\t\tif (contains(v[i])) {\n\t\t\t\t\tn++;\n\t\t\t\t\taddIndex(a, i);\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (n < minContains || n > maxContains) return false;\n\t\t}\n\t\treturn true;\n\t};\n}\n\n/** `anyOf` (or `oneOf` when `exactlyOne`): every passing branch contributes its annotations. */\nexport function annotateAny(branches: readonly AnnotatingCheck[], exactlyOne: boolean): AnnotatingCheck {\n\treturn (v, a) => {\n\t\tlet n = 0;\n\t\tfor (const branch of branches) {\n\t\t\tconst b = newAnnotations();\n\t\t\tif (branch(v, b)) {\n\t\t\t\tn++;\n\t\t\t\tmerge(a, b);\n\t\t\t}\n\t\t}\n\t\treturn exactlyOne ? n === 1 : n > 0;\n\t};\n}\n\n/** `if`, `then` and `else`: a passing `if` contributes its annotations even without `then`. */\nexport function annotateIf(\n\tcond: AnnotatingCheck,\n\tthen: AnnotatingCheck | undefined,\n\totherwise: AnnotatingCheck | undefined,\n): AnnotatingCheck {\n\treturn (v, a) => {\n\t\tconst b = newAnnotations();\n\t\tif (cond(v, b)) {\n\t\t\tmerge(a, b);\n\t\t\treturn then === undefined || then(v, a);\n\t\t}\n\t\treturn otherwise === undefined || otherwise(v, a);\n\t};\n}\n\n/** One `dependentSchemas` entry. */\nexport function annotateDependent(key: string, check: AnnotatingCheck): AnnotatingCheck {\n\treturn (v, a) => !isObject(v) || !Object.hasOwn(v, key) || check(v, a);\n}\n\n/** `unevaluatedProperties`: every property no other keyword evaluated passes `check`. */\nexport function unevaluatedProperties(check: Check): AnnotatingCheck {\n\treturn (v, a) => {\n\t\tif (!isObject(v) || a.allProps) return true;\n\t\tfor (const k of Object.keys(v)) if (!(a.props?.has(k) ?? false) && !check(v[k])) return false;\n\t\ta.allProps = true;\n\t\treturn true;\n\t};\n}\n\n/** `unevaluatedItems`: every item no other keyword evaluated passes `check`. */\nexport function unevaluatedItems(check: Check): AnnotatingCheck {\n\treturn (v, a) => {\n\t\tif (!Array.isArray(v) || a.allItems) return true;\n\t\tfor (let i = a.items; i < v.length; i++) if (!(a.indexes?.has(i) ?? false) && !check(v[i])) return false;\n\t\ta.allItems = true;\n\t\treturn true;\n\t};\n}\n"]}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import type { TypeBoxStatic } from "./typebox-static.ts";
|
|
2
|
+
declare const STATIC: unique symbol;
|
|
3
|
+
declare const OPTIONAL_TYPE: unique symbol;
|
|
4
|
+
/** A JSON Schema object whose valid values have type `T` (a type-level brand; nothing extra exists at run time). */
|
|
5
|
+
export type Schema<T = unknown> = {
|
|
6
|
+
readonly [keyword: string]: unknown;
|
|
7
|
+
} & {
|
|
8
|
+
readonly [STATIC]?: T;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* The type of the values `S` accepts: the type a builder recorded; for a TypeBox schema (`'~kind'` or `'~unsafe'`),
|
|
12
|
+
* TypeBox's static type for the common kinds (see `TypeBoxStatic`); else `unknown`.
|
|
13
|
+
*/
|
|
14
|
+
export type Static<S> = S extends {
|
|
15
|
+
readonly "~kind": unknown;
|
|
16
|
+
} | {
|
|
17
|
+
readonly "~unsafe": unknown;
|
|
18
|
+
} ? TypeBoxStatic<S> : S extends {
|
|
19
|
+
readonly [STATIC]?: infer T;
|
|
20
|
+
} ? unknown extends T ? unknown : T : unknown;
|
|
21
|
+
/** Annotations every builder accepts: they describe the schema and never change what it accepts. */
|
|
22
|
+
export interface Annotations<T = unknown> {
|
|
23
|
+
readonly title?: string;
|
|
24
|
+
readonly description?: string;
|
|
25
|
+
readonly examples?: readonly T[];
|
|
26
|
+
readonly deprecated?: boolean;
|
|
27
|
+
/** A default value, an annotation for tools and forms; validation never inserts it. */
|
|
28
|
+
readonly default?: T;
|
|
29
|
+
}
|
|
30
|
+
/** Length bounds of a string, an array or bytes. */
|
|
31
|
+
export interface LengthBounds {
|
|
32
|
+
readonly minLength?: number;
|
|
33
|
+
readonly maxLength?: number;
|
|
34
|
+
}
|
|
35
|
+
/** A string, with optional length bounds (in code points), pattern and format. */
|
|
36
|
+
export declare function str(options?: Annotations<string> & LengthBounds & {
|
|
37
|
+
readonly pattern?: string;
|
|
38
|
+
readonly format?: string;
|
|
39
|
+
}): Schema<string>;
|
|
40
|
+
/** Bounds of a number. */
|
|
41
|
+
export interface NumberBounds {
|
|
42
|
+
readonly minimum?: number;
|
|
43
|
+
readonly maximum?: number;
|
|
44
|
+
readonly exclusiveMinimum?: number;
|
|
45
|
+
readonly exclusiveMaximum?: number;
|
|
46
|
+
readonly multipleOf?: number;
|
|
47
|
+
}
|
|
48
|
+
/** A number within optional bounds. */
|
|
49
|
+
export declare function num(options?: Annotations<number> & NumberBounds): Schema<number>;
|
|
50
|
+
/** An integer within optional bounds. */
|
|
51
|
+
export declare function int(options?: Annotations<number> & NumberBounds): Schema<number>;
|
|
52
|
+
/** A boolean. */
|
|
53
|
+
export declare function bool(options?: Annotations<boolean>): Schema<boolean>;
|
|
54
|
+
/** `null`. */
|
|
55
|
+
export declare function nul(options?: Annotations<null>): Schema<null>;
|
|
56
|
+
/** Any value. */
|
|
57
|
+
export declare function unknown(options?: Annotations): Schema<unknown>;
|
|
58
|
+
/** Exactly `value` (a string, number, boolean or null). */
|
|
59
|
+
export declare function lit<const V extends string | number | boolean | null>(value: V, options?: Annotations<V>): Schema<V>;
|
|
60
|
+
/** One of the given values. */
|
|
61
|
+
export declare function oneOf<const V extends readonly (string | number | boolean | null)[]>(values: V, options?: Annotations<V[number]>): Schema<V[number]>;
|
|
62
|
+
/**
|
|
63
|
+
* Any of the given schemas. A union of objects tagged by a `lit()` property (`{ kind: lit("text"), ... }`) is checked
|
|
64
|
+
* by the tag: only the branch it selects runs, and its errors are the ones reported.
|
|
65
|
+
*/
|
|
66
|
+
export declare function union<const S extends readonly Schema[]>(schemas: S, options?: Annotations<Static<S[number]>>): Schema<Static<S[number]>>;
|
|
67
|
+
/** `schema` or `null`. */
|
|
68
|
+
export declare function nullable<T>(schema: Schema<T>, options?: Annotations<T | null>): Schema<T | null>;
|
|
69
|
+
/** An array of `items`, with optional length bounds and unique items. */
|
|
70
|
+
export declare function arr<T>(items: Schema<T>, options?: Annotations<T[]> & {
|
|
71
|
+
readonly minItems?: number;
|
|
72
|
+
readonly maxItems?: number;
|
|
73
|
+
readonly uniqueItems?: boolean;
|
|
74
|
+
}): Schema<T[]>;
|
|
75
|
+
/** A fixed-length array whose items match `items` in order. */
|
|
76
|
+
export declare function tuple<const S extends readonly Schema[]>(items: S, options?: Annotations<{
|
|
77
|
+
-readonly [K in keyof S]: Static<S[K]>;
|
|
78
|
+
}>): Schema<{
|
|
79
|
+
-readonly [K in keyof S]: Static<S[K]>;
|
|
80
|
+
}>;
|
|
81
|
+
/** An object with string keys and values of `value` (a record). */
|
|
82
|
+
export declare function map<T>(value: Schema<T>, options?: Annotations<Record<string, T>>): Schema<Record<string, T>>;
|
|
83
|
+
/** A Uint8Array with optional length bounds (an extension of JSON Schema for values that never pass through JSON). */
|
|
84
|
+
export declare function bytes(options?: Annotations<Uint8Array> & LengthBounds): Schema<Uint8Array>;
|
|
85
|
+
/** An optional property of obj(): `{ name: opt(str()) }` may omit `name` or set it to undefined. */
|
|
86
|
+
export declare class Optional<S extends Schema = Schema> {
|
|
87
|
+
readonly [OPTIONAL_TYPE]?: S;
|
|
88
|
+
readonly schema: S;
|
|
89
|
+
constructor(schema: S);
|
|
90
|
+
}
|
|
91
|
+
/** Marks an obj() property as optional. */
|
|
92
|
+
export declare function opt<S extends Schema>(schema: S): Optional<S>;
|
|
93
|
+
/** The properties of obj(): a schema or an opt() schema per name. */
|
|
94
|
+
export type Properties = {
|
|
95
|
+
readonly [name: string]: Schema | Optional;
|
|
96
|
+
};
|
|
97
|
+
/** The value type of an obj() schema. */
|
|
98
|
+
export type StaticObject<P extends Properties> = Simplify<{
|
|
99
|
+
-readonly [K in keyof P as P[K] extends Optional ? never : K]: Static<P[K]>;
|
|
100
|
+
} & {
|
|
101
|
+
-readonly [K in keyof P as P[K] extends Optional ? K : never]?: P[K] extends Optional<infer S> ? Static<S> : never;
|
|
102
|
+
}>;
|
|
103
|
+
type Simplify<T> = {
|
|
104
|
+
[K in keyof T]: T[K];
|
|
105
|
+
} & {};
|
|
106
|
+
/**
|
|
107
|
+
* A closed object: the given properties (required unless opt()) and no others. `open: true` allows other
|
|
108
|
+
* properties, as tool schemas from other systems often do.
|
|
109
|
+
*/
|
|
110
|
+
export declare function obj<const P extends Properties>(properties: P, options?: Annotations<StaticObject<P>> & {
|
|
111
|
+
readonly open?: boolean;
|
|
112
|
+
}): Schema<StaticObject<P>>;
|
|
113
|
+
export {};
|
|
114
|
+
//# sourceMappingURL=builders.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builders.d.ts","sourceRoot":"","sources":["../src/builders.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD,OAAO,CAAC,MAAM,MAAM,EAAE,OAAO,MAAM,CAAC;AACpC,OAAO,CAAC,MAAM,aAAa,EAAE,OAAO,MAAM,CAAC;AAE3C,oHAAoH;AACpH,MAAM,MAAM,MAAM,CAAC,CAAC,GAAG,OAAO,IAAI;IAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAA;CAAE,GAAG;IAAE,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAA;CAAE,CAAC;AAItG;;;GAGG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,IAAI,CAAC,SAAS;IAAE,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAA;CAAE,GAAG;IAAE,QAAQ,CAAC,SAAS,EAAE,OAAO,CAAA;CAAE,GAC9F,aAAa,CAAC,CAAC,CAAC,GAChB,CAAC,SAAS;IAAE,QAAQ,CAAC,CAAC,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,CAAA;CAAE,GACxC,OAAO,SAAS,CAAC,GAChB,OAAO,GACP,CAAC,GACF,OAAO,CAAC;AAEZ,oGAAoG;AACpG,MAAM,WAAW,WAAW,CAAC,CAAC,GAAG,OAAO;IACvC,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,QAAQ,CAAC,EAAE,SAAS,CAAC,EAAE,CAAC;IACjC,QAAQ,CAAC,UAAU,CAAC,EAAE,OAAO,CAAC;IAC9B,uFAA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AAE,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,QAAQ,CAAA;CAAE,CAAC;AAExE,yCAAyC;AACzC,MAAM,MAAM,YAAY,CAAC,CAAC,SAAS,UAAU,IAAI,QAAQ,CACxD;IAAE,CAAC,UAAU,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,QAAQ,GAAG,KAAK,GAAG,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;CAAE,GAAG;IACjF,CAAC,UAAU,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,QAAQ,GAAG,CAAC,GAAG,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,QAAQ,CAAC,MAAM,CAAC,CAAC,GAC3F,MAAM,CAAC,CAAC,CAAC,GACT,KAAK;CACR,CACD,CAAC;AAEF,KAAK,QAAQ,CAAC,CAAC,IAAI;KAAG,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAAE,GAAG,EAAE,CAAC;AAEjD;;;GAGG;AACH,wBAAgB,GAAG,CAAC,KAAK,CAAC,CAAC,SAAS,UAAU,EAC7C,UAAU,EAAE,CAAC,EACb,OAAO,GAAE,WAAW,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,GAAG;IAAE,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAA;CAAO,GACtE,MAAM,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAkBzB"}
|
package/dist/builders.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// Schema builders: short functions that return plain JSON Schema objects and carry the static type of the values
|
|
2
|
+
// they accept, so `Static<typeof schema>` is the TypeScript type a valid value has. The objects are ordinary data:
|
|
3
|
+
// they serialize with JSON.stringify and compile with compile() like any other schema.
|
|
4
|
+
/** A string, with optional length bounds (in code points), pattern and format. */
|
|
5
|
+
export function str(options = {}) {
|
|
6
|
+
return { type: "string", ...options };
|
|
7
|
+
}
|
|
8
|
+
/** A number within optional bounds. */
|
|
9
|
+
export function num(options = {}) {
|
|
10
|
+
return { type: "number", ...options };
|
|
11
|
+
}
|
|
12
|
+
/** An integer within optional bounds. */
|
|
13
|
+
export function int(options = {}) {
|
|
14
|
+
return { type: "integer", ...options };
|
|
15
|
+
}
|
|
16
|
+
/** A boolean. */
|
|
17
|
+
export function bool(options = {}) {
|
|
18
|
+
return { type: "boolean", ...options };
|
|
19
|
+
}
|
|
20
|
+
/** `null`. */
|
|
21
|
+
export function nul(options = {}) {
|
|
22
|
+
return { type: "null", ...options };
|
|
23
|
+
}
|
|
24
|
+
/** Any value. */
|
|
25
|
+
export function unknown(options = {}) {
|
|
26
|
+
return { ...options };
|
|
27
|
+
}
|
|
28
|
+
/** Exactly `value` (a string, number, boolean or null). */
|
|
29
|
+
export function lit(value, options = {}) {
|
|
30
|
+
return { const: value, ...options };
|
|
31
|
+
}
|
|
32
|
+
/** One of the given values. */
|
|
33
|
+
export function oneOf(values, options = {}) {
|
|
34
|
+
return { enum: values, ...options };
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Any of the given schemas. A union of objects tagged by a `lit()` property (`{ kind: lit("text"), ... }`) is checked
|
|
38
|
+
* by the tag: only the branch it selects runs, and its errors are the ones reported.
|
|
39
|
+
*/
|
|
40
|
+
export function union(schemas, options = {}) {
|
|
41
|
+
return { anyOf: schemas, ...options };
|
|
42
|
+
}
|
|
43
|
+
/** `schema` or `null`. */
|
|
44
|
+
export function nullable(schema, options = {}) {
|
|
45
|
+
return { anyOf: [schema, { type: "null" }], ...options };
|
|
46
|
+
}
|
|
47
|
+
/** An array of `items`, with optional length bounds and unique items. */
|
|
48
|
+
export function arr(items, options = {}) {
|
|
49
|
+
return { type: "array", items, ...options };
|
|
50
|
+
}
|
|
51
|
+
/** A fixed-length array whose items match `items` in order. */
|
|
52
|
+
export function tuple(items, options = {}) {
|
|
53
|
+
return { type: "array", prefixItems: items, items: false, minItems: items.length, ...options };
|
|
54
|
+
}
|
|
55
|
+
/** An object with string keys and values of `value` (a record). */
|
|
56
|
+
export function map(value, options = {}) {
|
|
57
|
+
return { type: "object", additionalProperties: value, ...options };
|
|
58
|
+
}
|
|
59
|
+
/** A Uint8Array with optional length bounds (an extension of JSON Schema for values that never pass through JSON). */
|
|
60
|
+
export function bytes(options = {}) {
|
|
61
|
+
return { type: "bytes", ...options };
|
|
62
|
+
}
|
|
63
|
+
/** An optional property of obj(): `{ name: opt(str()) }` may omit `name` or set it to undefined. */
|
|
64
|
+
export class Optional {
|
|
65
|
+
schema;
|
|
66
|
+
constructor(schema) {
|
|
67
|
+
this.schema = schema;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/** Marks an obj() property as optional. */
|
|
71
|
+
export function opt(schema) {
|
|
72
|
+
return new Optional(schema);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* A closed object: the given properties (required unless opt()) and no others. `open: true` allows other
|
|
76
|
+
* properties, as tool schemas from other systems often do.
|
|
77
|
+
*/
|
|
78
|
+
export function obj(properties, options = {}) {
|
|
79
|
+
const { open, ...annotations } = options;
|
|
80
|
+
const props = {};
|
|
81
|
+
const required = [];
|
|
82
|
+
for (const [name, property] of Object.entries(properties)) {
|
|
83
|
+
if (property instanceof Optional)
|
|
84
|
+
props[name] = property.schema;
|
|
85
|
+
else {
|
|
86
|
+
props[name] = property;
|
|
87
|
+
required.push(name);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
type: "object",
|
|
92
|
+
properties: props,
|
|
93
|
+
...(required.length > 0 ? { required } : {}),
|
|
94
|
+
...(open === true ? {} : { additionalProperties: false }),
|
|
95
|
+
...annotations,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=builders.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builders.js","sourceRoot":"","sources":["../src/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,MAAS;IAC9C,OAAO,IAAI,QAAQ,CAAC,MAAM,CAAC,CAAC;AAC7B,CAAC;AAgBD;;;GAGG;AACH,MAAM,UAAU,GAAG,CAClB,UAAa,EACb,OAAO,GAA+D,EAAE;IAExE,MAAM,EAAE,IAAI,EAAE,GAAG,WAAW,EAAE,GAAG,OAAO,CAAC;IACzC,MAAM,KAAK,GAA2B,EAAE,CAAC;IACzC,MAAM,QAAQ,GAAa,EAAE,CAAC;IAC9B,KAAK,MAAM,CAAC,IAAI,EAAE,QAAQ,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC;QAC3D,IAAI,QAAQ,YAAY,QAAQ;YAAE,KAAK,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,MAAM,CAAC;aAC3D,CAAC;YACL,KAAK,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC;YACvB,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACrB,CAAC;IACF,CAAC;IACD,OAAO;QACN,IAAI,EAAE,QAAQ;QACd,UAAU,EAAE,KAAK;QACjB,GAAG,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC5C,GAAG,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,oBAAoB,EAAE,KAAK,EAAE,CAAC;QACzD,GAAG,WAAW;KACd,CAAC;AACH,CAAC","sourcesContent":["// Schema builders: short functions that return plain JSON Schema objects and carry the static type of the values\n// they accept, so `Static<typeof schema>` is the TypeScript type a valid value has. The objects are ordinary data:\n// they serialize with JSON.stringify and compile with compile() like any other schema.\n\nimport type { TypeBoxStatic } from \"./typebox-static.ts\";\n\ndeclare const STATIC: unique symbol;\ndeclare const OPTIONAL_TYPE: unique symbol;\n\n/** A JSON Schema object whose valid values have type `T` (a type-level brand; nothing extra exists at run time). */\nexport type Schema<T = unknown> = { readonly [keyword: string]: unknown } & { readonly [STATIC]?: T };\n\n// TypeBox is tested first and in its own branch: a builder brand's fallback through TypeBoxStatic would leave\n// `Static<S>` of a generic `S` (constrained to builder schemas) unresolvable to its constraint's types.\n/**\n * The type of the values `S` accepts: the type a builder recorded; for a TypeBox schema (`'~kind'` or `'~unsafe'`),\n * TypeBox's static type for the common kinds (see `TypeBoxStatic`); else `unknown`.\n */\nexport type Static<S> = S extends { readonly \"~kind\": unknown } | { readonly \"~unsafe\": unknown }\n\t? TypeBoxStatic<S>\n\t: S extends { readonly [STATIC]?: infer T }\n\t\t? unknown extends T\n\t\t\t? unknown\n\t\t\t: T\n\t\t: unknown;\n\n/** Annotations every builder accepts: they describe the schema and never change what it accepts. */\nexport interface Annotations<T = unknown> {\n\treadonly title?: string;\n\treadonly description?: string;\n\treadonly examples?: readonly T[];\n\treadonly deprecated?: boolean;\n\t/** A default value, an annotation for tools and forms; validation never inserts it. */\n\treadonly default?: T;\n}\n\n/** Length bounds of a string, an array or bytes. */\nexport interface LengthBounds {\n\treadonly minLength?: number;\n\treadonly maxLength?: number;\n}\n\n/** A string, with optional length bounds (in code points), pattern and format. */\nexport function str(\n\toptions: Annotations<string> & LengthBounds & { readonly pattern?: string; readonly format?: string } = {},\n): Schema<string> {\n\treturn { type: \"string\", ...options };\n}\n\n/** Bounds of a number. */\nexport interface NumberBounds {\n\treadonly minimum?: number;\n\treadonly maximum?: number;\n\treadonly exclusiveMinimum?: number;\n\treadonly exclusiveMaximum?: number;\n\treadonly multipleOf?: number;\n}\n\n/** A number within optional bounds. */\nexport function num(options: Annotations<number> & NumberBounds = {}): Schema<number> {\n\treturn { type: \"number\", ...options };\n}\n\n/** An integer within optional bounds. */\nexport function int(options: Annotations<number> & NumberBounds = {}): Schema<number> {\n\treturn { type: \"integer\", ...options };\n}\n\n/** A boolean. */\nexport function bool(options: Annotations<boolean> = {}): Schema<boolean> {\n\treturn { type: \"boolean\", ...options };\n}\n\n/** `null`. */\nexport function nul(options: Annotations<null> = {}): Schema<null> {\n\treturn { type: \"null\", ...options };\n}\n\n/** Any value. */\nexport function unknown(options: Annotations = {}): Schema<unknown> {\n\treturn { ...options };\n}\n\n/** Exactly `value` (a string, number, boolean or null). */\nexport function lit<const V extends string | number | boolean | null>(\n\tvalue: V,\n\toptions: Annotations<V> = {},\n): Schema<V> {\n\treturn { const: value, ...options };\n}\n\n/** One of the given values. */\nexport function oneOf<const V extends readonly (string | number | boolean | null)[]>(\n\tvalues: V,\n\toptions: Annotations<V[number]> = {},\n): Schema<V[number]> {\n\treturn { enum: values, ...options };\n}\n\n/**\n * Any of the given schemas. A union of objects tagged by a `lit()` property (`{ kind: lit(\"text\"), ... }`) is checked\n * by the tag: only the branch it selects runs, and its errors are the ones reported.\n */\nexport function union<const S extends readonly Schema[]>(\n\tschemas: S,\n\toptions: Annotations<Static<S[number]>> = {},\n): Schema<Static<S[number]>> {\n\treturn { anyOf: schemas, ...options };\n}\n\n/** `schema` or `null`. */\nexport function nullable<T>(schema: Schema<T>, options: Annotations<T | null> = {}): Schema<T | null> {\n\treturn { anyOf: [schema, { type: \"null\" }], ...options };\n}\n\n/** An array of `items`, with optional length bounds and unique items. */\nexport function arr<T>(\n\titems: Schema<T>,\n\toptions: Annotations<T[]> & {\n\t\treadonly minItems?: number;\n\t\treadonly maxItems?: number;\n\t\treadonly uniqueItems?: boolean;\n\t} = {},\n): Schema<T[]> {\n\treturn { type: \"array\", items, ...options };\n}\n\n/** A fixed-length array whose items match `items` in order. */\nexport function tuple<const S extends readonly Schema[]>(\n\titems: S,\n\toptions: Annotations<{ -readonly [K in keyof S]: Static<S[K]> }> = {},\n): Schema<{ -readonly [K in keyof S]: Static<S[K]> }> {\n\treturn { type: \"array\", prefixItems: items, items: false, minItems: items.length, ...options };\n}\n\n/** An object with string keys and values of `value` (a record). */\nexport function map<T>(value: Schema<T>, options: Annotations<Record<string, T>> = {}): Schema<Record<string, T>> {\n\treturn { type: \"object\", additionalProperties: value, ...options };\n}\n\n/** A Uint8Array with optional length bounds (an extension of JSON Schema for values that never pass through JSON). */\nexport function bytes(options: Annotations<Uint8Array> & LengthBounds = {}): Schema<Uint8Array> {\n\treturn { type: \"bytes\", ...options };\n}\n\n/** An optional property of obj(): `{ name: opt(str()) }` may omit `name` or set it to undefined. */\nexport class Optional<S extends Schema = Schema> {\n\tdeclare readonly [OPTIONAL_TYPE]?: S;\n\treadonly schema: S;\n\n\tconstructor(schema: S) {\n\t\tthis.schema = schema;\n\t}\n}\n\n/** Marks an obj() property as optional. */\nexport function opt<S extends Schema>(schema: S): Optional<S> {\n\treturn new Optional(schema);\n}\n\n/** The properties of obj(): a schema or an opt() schema per name. */\nexport type Properties = { readonly [name: string]: Schema | Optional };\n\n/** The value type of an obj() schema. */\nexport type StaticObject<P extends Properties> = Simplify<\n\t{ -readonly [K in keyof P as P[K] extends Optional ? never : K]: Static<P[K]> } & {\n\t\t-readonly [K in keyof P as P[K] extends Optional ? K : never]?: P[K] extends Optional<infer S>\n\t\t\t? Static<S>\n\t\t\t: never;\n\t}\n>;\n\ntype Simplify<T> = { [K in keyof T]: T[K] } & {};\n\n/**\n * A closed object: the given properties (required unless opt()) and no others. `open: true` allows other\n * properties, as tool schemas from other systems often do.\n */\nexport function obj<const P extends Properties>(\n\tproperties: P,\n\toptions: Annotations<StaticObject<P>> & { readonly open?: boolean } = {},\n): Schema<StaticObject<P>> {\n\tconst { open, ...annotations } = options;\n\tconst props: Record<string, Schema> = {};\n\tconst required: string[] = [];\n\tfor (const [name, property] of Object.entries(properties)) {\n\t\tif (property instanceof Optional) props[name] = property.schema;\n\t\telse {\n\t\t\tprops[name] = property;\n\t\t\trequired.push(name);\n\t\t}\n\t}\n\treturn {\n\t\ttype: \"object\",\n\t\tproperties: props,\n\t\t...(required.length > 0 ? { required } : {}),\n\t\t...(open === true ? {} : { additionalProperties: false }),\n\t\t...annotations,\n\t};\n}\n"]}
|
package/dist/checks.d.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { Check } from "./types.ts";
|
|
2
|
+
export declare const ALWAYS: Check;
|
|
3
|
+
export declare const NEVER: Check;
|
|
4
|
+
export declare const IS_ARRAY: Check;
|
|
5
|
+
export declare const IS_OBJECT: Check;
|
|
6
|
+
/** Whether `v` is a JSON object: not null, an array or bytes. */
|
|
7
|
+
export declare function isObject(v: unknown): v is Record<string, unknown>;
|
|
8
|
+
/** The check of a `type` name; `bytes` (a Uint8Array) extends JSON Schema for values that never pass through JSON. */
|
|
9
|
+
export declare const TYPE_CHECKS: ReadonlyMap<string, Check>;
|
|
10
|
+
/**
|
|
11
|
+
* A leaf check as a code and its argument, which object and array loops run by `leafOk` instead of calling the
|
|
12
|
+
* check: a call through a list of checks is not inlined, a switch is.
|
|
13
|
+
*/
|
|
14
|
+
export type Leaf = readonly [code: number, arg: unknown];
|
|
15
|
+
/** The leaf form of `check`, undefined when it is not a leaf. */
|
|
16
|
+
export declare function leafOf(check: Check): Leaf | undefined;
|
|
17
|
+
/** Whether a combined object or array check also checks the type: not at all, strictly, or passing other types. */
|
|
18
|
+
export declare const GUARD_NONE = 0;
|
|
19
|
+
export declare const GUARD_STRICT = 1;
|
|
20
|
+
export declare const GUARD_LOOSE = 2;
|
|
21
|
+
export type Guard = typeof GUARD_NONE | typeof GUARD_STRICT | typeof GUARD_LOOSE;
|
|
22
|
+
/** Flags of a declared property in an object check's entries: its leaf code and two bits. */
|
|
23
|
+
export declare const CODE = 15;
|
|
24
|
+
export declare const REQUIRED = 16;
|
|
25
|
+
export declare const INHERITED = 32;
|
|
26
|
+
/** All of `parts`; two and three parts get closures without a loop. */
|
|
27
|
+
export declare function and(parts: readonly Check[]): Check;
|
|
28
|
+
/** A check whose implementation is set later: the indirection a recursive reference goes through. */
|
|
29
|
+
export declare function indirect(slot: {
|
|
30
|
+
check: Check | undefined;
|
|
31
|
+
}): Check;
|
|
32
|
+
/** `check` with `uri` pushed onto the dynamic scope while it runs (only for schemas with `$dynamicRef`). */
|
|
33
|
+
export declare function scopedCheck(scope: string[], uri: string, check: Check): Check;
|
|
34
|
+
/** Any of the named types. */
|
|
35
|
+
export declare function typeCheck(types: readonly unknown[]): Check;
|
|
36
|
+
/** The bits of the named types; unknown names add none. */
|
|
37
|
+
export declare function typeMask(types: readonly unknown[]): number;
|
|
38
|
+
/** `anyOf` whose bare-type branches have the types of `mask`: those by one type test, the others by their checks. */
|
|
39
|
+
export declare function maskedAnyCheck(mask: number, checks: readonly Check[]): Check;
|
|
40
|
+
/** A Uint8Array within the length bounds. */
|
|
41
|
+
export declare function bytesCheck(min: number, max: number): Check;
|
|
42
|
+
/** The number of Unicode code points of `s`: JSON Schema string lengths count code points, not UTF-16 units. */
|
|
43
|
+
export declare function codePoints(s: string): number;
|
|
44
|
+
/** The string keywords: length bounds, pattern and format. */
|
|
45
|
+
export declare function stringCheck(strict: boolean, min: number, max: number, re: RegExp | undefined, format: ((value: string) => boolean) | undefined): Check;
|
|
46
|
+
/** The number keywords. `mode`: 0 passes other types, 1 requires a number, 2 an integer. */
|
|
47
|
+
export declare function numberCheck(mode: 0 | 1 | 2, min: number, max: number, xmin: number, xmax: number, multipleOf: number | undefined): Check;
|
|
48
|
+
/** Whether `v` is a multiple of `d`, exactly for decimals: 0.0075 / 0.0001 is 74.99999999999999 in floating point. */
|
|
49
|
+
export declare function isMultiple(v: number, d: number): boolean;
|
|
50
|
+
/** `inner` for objects; other values pass unless `strict`. */
|
|
51
|
+
export declare function guardObject(inner: Check, strict: boolean): Check;
|
|
52
|
+
/** `inner` for arrays; other values pass unless `strict`. */
|
|
53
|
+
export declare function guardArray(inner: Check, strict: boolean): Check;
|
|
54
|
+
/**
|
|
55
|
+
* An object with declared properties: `entries` is `[key, check, flags]` per property. The value's own keys are
|
|
56
|
+
* walked with for...in (whose `o[k]` V8 reads from the enum cache, unlike a load by a name from a list), each found
|
|
57
|
+
* among the declared names; `closed` rejects any other key. A property whose value is `undefined` counts as absent.
|
|
58
|
+
*/
|
|
59
|
+
export declare function declaredObject(entries: readonly unknown[], requiredCount: number, closed: boolean, guard: Guard): Check;
|
|
60
|
+
/** An object with declared properties, required keys not declared, pattern properties and additional properties. */
|
|
61
|
+
export declare function openObject(entries: readonly unknown[], keys: readonly string[], extraRequired: readonly string[], patterns: readonly (readonly [RegExp, Check])[], additional: Check | undefined, guard: Guard): Check;
|
|
62
|
+
/** Every property name passes `check` (`v` is an object). */
|
|
63
|
+
export declare function propertyNamesCheck(check: Check): Check;
|
|
64
|
+
/** `minProperties` and `maxProperties` (`v` is an object). */
|
|
65
|
+
export declare function propertyCountCheck(min: number, max: number): Check;
|
|
66
|
+
/** `dependentRequired` (`v` is an object). */
|
|
67
|
+
export declare function dependentRequiredCheck(entries: readonly (readonly [string, readonly string[]])[]): Check;
|
|
68
|
+
/** `dependentSchemas` (`v` is an object). */
|
|
69
|
+
export declare function dependentSchemasCheck(entries: readonly (readonly [string, Check])[]): Check;
|
|
70
|
+
/** The common array: guard, length bounds, prefix items and the items after them, in one closure. */
|
|
71
|
+
export declare function listCheck(guard: Guard, min: number, max: number, prefix: readonly Check[], rest: Check | undefined): Check;
|
|
72
|
+
/** Prefix items and the items after them (`v` is an array). */
|
|
73
|
+
export declare function itemsCheck(prefix: readonly Check[], rest: Check | undefined): Check;
|
|
74
|
+
/** `minItems` and `maxItems` (`v` is an array). */
|
|
75
|
+
export declare function lengthCheck(min: number, max: number): Check;
|
|
76
|
+
/** `contains` with its bounds (`v` is an array). */
|
|
77
|
+
export declare function containsCheck(check: Check, min: number, max: number): Check;
|
|
78
|
+
/** `uniqueItems` (`v` is an array). */
|
|
79
|
+
export declare function uniqueCheck(v: unknown): boolean;
|
|
80
|
+
/** `enum`: up to 8 primitives by a scan, more by a Set, objects and arrays by deep equality. */
|
|
81
|
+
export declare function enumCheck(values: readonly unknown[]): Check;
|
|
82
|
+
/** `const`. */
|
|
83
|
+
export declare function constCheck(value: unknown): Check;
|
|
84
|
+
/** `anyOf`. */
|
|
85
|
+
export declare function anyCheck(checks: readonly Check[]): Check;
|
|
86
|
+
/** `oneOf`. */
|
|
87
|
+
export declare function oneCheck(checks: readonly Check[]): Check;
|
|
88
|
+
/** `not`. */
|
|
89
|
+
export declare function notCheck(check: Check): Check;
|
|
90
|
+
/** `if`, `then` and `else`. */
|
|
91
|
+
export declare function ifCheck(cond: Check, then: Check, otherwise: Check): Check;
|
|
92
|
+
/** A union of objects tagged by a const on `key`: the tag selects the one branch that can match. */
|
|
93
|
+
export declare function taggedCheck(key: string, tags: readonly unknown[], checks: readonly Check[]): Check;
|
|
94
|
+
/** JSON equality: same primitives, arrays with equal items in order, objects with the same keys and equal values. */
|
|
95
|
+
export declare function deepEqual(a: unknown, b: unknown): boolean;
|
|
96
|
+
//# sourceMappingURL=checks.d.ts.map
|