@draht/schema 2026.11.5 → 2026.11.14-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/dist/builders.d.ts +29 -0
- package/dist/builders.d.ts.map +1 -1
- package/dist/builders.js +28 -0
- package/dist/builders.js.map +1 -1
- package/dist/checks.d.ts +24 -6
- package/dist/checks.d.ts.map +1 -1
- package/dist/checks.js +52 -14
- package/dist/checks.js.map +1 -1
- package/dist/coerce.d.ts.map +1 -1
- package/dist/coerce.js +41 -2
- package/dist/coerce.js.map +1 -1
- package/dist/compiler.d.ts.map +1 -1
- package/dist/compiler.js +12 -7
- package/dist/compiler.js.map +1 -1
- package/dist/explain.d.ts.map +1 -1
- package/dist/explain.js +25 -15
- package/dist/explain.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/typebox-static.d.ts +9 -1
- package/dist/typebox-static.d.ts.map +1 -1
- package/dist/typebox-static.js +3 -1
- package/dist/typebox-static.js.map +1 -1
- package/dist/validator.d.ts +9 -0
- package/dist/validator.d.ts.map +1 -1
- package/dist/validator.js +12 -2
- package/dist/validator.js.map +1 -1
- package/package.json +2 -2
- /package/{LICENSES → THIRD_PARTY_NOTICES}/typebox-LICENSE +0 -0
package/dist/builders.d.ts
CHANGED
|
@@ -49,6 +49,19 @@ export interface NumberBounds {
|
|
|
49
49
|
export declare function num(options?: Annotations<number> & NumberBounds): Schema<number>;
|
|
50
50
|
/** An integer within optional bounds. */
|
|
51
51
|
export declare function int(options?: Annotations<number> & NumberBounds): Schema<number>;
|
|
52
|
+
/** Bounds of a bigint: TypeBox's `Type.BigInt` options. */
|
|
53
|
+
export interface BigintBounds {
|
|
54
|
+
readonly minimum?: bigint;
|
|
55
|
+
readonly maximum?: bigint;
|
|
56
|
+
readonly exclusiveMinimum?: bigint;
|
|
57
|
+
readonly exclusiveMaximum?: bigint;
|
|
58
|
+
readonly multipleOf?: bigint;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* A bigint within optional bounds (`{ type: "bigint" }`, as TypeBox's `Type.BigInt`): an extension of JSON Schema for
|
|
62
|
+
* values that never pass through JSON, such as 64-bit integers a Parquet reader decodes.
|
|
63
|
+
*/
|
|
64
|
+
export declare function bigint(options?: Annotations<bigint> & BigintBounds): Schema<bigint>;
|
|
52
65
|
/** A boolean. */
|
|
53
66
|
export declare function bool(options?: Annotations<boolean>): Schema<boolean>;
|
|
54
67
|
/** `null`. */
|
|
@@ -80,6 +93,22 @@ export declare function tuple<const S extends readonly Schema[]>(items: S, optio
|
|
|
80
93
|
}>;
|
|
81
94
|
/** An object with string keys and values of `value` (a record). */
|
|
82
95
|
export declare function map<T>(value: Schema<T>, options?: Annotations<Record<string, T>>): Schema<Record<string, T>>;
|
|
96
|
+
/**
|
|
97
|
+
* `schema` typed as accepting `T`, for values the builders cannot express (a type checked elsewhere, a schema from
|
|
98
|
+
* another system). The schema is returned as it is: nothing checks that its values are `T`.
|
|
99
|
+
*/
|
|
100
|
+
export declare function unsafe<T>(schema?: Schema): Schema<T>;
|
|
101
|
+
/**
|
|
102
|
+
* A schema that contains itself, as a tree's children are trees: `build` receives a reference to the schema it
|
|
103
|
+
* returns. TypeScript cannot infer a recursive type from the builders, so `T` is given:
|
|
104
|
+
* `recursive<Tree>("Tree", (tree) => obj({ name: str(), children: arr(tree) }))`. `id` becomes the schema's `$id`
|
|
105
|
+
* and the target of the reference, so it must be unique among the recursive schemas within one schema.
|
|
106
|
+
*
|
|
107
|
+
* The counterpart of TypeBox's `Type.Cyclic` + `Type.Ref`, with other JSON: an `$id` on the schema instead of `$defs`
|
|
108
|
+
* and a root `$ref`. It validates the same values. The JSON differs only where a schema is published, and its type is
|
|
109
|
+
* written once instead of inferred, which a recursive schema (a file tree) needs only once.
|
|
110
|
+
*/
|
|
111
|
+
export declare function recursive<T>(id: string, build: (self: Schema<T>) => Schema<T>): Schema<T>;
|
|
83
112
|
/** A Uint8Array with optional length bounds (an extension of JSON Schema for values that never pass through JSON). */
|
|
84
113
|
export declare function bytes(options?: Annotations<Uint8Array> & LengthBounds): Schema<Uint8Array>;
|
|
85
114
|
/** An optional property of obj(): `{ name: opt(str()) }` may omit `name` or set it to undefined. */
|
package/dist/builders.d.ts.map
CHANGED
|
@@ -1 +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,CAA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E,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
CHANGED
|
@@ -13,6 +13,13 @@ export function num(options = {}) {
|
|
|
13
13
|
export function int(options = {}) {
|
|
14
14
|
return { type: "integer", ...options };
|
|
15
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* A bigint within optional bounds (`{ type: "bigint" }`, as TypeBox's `Type.BigInt`): an extension of JSON Schema for
|
|
18
|
+
* values that never pass through JSON, such as 64-bit integers a Parquet reader decodes.
|
|
19
|
+
*/
|
|
20
|
+
export function bigint(options = {}) {
|
|
21
|
+
return { type: "bigint", ...options };
|
|
22
|
+
}
|
|
16
23
|
/** A boolean. */
|
|
17
24
|
export function bool(options = {}) {
|
|
18
25
|
return { type: "boolean", ...options };
|
|
@@ -56,6 +63,27 @@ export function tuple(items, options = {}) {
|
|
|
56
63
|
export function map(value, options = {}) {
|
|
57
64
|
return { type: "object", additionalProperties: value, ...options };
|
|
58
65
|
}
|
|
66
|
+
/**
|
|
67
|
+
* `schema` typed as accepting `T`, for values the builders cannot express (a type checked elsewhere, a schema from
|
|
68
|
+
* another system). The schema is returned as it is: nothing checks that its values are `T`.
|
|
69
|
+
*/
|
|
70
|
+
export function unsafe(schema = {}) {
|
|
71
|
+
// The brand is type-level only; asserting it is what this builder is for.
|
|
72
|
+
return schema;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* A schema that contains itself, as a tree's children are trees: `build` receives a reference to the schema it
|
|
76
|
+
* returns. TypeScript cannot infer a recursive type from the builders, so `T` is given:
|
|
77
|
+
* `recursive<Tree>("Tree", (tree) => obj({ name: str(), children: arr(tree) }))`. `id` becomes the schema's `$id`
|
|
78
|
+
* and the target of the reference, so it must be unique among the recursive schemas within one schema.
|
|
79
|
+
*
|
|
80
|
+
* The counterpart of TypeBox's `Type.Cyclic` + `Type.Ref`, with other JSON: an `$id` on the schema instead of `$defs`
|
|
81
|
+
* and a root `$ref`. It validates the same values. The JSON differs only where a schema is published, and its type is
|
|
82
|
+
* written once instead of inferred, which a recursive schema (a file tree) needs only once.
|
|
83
|
+
*/
|
|
84
|
+
export function recursive(id, build) {
|
|
85
|
+
return { ...build({ $ref: id }), $id: id };
|
|
86
|
+
}
|
|
59
87
|
/** A Uint8Array with optional length bounds (an extension of JSON Schema for values that never pass through JSON). */
|
|
60
88
|
export function bytes(options = {}) {
|
|
61
89
|
return { type: "bytes", ...options };
|
package/dist/builders.js.map
CHANGED
|
@@ -1 +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;AACjB,MAAM,UAAU,IAAI,CAAC,OAAO,GAAyB,EAAE;IACtD,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,GAAG,OAAO,EAAE,CAAC;AACxC,CAAC;AAED,cAAc;AACd,MAAM,UAAU,GAAG,CAAC,OAAO,GAAsB,EAAE;IAClD,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,OAAO,EAAE,C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,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"]}
|
|
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;AAWD;;;GAGG;AACH,MAAM,UAAU,MAAM,CAAC,OAAO,GAAuC,EAAE;IACtE,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,GAAG,OAAO,EAAE,CAAC;AACvC,CAAC;AAED,iBAAiB;AACjB,MAAM,UAAU,IAAI,CAAC,OAAO,GAAyB,EAAE;IACtD,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,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AAK,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/** Bounds of a bigint: TypeBox's `Type.BigInt` options. */\nexport interface BigintBounds {\n\treadonly minimum?: bigint;\n\treadonly maximum?: bigint;\n\treadonly exclusiveMinimum?: bigint;\n\treadonly exclusiveMaximum?: bigint;\n\treadonly multipleOf?: bigint;\n}\n\n/**\n * A bigint within optional bounds (`{ type: \"bigint\" }`, as TypeBox's `Type.BigInt`): an extension of JSON Schema for\n * values that never pass through JSON, such as 64-bit integers a Parquet reader decodes.\n */\nexport function bigint(options: Annotations<bigint> & BigintBounds = {}): Schema<bigint> {\n\treturn { type: \"bigint\", ...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/**\n * `schema` typed as accepting `T`, for values the builders cannot express (a type checked elsewhere, a schema from\n * another system). The schema is returned as it is: nothing checks that its values are `T`.\n */\nexport function unsafe<T>(schema: Schema = {}): Schema<T> {\n\t// The brand is type-level only; asserting it is what this builder is for.\n\treturn schema as Schema<T>;\n}\n\n/**\n * A schema that contains itself, as a tree's children are trees: `build` receives a reference to the schema it\n * returns. TypeScript cannot infer a recursive type from the builders, so `T` is given:\n * `recursive<Tree>(\"Tree\", (tree) => obj({ name: str(), children: arr(tree) }))`. `id` becomes the schema's `$id`\n * and the target of the reference, so it must be unique among the recursive schemas within one schema.\n *\n * The counterpart of TypeBox's `Type.Cyclic` + `Type.Ref`, with other JSON: an `$id` on the schema instead of `$defs`\n * and a root `$ref`. It validates the same values. The JSON differs only where a schema is published, and its type is\n * written once instead of inferred, which a recursive schema (a file tree) needs only once.\n */\nexport function recursive<T>(id: string, build: (self: Schema<T>) => Schema<T>): Schema<T> {\n\treturn { ...build({ $ref: id }), $id: id };\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
CHANGED
|
@@ -5,7 +5,11 @@ export declare const IS_ARRAY: Check;
|
|
|
5
5
|
export declare const IS_OBJECT: Check;
|
|
6
6
|
/** Whether `v` is a JSON object: not null, an array or bytes. */
|
|
7
7
|
export declare function isObject(v: unknown): v is Record<string, unknown>;
|
|
8
|
-
/**
|
|
8
|
+
/**
|
|
9
|
+
* The check of a `type` name. `bytes` (a Uint8Array), `bigint` (TypeBox's `Type.BigInt`) and `undefined` (TypeBox's
|
|
10
|
+
* `Type.Undefined`) extend JSON Schema for values that never pass through JSON. A `number` is finite, as in TypeBox:
|
|
11
|
+
* JSON has no NaN or Infinity, so only a value built in code can be one.
|
|
12
|
+
*/
|
|
9
13
|
export declare const TYPE_CHECKS: ReadonlyMap<string, Check>;
|
|
10
14
|
/**
|
|
11
15
|
* A leaf check as a code and its argument, which object and array loops run by `leafOk` instead of calling the
|
|
@@ -43,10 +47,21 @@ export declare function bytesCheck(min: number, max: number): Check;
|
|
|
43
47
|
export declare function codePoints(s: string): number;
|
|
44
48
|
/** The string keywords: length bounds, pattern and format. */
|
|
45
49
|
export declare function stringCheck(strict: boolean, min: number, max: number, re: RegExp | undefined, format: ((value: string) => boolean) | undefined): Check;
|
|
46
|
-
/**
|
|
47
|
-
export
|
|
48
|
-
/**
|
|
49
|
-
|
|
50
|
+
/** A number bound: a number, or a bigint (TypeBox's `Type.BigInt` writes its bounds as bigints). */
|
|
51
|
+
export type Bound = number | bigint;
|
|
52
|
+
/**
|
|
53
|
+
* The number keywords, for numbers and bigints alike (as TypeBox applies them): bounds of either kind compare with
|
|
54
|
+
* values of either kind. `mode`: 0 passes other types, 1 requires a number, 2 an integer, 3 a bigint.
|
|
55
|
+
*/
|
|
56
|
+
export declare function numberCheck(mode: 0 | 1 | 2 | 3, min: Bound, max: Bound, xmin: Bound, xmax: Bound, multipleOf: Bound | undefined): Check;
|
|
57
|
+
/**
|
|
58
|
+
* Whether `v` is a multiple of `d`, exactly for decimals: 0.0075 / 0.0001 is 74.99999999999999 in floating point. With
|
|
59
|
+
* a bigint on either side, exact integer arithmetic; a bigint is no multiple of 0 or of a fraction.
|
|
60
|
+
*
|
|
61
|
+
* Deliberately unlike TypeBox, which throws there (`BigInt(0.5)`, division by zero): a check answers for every value
|
|
62
|
+
* and never throws, and no real schema pairs a bigint with a fractional or zero `multipleOf`.
|
|
63
|
+
*/
|
|
64
|
+
export declare function isMultiple(v: Bound, d: Bound): boolean;
|
|
50
65
|
/** `inner` for objects; other values pass unless `strict`. */
|
|
51
66
|
export declare function guardObject(inner: Check, strict: boolean): Check;
|
|
52
67
|
/** `inner` for arrays; other values pass unless `strict`. */
|
|
@@ -91,6 +106,9 @@ export declare function notCheck(check: Check): Check;
|
|
|
91
106
|
export declare function ifCheck(cond: Check, then: Check, otherwise: Check): Check;
|
|
92
107
|
/** A union of objects tagged by a const on `key`: the tag selects the one branch that can match. */
|
|
93
108
|
export declare function taggedCheck(key: string, tags: readonly unknown[], checks: readonly Check[]): Check;
|
|
94
|
-
/**
|
|
109
|
+
/**
|
|
110
|
+
* JSON equality, as `const`, `enum` and `uniqueItems` compare: the same primitives, arrays with equal items in order,
|
|
111
|
+
* objects with the same keys and equal values (key order does not matter).
|
|
112
|
+
*/
|
|
95
113
|
export declare function deepEqual(a: unknown, b: unknown): boolean;
|
|
96
114
|
//# sourceMappingURL=checks.d.ts.map
|
package/dist/checks.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"checks.d.ts","sourceRoot":"","sources":["../src/checks.ts"],"names":[],"mappings":"AAIA,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;
|
|
1
|
+
{"version":3,"file":"checks.d.ts","sourceRoot":"","sources":["../src/checks.ts"],"names":[],"mappings":"AAIA,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;AAIvD,eAAO,MAAM,SAAS,EAAE,KAA0B,CAAC;AAEnD,iEAAiE;AACjE,wBAAgB,QAAQ,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAEjE;AAED;;;;GAIG;AACH,eAAO,MAAM,WAAW,EAAE,WAAW,CAAC,MAAM,EAAE,KAAK,CAWjD,CAAC;AAEH;;;GAGG;AACH,MAAM,MAAM,IAAI,GAAG,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC;AAWzD,iEAAiE;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AE,SAAS,OAAO,EAAE,GAAG,KAAK,CAW3D;AAED,eAAe;AACf,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,CAGhD;AAED,eAAe;AACf,wBAAgB,QAAQ,CAAC,MAAM,EAAE,SAAS,KAAK,EAAE,GAAG,KAAK,CAKxD;AAED,eAAe;AACf,wBAAgB,QAAQ,CAAC,MAAM,EAAE,SAAS,KAAK,EAAE,GAAG,KAAK,CAMxD;AAED,aAAa;AACb,wBAAgB,QAAQ,CAAC,KAAK,EAAE,KAAK,GAAG,KAAK,CAE5C;AAED,+BAA+B;AAC/B,wBAAgB,OAAO,CAAC,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,KAAK,GAAG,KAAK,CAEzE;AAED,oGAAoG;AACpG,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,OAAO,EAAE,EAAE,MAAM,EAAE,SAAS,KAAK,EAAE,GAAG,KAAK,CAOlG;AAED;;;GAGG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAkBzD"}
|
package/dist/checks.js
CHANGED
|
@@ -4,18 +4,24 @@
|
|
|
4
4
|
export const ALWAYS = () => true;
|
|
5
5
|
export const NEVER = () => false;
|
|
6
6
|
const IS_STRING = (v) => typeof v === "string";
|
|
7
|
-
const IS_NUMBER = (v) =>
|
|
7
|
+
const IS_NUMBER = (v) => Number.isFinite(v);
|
|
8
8
|
const IS_INTEGER = (v) => Number.isInteger(v);
|
|
9
9
|
const IS_BOOLEAN = (v) => typeof v === "boolean";
|
|
10
10
|
const IS_NULL = (v) => v === null;
|
|
11
11
|
export const IS_ARRAY = (v) => Array.isArray(v);
|
|
12
12
|
const IS_BYTES = (v) => v instanceof Uint8Array;
|
|
13
|
+
const IS_BIGINT = (v) => typeof v === "bigint";
|
|
14
|
+
const IS_UNDEFINED = (v) => v === undefined;
|
|
13
15
|
export const IS_OBJECT = (v) => isObject(v);
|
|
14
16
|
/** Whether `v` is a JSON object: not null, an array or bytes. */
|
|
15
17
|
export function isObject(v) {
|
|
16
18
|
return typeof v === "object" && v !== null && !Array.isArray(v) && !(v instanceof Uint8Array);
|
|
17
19
|
}
|
|
18
|
-
/**
|
|
20
|
+
/**
|
|
21
|
+
* The check of a `type` name. `bytes` (a Uint8Array), `bigint` (TypeBox's `Type.BigInt`) and `undefined` (TypeBox's
|
|
22
|
+
* `Type.Undefined`) extend JSON Schema for values that never pass through JSON. A `number` is finite, as in TypeBox:
|
|
23
|
+
* JSON has no NaN or Infinity, so only a value built in code can be one.
|
|
24
|
+
*/
|
|
19
25
|
export const TYPE_CHECKS = new Map([
|
|
20
26
|
["string", IS_STRING],
|
|
21
27
|
["number", IS_NUMBER],
|
|
@@ -25,6 +31,8 @@ export const TYPE_CHECKS = new Map([
|
|
|
25
31
|
["array", IS_ARRAY],
|
|
26
32
|
["object", IS_OBJECT],
|
|
27
33
|
["bytes", IS_BYTES],
|
|
34
|
+
["bigint", IS_BIGINT],
|
|
35
|
+
["undefined", IS_UNDEFINED],
|
|
28
36
|
]);
|
|
29
37
|
const leaves = new WeakMap([
|
|
30
38
|
[IS_STRING, [1, undefined]],
|
|
@@ -61,7 +69,7 @@ function leafOk(code, x, arg) {
|
|
|
61
69
|
case 1:
|
|
62
70
|
return typeof x === "string";
|
|
63
71
|
case 2:
|
|
64
|
-
return
|
|
72
|
+
return Number.isFinite(x);
|
|
65
73
|
case 3:
|
|
66
74
|
return Number.isInteger(x);
|
|
67
75
|
case 4:
|
|
@@ -82,7 +90,7 @@ function leafOk(code, x, arg) {
|
|
|
82
90
|
case 10:
|
|
83
91
|
return Number.isInteger(x) && inBounds(x, arg);
|
|
84
92
|
case 11:
|
|
85
|
-
return
|
|
93
|
+
return Number.isFinite(x) && inBounds(x, arg);
|
|
86
94
|
case 12:
|
|
87
95
|
return typeof x === "string" && lengthOk(x, arg.min, arg.max);
|
|
88
96
|
case 13:
|
|
@@ -154,6 +162,8 @@ const TYPE_BITS = new Map([
|
|
|
154
162
|
["array", 32],
|
|
155
163
|
["object", 64],
|
|
156
164
|
["bytes", 128],
|
|
165
|
+
["bigint", 256],
|
|
166
|
+
["undefined", 512],
|
|
157
167
|
]);
|
|
158
168
|
/** The bits of the named types; unknown names add none. */
|
|
159
169
|
export function typeMask(types) {
|
|
@@ -163,17 +173,21 @@ export function typeMask(types) {
|
|
|
163
173
|
mask |= TYPE_BITS.get(t) ?? 0;
|
|
164
174
|
return mask;
|
|
165
175
|
}
|
|
166
|
-
/** The type bits of `v`: an integer has the bits of `number` and `integer
|
|
176
|
+
/** The type bits of `v`: an integer has the bits of `number` and `integer`; NaN and Infinity have neither. */
|
|
167
177
|
function typeBits(v) {
|
|
168
178
|
switch (typeof v) {
|
|
169
179
|
case "string":
|
|
170
180
|
return 1;
|
|
171
181
|
case "number":
|
|
172
|
-
return Number.isInteger(v) ? 6 : 2;
|
|
182
|
+
return Number.isInteger(v) ? 6 : Number.isFinite(v) ? 2 : 0;
|
|
173
183
|
case "boolean":
|
|
174
184
|
return 8;
|
|
175
185
|
case "object":
|
|
176
186
|
return v === null ? 16 : Array.isArray(v) ? 32 : v instanceof Uint8Array ? 128 : 64;
|
|
187
|
+
case "bigint":
|
|
188
|
+
return 256;
|
|
189
|
+
case "undefined":
|
|
190
|
+
return 512;
|
|
177
191
|
default:
|
|
178
192
|
return 0;
|
|
179
193
|
}
|
|
@@ -233,21 +247,39 @@ export function stringCheck(strict, min, max, re, format) {
|
|
|
233
247
|
return lengthOk(v, min, max) && (re === undefined || re.test(v)) && (format === undefined || format(v));
|
|
234
248
|
};
|
|
235
249
|
}
|
|
236
|
-
/**
|
|
250
|
+
/**
|
|
251
|
+
* The number keywords, for numbers and bigints alike (as TypeBox applies them): bounds of either kind compare with
|
|
252
|
+
* values of either kind. `mode`: 0 passes other types, 1 requires a number, 2 an integer, 3 a bigint.
|
|
253
|
+
*/
|
|
237
254
|
export function numberCheck(mode, min, max, xmin, xmax, multipleOf) {
|
|
238
255
|
const check = (v) => {
|
|
239
|
-
if (typeof v !== "number")
|
|
256
|
+
if (typeof v !== "number" && typeof v !== "bigint")
|
|
240
257
|
return mode === 0;
|
|
258
|
+
if (mode === 3 ? typeof v !== "bigint" : mode !== 0 && !Number.isFinite(v))
|
|
259
|
+
return false;
|
|
241
260
|
if (mode === 2 && !Number.isInteger(v))
|
|
242
261
|
return false;
|
|
243
262
|
return v >= min && v <= max && v > xmin && v < xmax && (multipleOf === undefined || isMultiple(v, multipleOf));
|
|
244
263
|
};
|
|
245
|
-
|
|
264
|
+
const plain = typeof min === "number" && typeof max === "number" && typeof xmin === "number" && typeof xmax === "number";
|
|
265
|
+
if (mode === 0 || mode === 3 || multipleOf !== undefined || !plain)
|
|
246
266
|
return check;
|
|
247
267
|
return leaf(mode === 2 ? 10 : 11, new Bounds(min, max, xmin, xmax), check);
|
|
248
268
|
}
|
|
249
|
-
/**
|
|
269
|
+
/**
|
|
270
|
+
* Whether `v` is a multiple of `d`, exactly for decimals: 0.0075 / 0.0001 is 74.99999999999999 in floating point. With
|
|
271
|
+
* a bigint on either side, exact integer arithmetic; a bigint is no multiple of 0 or of a fraction.
|
|
272
|
+
*
|
|
273
|
+
* Deliberately unlike TypeBox, which throws there (`BigInt(0.5)`, division by zero): a check answers for every value
|
|
274
|
+
* and never throws, and no real schema pairs a bigint with a fractional or zero `multipleOf`.
|
|
275
|
+
*/
|
|
250
276
|
export function isMultiple(v, d) {
|
|
277
|
+
if (typeof v === "bigint" || typeof d === "bigint") {
|
|
278
|
+
if ((typeof v === "number" && !Number.isInteger(v)) || (typeof d === "number" && !Number.isInteger(d)))
|
|
279
|
+
return false;
|
|
280
|
+
const divisor = BigInt(d);
|
|
281
|
+
return divisor !== 0n && BigInt(v) % divisor === 0n;
|
|
282
|
+
}
|
|
251
283
|
const q = v / d;
|
|
252
284
|
if (Number.isInteger(q))
|
|
253
285
|
return true;
|
|
@@ -300,9 +332,11 @@ export function declaredObject(entries, requiredCount, closed, guard) {
|
|
|
300
332
|
}
|
|
301
333
|
next = i + 3;
|
|
302
334
|
const x = o[k];
|
|
303
|
-
if (x === undefined)
|
|
304
|
-
continue;
|
|
305
335
|
const f = entries[i + 2];
|
|
336
|
+
// An optional property set to undefined counts as absent; a required one is present, and its schema decides
|
|
337
|
+
// whether undefined is a value of it (TypeBox's Type.Undefined, Unknown, Any).
|
|
338
|
+
if (x === undefined && !(f & REQUIRED))
|
|
339
|
+
continue;
|
|
306
340
|
const c = f & CODE;
|
|
307
341
|
if (c !== 0 ? !leafOk(c, x, entries[i + 1]) : !entries[i + 1](x))
|
|
308
342
|
return false;
|
|
@@ -323,7 +357,8 @@ export function openObject(entries, keys, extraRequired, patterns, additional, g
|
|
|
323
357
|
const k = entries[i];
|
|
324
358
|
const x = o[k];
|
|
325
359
|
const f = entries[i + 2];
|
|
326
|
-
|
|
360
|
+
// Absent, or an optional property set to undefined (which counts as absent); see declaredObject.
|
|
361
|
+
if ((x === undefined && !(f & REQUIRED && Object.hasOwn(o, k))) || (f & INHERITED && !Object.hasOwn(o, k))) {
|
|
327
362
|
if (f & REQUIRED)
|
|
328
363
|
return false;
|
|
329
364
|
continue;
|
|
@@ -508,7 +543,10 @@ export function taggedCheck(key, tags, checks) {
|
|
|
508
543
|
return false;
|
|
509
544
|
};
|
|
510
545
|
}
|
|
511
|
-
/**
|
|
546
|
+
/**
|
|
547
|
+
* JSON equality, as `const`, `enum` and `uniqueItems` compare: the same primitives, arrays with equal items in order,
|
|
548
|
+
* objects with the same keys and equal values (key order does not matter).
|
|
549
|
+
*/
|
|
512
550
|
export function deepEqual(a, b) {
|
|
513
551
|
if (a === b)
|
|
514
552
|
return true;
|