@earendil-works/pi-schema 0.0.0-stage → 0.0.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/CHANGELOG.md +13 -0
- package/LICENSES/typebox-LICENSE +23 -0
- package/README.md +295 -2
- 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 +87 -0
- package/dist/checks.d.ts.map +1 -0
- package/dist/checks.js +447 -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 +532 -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 +381 -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 +57 -0
- package/dist/resolver.d.ts.map +1 -0
- package/dist/resolver.js +202 -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 +59 -5
|
@@ -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;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,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/builders.ts"],"names":[],"mappings":"AAAA,iHAAiH;AACjH,mHAAmH;AACnH,uFAAuF;AAwCvF,kFAAkF;AAClF,MAAM,UAAU,GAAG,CAClB,OAAO,GAAiG,EAAE;IAE1G,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,OAAO,EAAE,CAAC;AACvC,CAAC;AAWD,uCAAuC;AACvC,MAAM,UAAU,GAAG,CAAC,OAAO,GAAuC,EAAE;IACnE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,OAAO,EAAE,CAAC;AACvC,CAAC;AAED,yCAAyC;AACzC,MAAM,UAAU,GAAG,CAAC,OAAO,GAAuC,EAAE;IACnE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,GAAG,OAAO,EAAE,CAAC;AACxC,CAAC;AAED,iBAAiB;AACj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AC,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,87 @@
|
|
|
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
|
+
/** Leaf checks that object and array loops run inline by code instead of calling them. */
|
|
11
|
+
export declare const LEAF_CODES: ReadonlyMap<Check, number>;
|
|
12
|
+
/** Whether a combined object or array check also checks the type: not at all, strictly, or passing other types. */
|
|
13
|
+
export declare const GUARD_NONE = 0;
|
|
14
|
+
export declare const GUARD_STRICT = 1;
|
|
15
|
+
export declare const GUARD_LOOSE = 2;
|
|
16
|
+
export type Guard = typeof GUARD_NONE | typeof GUARD_STRICT | typeof GUARD_LOOSE;
|
|
17
|
+
/** Flags of a declared property in an object check's entries: its leaf code and two bits. */
|
|
18
|
+
export declare const CODE = 7;
|
|
19
|
+
export declare const REQUIRED = 8;
|
|
20
|
+
export declare const INHERITED = 16;
|
|
21
|
+
/** All of `parts`; two and three parts get closures without a loop. */
|
|
22
|
+
export declare function and(parts: readonly Check[]): Check;
|
|
23
|
+
/** A check whose implementation is set later: the indirection a recursive reference goes through. */
|
|
24
|
+
export declare function indirect(slot: {
|
|
25
|
+
check: Check | undefined;
|
|
26
|
+
}): Check;
|
|
27
|
+
/** `check` with `uri` pushed onto the dynamic scope while it runs (only for schemas with `$dynamicRef`). */
|
|
28
|
+
export declare function scopedCheck(scope: string[], uri: string, check: Check): Check;
|
|
29
|
+
/** Any of the named types. */
|
|
30
|
+
export declare function typeCheck(types: readonly unknown[]): Check;
|
|
31
|
+
/** A Uint8Array within the length bounds. */
|
|
32
|
+
export declare function bytesCheck(min: number, max: number): Check;
|
|
33
|
+
/** The number of Unicode code points of `s`: JSON Schema string lengths count code points, not UTF-16 units. */
|
|
34
|
+
export declare function codePoints(s: string): number;
|
|
35
|
+
/** The string keywords: length bounds, pattern and format. */
|
|
36
|
+
export declare function stringCheck(strict: boolean, min: number, max: number, re: RegExp | undefined, format: ((value: string) => boolean) | undefined): Check;
|
|
37
|
+
/** The number keywords. `mode`: 0 passes other types, 1 requires a number, 2 an integer. */
|
|
38
|
+
export declare function numberCheck(mode: 0 | 1 | 2, min: number, max: number, xmin: number, xmax: number, multipleOf: number | undefined): Check;
|
|
39
|
+
/** Whether `v` is a multiple of `d`, exactly for decimals: 0.0075 / 0.0001 is 74.99999999999999 in floating point. */
|
|
40
|
+
export declare function isMultiple(v: number, d: number): boolean;
|
|
41
|
+
/** `inner` for objects; other values pass unless `strict`. */
|
|
42
|
+
export declare function guardObject(inner: Check, strict: boolean): Check;
|
|
43
|
+
/** `inner` for arrays; other values pass unless `strict`. */
|
|
44
|
+
export declare function guardArray(inner: Check, strict: boolean): Check;
|
|
45
|
+
/**
|
|
46
|
+
* An object with declared properties: `entries` is `[key, check, flags]` per property. The value's own keys are
|
|
47
|
+
* walked with for...in (whose `o[k]` V8 reads from the enum cache, unlike a load by a name from a list), each found
|
|
48
|
+
* among the declared names; `closed` rejects any other key. A property whose value is `undefined` counts as absent.
|
|
49
|
+
*/
|
|
50
|
+
export declare function declaredObject(entries: readonly unknown[], requiredCount: number, closed: boolean, guard: Guard): Check;
|
|
51
|
+
/** An object with declared properties, required keys not declared, pattern properties and additional properties. */
|
|
52
|
+
export declare function openObject(entries: readonly unknown[], keys: readonly string[], extraRequired: readonly string[], patterns: readonly (readonly [RegExp, Check])[], additional: Check | undefined, guard: Guard): Check;
|
|
53
|
+
/** Every property name passes `check` (`v` is an object). */
|
|
54
|
+
export declare function propertyNamesCheck(check: Check): Check;
|
|
55
|
+
/** `minProperties` and `maxProperties` (`v` is an object). */
|
|
56
|
+
export declare function propertyCountCheck(min: number, max: number): Check;
|
|
57
|
+
/** `dependentRequired` (`v` is an object). */
|
|
58
|
+
export declare function dependentRequiredCheck(entries: readonly (readonly [string, readonly string[]])[]): Check;
|
|
59
|
+
/** `dependentSchemas` (`v` is an object). */
|
|
60
|
+
export declare function dependentSchemasCheck(entries: readonly (readonly [string, Check])[]): Check;
|
|
61
|
+
/** The common array: guard, length bounds, prefix items and the items after them, in one closure. */
|
|
62
|
+
export declare function listCheck(guard: Guard, min: number, max: number, prefix: readonly Check[], rest: Check | undefined): Check;
|
|
63
|
+
/** Prefix items and the items after them (`v` is an array). */
|
|
64
|
+
export declare function itemsCheck(prefix: readonly Check[], rest: Check | undefined): Check;
|
|
65
|
+
/** `minItems` and `maxItems` (`v` is an array). */
|
|
66
|
+
export declare function lengthCheck(min: number, max: number): Check;
|
|
67
|
+
/** `contains` with its bounds (`v` is an array). */
|
|
68
|
+
export declare function containsCheck(check: Check, min: number, max: number): Check;
|
|
69
|
+
/** `uniqueItems` (`v` is an array). */
|
|
70
|
+
export declare function uniqueCheck(v: unknown): boolean;
|
|
71
|
+
/** `enum`: up to 8 primitives by a scan, more by a Set, objects and arrays by deep equality. */
|
|
72
|
+
export declare function enumCheck(values: readonly unknown[]): Check;
|
|
73
|
+
/** `const`. */
|
|
74
|
+
export declare function constCheck(value: unknown): Check;
|
|
75
|
+
/** `anyOf`. */
|
|
76
|
+
export declare function anyCheck(checks: readonly Check[]): Check;
|
|
77
|
+
/** `oneOf`. */
|
|
78
|
+
export declare function oneCheck(checks: readonly Check[]): Check;
|
|
79
|
+
/** `not`. */
|
|
80
|
+
export declare function notCheck(check: Check): Check;
|
|
81
|
+
/** `if`, `then` and `else`. */
|
|
82
|
+
export declare function ifCheck(cond: Check, then: Check, otherwise: Check): Check;
|
|
83
|
+
/** A union of objects tagged by a const on `key`: the tag selects the one branch that can match. */
|
|
84
|
+
export declare function taggedCheck(key: string, tags: readonly unknown[], checks: readonly Check[]): Check;
|
|
85
|
+
/** JSON equality: same primitives, arrays with equal items in order, objects with the same keys and equal values. */
|
|
86
|
+
export declare function deepEqual(a: unknown, b: unknown): boolean;
|
|
87
|
+
//# sourceMappingURL=checks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"checks.d.ts","sourceRoot":"","sources":["../src/checks.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAExC,eAAO,MAAM,MAAM,EAAE,KAAkB,CAAC;AACxC,eAAO,MAAM,KAAK,EAAE,KAAmB,CAAC;AAMxC,eAAO,MAAM,QAAQ,EAAE,KAA+B,CAAC;AAEvD,eAAO,MAAM,SAAS,EAAE,KAA0B,CAAC;AAEnD,iEAAiE;AACjE,wBAAgB,QAAQ,CAAC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