@earendil-works/pi-schema 0.0.0 → 1.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/CHANGELOG.md CHANGED
@@ -1,6 +1,28 @@
1
1
  # Changelog
2
2
 
3
- ## [Unreleased]
3
+ ## [1.5.1] - 2026-11-09
4
+
5
+ ### Added
6
+
7
+ - `unsafe<T>(schema)` types a schema as accepting `T` without checking it, as TypeBox's `Type.Unsafe` does.
8
+ - `recursive<T>(id, build)` builds a schema that contains itself (a tree), with its type given as `T`.
9
+ - `Validator.validate(value)` returns `{ ok: true, value }` or `{ ok: false, errors }` without coercing, on every validator.
10
+ - `Static` types TypeBox's `Enum`, `Tuple`, `BigInt` and `Undefined` schemas.
11
+ - TypeBox's JavaScript types validate and coerce as TypeBox does: `{ type: "bigint" }` (`Type.BigInt`) and `{ type: "undefined" }` (`Type.Undefined`), with `Value.Convert`'s rules for both; the number keywords apply to numbers and bigints alike, with bounds of either kind. The `bigint()` builder builds the former.
12
+ - `equal(a, b)`: JSON equality as `const`, `enum` and `uniqueItems` compare, a drop-in for TypeBox's `Value.Equal`.
13
+
14
+ ### Changed
15
+
16
+ - Faster checks, still without eval: object and array loops run `const`, small `enum`s, type lists, number bounds, string lengths and patterns inline instead of calling a closure; a closed object looks up keys in declared order first; `anyOf` branches that are bare types (as `nullable` builds) run as one type test. Checks take about 16% less time across the benchmark's speed cases, up to 30% less for tagged unions and 45% less for nullable properties.
17
+
18
+ ### Fixed
19
+
20
+ - A `number` or `integer` no longer accepts `NaN`, `Infinity` or `-Infinity`, as in TypeBox; before, an unbounded `num()` accepted them. JSON has neither, so only values built in code are affected. Errors name the value (`expected number, got NaN`).
21
+ - The third-party licenses are in `THIRD_PARTY_NOTICES/`, renamed from `LICENSES/`, which Vite's license notices read as the package's license file, failing browser builds that bundle the package.
22
+ - A required property set to `undefined` is valid when its schema takes `undefined` (a union with `Type.Undefined()`, `Type.Unknown()`, `Type.Any()`), as in TypeBox; before, it was always missing. An optional one set to `undefined` still counts as absent.
23
+ - Fixed local `$ref`s in schemas whose root `$id` is a relative path with a directory, such as `schemas/tool.json`: the `$id` was applied twice, so `compile` threw and `unresolvedRefs: "reject"` rejected valid tool arguments. Relative `$id`s are now also applied once with `unevaluatedProperties`/`unevaluatedItems`, when explaining errors under nested resources, and on the first coercion of a schema with `$dynamicRef` ([#11697](https://github.com/earendil-works/pi/issues/11697))
24
+
25
+ ## [1.5.0] - 2026-11-02
4
26
 
5
27
  ### Added
6
28
 
package/README.md CHANGED
@@ -47,7 +47,8 @@ or not.
47
47
 
48
48
  `check(value)` is the fast path: a boolean, no allocations for most schemas, and a plain function you can pass
49
49
  around. `errors(value)` explains a failure and returns `[]` for a valid value. It is built on the first failure and
50
- kept, so a schema whose values always pass never pays for it.
50
+ kept, so a schema whose values always pass never pays for it. `validate(value)` combines the two without changing
51
+ the value: `{ ok: true, value }` or `{ ok: false, errors }`.
51
52
 
52
53
  ### Builders
53
54
 
@@ -65,6 +66,9 @@ kept, so a schema whose values always pass never pays for it.
65
66
  | `union([...])` | `{ anyOf: [...] }` | the union |
66
67
  | `nullable(schema)` | `{ anyOf: [schema, { type: "null" }] }` | `T \| null` |
67
68
  | `bytes({ minLength, maxLength })` | `{ type: "bytes", ... }` | `Uint8Array` |
69
+ | `bigint({ minimum, maximum, exclusiveMinimum, exclusiveMaximum, multipleOf })` | `{ type: "bigint", ... }`, bigint bounds | `bigint` |
70
+ | `unsafe<T>(schema)` | `schema` as given (`{}` by default) | `T`, unchecked |
71
+ | `recursive<T>(id, (self) => schema)` | `schema` with `$id: id`; `self` is `{ $ref: id }` | `T` |
68
72
 
69
73
  Every builder also takes `title`, `description`, `examples`, `deprecated` and `default`. A `default` is an annotation;
70
74
  validation never inserts it.
@@ -77,11 +81,20 @@ compile(Block).errors({ kind: "video" });
77
81
  // [{ path: "/kind", keyword: "anyOf", message: 'expected one of ["text","image"]' }]
78
82
  ```
79
83
 
84
+ A recursive schema needs its type spelled out, since TypeScript cannot infer one from the builders. Its `id` must be
85
+ unique among the recursive schemas within one schema:
86
+
87
+ ```typescript
88
+ type Tree = { name: string; children: Tree[] };
89
+ const Tree = recursive<Tree>("Tree", (self) => obj({ name: str(), children: arr(self) }));
90
+ ```
91
+
80
92
  `Static` also types schemas built with TypeBox 1.x, read from TypeBox's type shapes (pi-schema does not import
81
93
  TypeBox): `String`, `Number`, `Integer`, `Boolean`, `Null`, `Literal`, `Array`, `Object` (with `Type.Optional` and
82
- `Type.Readonly` properties), `Union`, `Record`, `Unsafe`, `Unknown` and `Any` give TypeBox's own static type, so
83
- `Static<typeof Type.Object({ path: Type.String() })>` is `{ path: string }`. Other TypeBox kinds (`Enum`, `Tuple`,
84
- `Intersect`, `Cyclic`, ...) give `unknown` where they stand. Other JSON Schema objects give `unknown`.
94
+ `Type.Readonly` properties), `Union`, `Record`, `Enum`, `Tuple`, `BigInt`, `Undefined`, `Unsafe`, `Unknown` and `Any`
95
+ give TypeBox's own static type, so `Static<typeof Type.Object({ path: Type.String() })>` is `{ path: string }`. Other
96
+ TypeBox kinds (`Intersect`, `Cyclic`, `Ref`, ...) give `unknown` where they stand. Other JSON Schema objects give
97
+ `unknown`.
85
98
 
86
99
  ### Options
87
100
 
@@ -116,8 +129,8 @@ import Type from "typebox";
116
129
  const user = compile(Type.Object({ name: Type.String(), tags: Type.Array(Type.String()) }));
117
130
  ```
118
131
 
119
- TypeBox's JavaScript-only types (`BigInt`, `Undefined`, `Function`, ...), refinements and codecs are not JSON Schema
120
- and are not supported.
132
+ TypeBox's `BigInt` and `Undefined` are supported (see Values). Its other JavaScript-only types (`Function`, `Symbol`,
133
+ ...), refinements and codecs are not JSON Schema and are not supported.
121
134
 
122
135
  ### Coercion
123
136
 
@@ -151,6 +164,9 @@ Three passes run in order:
151
164
  - `integer`: as `number`, then truncated (`"3.5"` and `3.5` become 3).
152
165
  - `boolean`: `"true"`/`"false"` (any case), `"1"`/`"0"`, 1/0, `null` to `false`.
153
166
  - `null`: `"null"`, `"undefined"`, `""`, `"0"`, 0 and `false`.
167
+ - `bigint`: integer strings (`"12"`, `"12n"`; `"12.9"` truncated), numbers truncated, `"true"`/`"false"` and
168
+ booleans to 1n/0n, `null` to 0n.
169
+ - `undefined`: `"undefined"`, `"null"`, `""`, `"0"`, 0, 0n, `false` and `null`.
154
170
  - `array`: a single value becomes a one-item array, then each item converts; a tuple (`prefixItems` or an `items`
155
171
  array) converts its items and never wraps.
156
172
  - `object`: each property converts by `properties`, `patternProperties` or `additionalProperties`.
@@ -171,9 +187,16 @@ coerced by any pass.
171
187
 
172
188
  ### Values
173
189
 
174
- - A property whose value is `undefined` counts as absent, as in TypeBox.
190
+ - An optional property whose value is `undefined` counts as absent, as in TypeBox. A required property set to
191
+ `undefined` is present: valid when its schema takes `undefined` (`Type.Undefined()` in it, `Type.Unknown()`,
192
+ `Type.Any()`), else missing.
175
193
  - `{ type: "bytes", minLength, maxLength }` matches a `Uint8Array` of that length, an extension for values that never
176
194
  pass through JSON.
195
+ - `{ type: "bigint" }` (TypeBox's `Type.BigInt`) matches a `bigint`. The number keywords (`minimum`, `maximum`,
196
+ `exclusiveMinimum`, `exclusiveMaximum`, `multipleOf`) apply to numbers and bigints alike, with bounds of either kind,
197
+ as in TypeBox.
198
+ - `{ type: "undefined" }` (TypeBox's `Type.Undefined`) matches `undefined`.
199
+ - `equal(a, b)` is JSON equality as `const`, `enum` and `uniqueItems` compare (TypeBox's `Value.Equal`).
177
200
 
178
201
  ## Supported keywords
179
202
 
@@ -293,4 +316,4 @@ schemas that arrive later.
293
316
  `npm test` runs the official test suite (drafts 2020-12 and 7, including the optional and format tests, vendored in
294
317
  `test/fixtures/`) and the package's own tests.
295
318
 
296
- The format checks are adapted from TypeBox 1.3.27 (MIT); its license is in `LICENSES/`.
319
+ The format checks are adapted from TypeBox 1.3.27 (MIT); its license is in `THIRD_PARTY_NOTICES/`.
@@ -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. */
@@ -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,CAAC,QAAQ,CAAC,EA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,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 };
@@ -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,CAAC;AACrC,CAAC;AAED,iBAAiB;AACjB,MAAM,UAAU,OAAO,CAAC,OAAO,GAAgB,EAAE;IAChD,OAAO,EAAE,GAAG,OAAO,EAAE,CAAC;AACvB,CAAC;AAED,2DAA2D;AAC3D,MAAM,UAAU,GAAG,CAClB,KAAQ,EACR,OAAO,GAAmB,EAAE;IAE5B,OAAO,EAAE,KAAK,EAAE,KAAK,EAAE,GAAG,OAAO,EAAE,CAAC;AACrC,CAAC;AAED,+BAA+B;AAC/B,MAAM,UAAU,KAAK,CACpB,MAAS,EACT,OAAO,GAA2B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nB,YAAY,MAAS;QACpB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACtB,CAAC;CACD;AAED,2CAA2C;AAC3C,MAAM,UAAU,GAAG,CAAmB,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"]}
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,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,CAAC;AACrC,CAAC;AAED,iBAAiB;AACjB,MAAM,UAAU,OAAO,CAAC,OAAO,GAAgB,EAAE;IAChD,OAAO,EAAE,GAAG,OAAO,EAAE,CAAC;AACvB,CAAC;AAED,2DAA2D;AAC3D,MAAM,UAAU,GAAG,CAClB,KAAQ,EACR,OAAO,GAAmB,EAA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,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,19 +5,28 @@ 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
- /** The check of a `type` name; `bytes` (a Uint8Array) extends JSON Schema for values that never pass through JSON. */
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
- /** Leaf checks that object and array loops run inline by code instead of calling them. */
11
- export declare const LEAF_CODES: ReadonlyMap<Check, number>;
14
+ /**
15
+ * A leaf check as a code and its argument, which object and array loops run by `leafOk` instead of calling the
16
+ * check: a call through a list of checks is not inlined, a switch is.
17
+ */
18
+ export type Leaf = readonly [code: number, arg: unknown];
19
+ /** The leaf form of `check`, undefined when it is not a leaf. */
20
+ export declare function leafOf(check: Check): Leaf | undefined;
12
21
  /** Whether a combined object or array check also checks the type: not at all, strictly, or passing other types. */
13
22
  export declare const GUARD_NONE = 0;
14
23
  export declare const GUARD_STRICT = 1;
15
24
  export declare const GUARD_LOOSE = 2;
16
25
  export type Guard = typeof GUARD_NONE | typeof GUARD_STRICT | typeof GUARD_LOOSE;
17
26
  /** 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;
27
+ export declare const CODE = 15;
28
+ export declare const REQUIRED = 16;
29
+ export declare const INHERITED = 32;
21
30
  /** All of `parts`; two and three parts get closures without a loop. */
22
31
  export declare function and(parts: readonly Check[]): Check;
23
32
  /** A check whose implementation is set later: the indirection a recursive reference goes through. */
@@ -28,16 +37,31 @@ export declare function indirect(slot: {
28
37
  export declare function scopedCheck(scope: string[], uri: string, check: Check): Check;
29
38
  /** Any of the named types. */
30
39
  export declare function typeCheck(types: readonly unknown[]): Check;
40
+ /** The bits of the named types; unknown names add none. */
41
+ export declare function typeMask(types: readonly unknown[]): number;
42
+ /** `anyOf` whose bare-type branches have the types of `mask`: those by one type test, the others by their checks. */
43
+ export declare function maskedAnyCheck(mask: number, checks: readonly Check[]): Check;
31
44
  /** A Uint8Array within the length bounds. */
32
45
  export declare function bytesCheck(min: number, max: number): Check;
33
46
  /** The number of Unicode code points of `s`: JSON Schema string lengths count code points, not UTF-16 units. */
34
47
  export declare function codePoints(s: string): number;
35
48
  /** The string keywords: length bounds, pattern and format. */
36
49
  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;
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;
41
65
  /** `inner` for objects; other values pass unless `strict`. */
42
66
  export declare function guardObject(inner: Check, strict: boolean): Check;
43
67
  /** `inner` for arrays; other values pass unless `strict`. */
@@ -82,6 +106,9 @@ export declare function notCheck(check: Check): Check;
82
106
  export declare function ifCheck(cond: Check, then: Check, otherwise: Check): Check;
83
107
  /** A union of objects tagged by a const on `key`: the tag selects the one branch that can match. */
84
108
  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. */
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
+ */
86
113
  export declare function deepEqual(a: unknown, b: unknown): boolean;
87
114
  //# sourceMappingURL=checks.d.ts.map
@@ -1 +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;A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