@schema-engine/registry 0.5.1 → 0.6.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.
Files changed (85) hide show
  1. package/README.md +98 -14
  2. package/dist/bind-registry.d.ts +68 -0
  3. package/dist/bind-registry.js +1 -0
  4. package/dist/bind-registry.js.map +7 -0
  5. package/dist/chunk-24SNVXNP.js +2 -0
  6. package/dist/chunk-24SNVXNP.js.map +7 -0
  7. package/dist/chunk-55J6XMHW.js +0 -0
  8. package/dist/chunk-55J6XMHW.js.map +7 -0
  9. package/dist/chunk-DE7GQKZP.js +3 -0
  10. package/dist/chunk-DE7GQKZP.js.map +7 -0
  11. package/dist/chunk-FMWSZRDV.js +1 -0
  12. package/dist/chunk-FMWSZRDV.js.map +7 -0
  13. package/dist/chunk-GX6PAJLU.js +1 -0
  14. package/dist/chunk-GX6PAJLU.js.map +7 -0
  15. package/dist/chunk-IYIWS4LW.js +1 -0
  16. package/dist/chunk-IYIWS4LW.js.map +7 -0
  17. package/dist/chunk-LG33VSKX.js +1 -0
  18. package/dist/chunk-LG33VSKX.js.map +7 -0
  19. package/dist/chunk-NSLF54GM.js +1 -0
  20. package/dist/chunk-NSLF54GM.js.map +7 -0
  21. package/dist/chunk-O5CRFISG.js +1 -0
  22. package/dist/chunk-O5CRFISG.js.map +7 -0
  23. package/dist/chunk-P3DDHPDA.js +1 -0
  24. package/dist/chunk-P3DDHPDA.js.map +7 -0
  25. package/dist/chunk-QHVQ4S4D.js +1 -0
  26. package/dist/chunk-QHVQ4S4D.js.map +7 -0
  27. package/dist/chunk-RVS7JY2Y.js +1 -0
  28. package/dist/chunk-RVS7JY2Y.js.map +7 -0
  29. package/dist/chunk-UH3CEF45.js +1 -0
  30. package/dist/chunk-UH3CEF45.js.map +7 -0
  31. package/dist/chunk-XG4EZ5VP.js +1 -0
  32. package/dist/chunk-XG4EZ5VP.js.map +7 -0
  33. package/dist/create-registry.d.ts +83 -7
  34. package/dist/create-registry.js +1 -1
  35. package/dist/create-registry.js.map +4 -4
  36. package/dist/export-tree-schema.d.ts +100 -0
  37. package/dist/export-tree-schema.js +1 -0
  38. package/dist/export-tree-schema.js.map +7 -0
  39. package/dist/format-issue-path.d.ts +12 -0
  40. package/dist/format-issue-path.js +1 -0
  41. package/dist/format-issue-path.js.map +7 -0
  42. package/dist/index.d.ts +8 -1
  43. package/dist/index.js +1 -1
  44. package/dist/index.js.map +4 -4
  45. package/dist/json-schema/embedder.d.ts +119 -0
  46. package/dist/json-schema/embedder.js +1 -0
  47. package/dist/json-schema/embedder.js.map +7 -0
  48. package/dist/json-schema/index.d.ts +5 -0
  49. package/dist/json-schema/index.js +1 -0
  50. package/dist/json-schema/index.js.map +7 -0
  51. package/dist/json-schema/slot.d.ts +46 -0
  52. package/dist/json-schema/slot.js +1 -0
  53. package/dist/json-schema/slot.js.map +7 -0
  54. package/dist/json-schema/standard.d.ts +44 -0
  55. package/dist/json-schema/standard.js +1 -0
  56. package/dist/json-schema/standard.js.map +7 -0
  57. package/dist/parse-node.d.ts +131 -0
  58. package/dist/parse-node.js +1 -0
  59. package/dist/parse-node.js.map +7 -0
  60. package/dist/parse-tree.d.ts +131 -0
  61. package/dist/parse-tree.js +1 -0
  62. package/dist/parse-tree.js.map +7 -0
  63. package/dist/registry-error.d.ts +80 -0
  64. package/dist/registry-error.js +1 -0
  65. package/dist/registry-error.js.map +7 -0
  66. package/dist/standard-schema.d.ts +19 -0
  67. package/dist/standard-schema.js +1 -1
  68. package/dist/standard-schema.js.map +4 -4
  69. package/dist/tree-types.d.ts +152 -0
  70. package/dist/tree-types.js +0 -0
  71. package/dist/tree-types.js.map +7 -0
  72. package/dist/types.d.ts +41 -0
  73. package/dist/types.js +1 -0
  74. package/dist/yaml/index.js +1 -4
  75. package/dist/yaml/index.js.map +4 -4
  76. package/dist/yaml/parse.js +1 -2
  77. package/dist/yaml/parse.js.map +4 -4
  78. package/dist/yaml/stringify.js +1 -3
  79. package/dist/yaml/stringify.js.map +4 -4
  80. package/package.json +13 -9
  81. package/skill/SKILL.md +34 -33
  82. package/skill/reference/registry.md +120 -7
  83. package/dist/base-registry.d.ts +0 -73
  84. package/dist/base-registry.js +0 -1
  85. package/dist/base-registry.js.map +0 -7
@@ -0,0 +1,119 @@
1
+ import { type JSONSchemaObject, type JSONSchemaTarget } from './standard.js';
2
+ /** Where the JSON Schema being written keeps its definitions. */
3
+ export type JSONSchemaPlacement = {
4
+ readonly kind: 'document';
5
+ } | {
6
+ readonly kind: 'collection';
7
+ /** The URI the collection refers to an entry by. */
8
+ readonly uri: (id: string) => string;
9
+ };
10
+ /** The keyword a target writes a schema's identifier under: `id` for draft-04, `$id` for every other target. */
11
+ export declare const idKeyword: (target: JSONSchemaTarget) => "id" | "$id";
12
+ /**
13
+ * Whether a schema is the root of a resource of its own, which resolves its references against itself: it sets the
14
+ * identifier of its target ({@link idKeyword}) to something other than a fragment. An identifier that is only a
15
+ * fragment (`#node`) names an anchor instead.
16
+ */
17
+ export declare const startsResource: (schema: Record<string, unknown>, target: JSONSchemaTarget) => boolean;
18
+ /** The keyword a document keeps its definitions under, as Zod writes it for the target. */
19
+ export declare const definitionsKeyword: (target: JSONSchemaTarget) => "$defs" | "definitions";
20
+ /**
21
+ * Embeds JSON Schemas that were written as documents of their own (by a Standard JSON Schema converter or a separate
22
+ * Zod conversion) into one JSON Schema being written.
23
+ */
24
+ export interface JSONSchemaEmbedder {
25
+ /** The schema to write in place of `own`, which is not changed. */
26
+ embed(own: JSONSchemaObject): JSONSchemaObject;
27
+ /**
28
+ * The schema to write as the definition named `name` of a document, or as the entry of that name of a collection,
29
+ * where `own` is not changed.
30
+ *
31
+ * @remarks
32
+ * The schema stays at its own place, so a container found by its name is the schema itself and not a reference to
33
+ * another definition. In a document, `#` points at the definition, a pointer into the schema's own definitions at the
34
+ * definition lifted to the document as `<name>.<its name>` and anchors are renamed under `name`, as
35
+ * {@link createJSONSchemaEmbedder} explains. In a collection the entry is its own resource, so the schema is kept as it is, with
36
+ * its own definitions, without `$schema`, and a root fragment identifier (`$id: '#node'`, `id: '#node'` for draft-04) is
37
+ * dropped with its references turned into `#`, since the entry is addressed by the identifier the collection gives
38
+ * it. A schema with an identifier of its own (`$id`, or `id` for draft-04) is a
39
+ * resource that keeps its base: in a document it is kept as it is, and in a collection the entry is an `allOf` of a
40
+ * reference to it, with the schema beside it under the name (a `$ref` beside other keywords is ignored before 2019-09), so the entry keeps the collection's URI and the schema keeps its
41
+ * identifier.
42
+ */
43
+ embedDefinition(own: JSONSchemaObject, name: string): JSONSchemaObject;
44
+ /**
45
+ * Adds the definitions `embed` and `embedDefinition` moved to the finished document or collection, in place.
46
+ *
47
+ * @throws {RegistryError} Code `'definition-name-clash'` when a definition `embedDefinition` lifted takes a name
48
+ * `written` already defines
49
+ * @throws {Error} When `written` already defines an `embedded-<n>` key `embed` hoisted: the embedder reserves them
50
+ */
51
+ finish(written: JSONSchemaObject): JSONSchemaObject;
52
+ }
53
+ /**
54
+ * Create the embedder for one JSON Schema being written.
55
+ *
56
+ * @param target - The target the schema is written for
57
+ * @throws {Error} When `target` is not one of the four {@link JSONSchemaTarget}s, before anything is embedded
58
+ * @param placement - Whether it is one document or a collection of entries
59
+ *
60
+ * @remarks
61
+ * `$schema` is dropped, since the document being written declares the dialect. A schema whose `$ref` points into
62
+ * itself from its root (`#`, `#/$defs/node`, as ArkType writes a recursive type) would point at the wrong root once
63
+ * embedded, so it is hoisted: in a document, into its definitions under a key of its own (`embedded-1`), with each of
64
+ * its local pointers prefixed by that place; in a collection, as an entry of its own, referred to by its URI. The
65
+ * references stay `#/...` pointers and `#name` anchors, which is what AI tool APIs resolve, rather than `$id` URIs (`id` for draft-04).
66
+ *
67
+ * A plain-name anchor (`$anchor`, `$dynamicAnchor`, a draft-07 `$id: '#node'`, a draft-04 `id: '#node'`) resolves anywhere in the document, so
68
+ * two schemas that each declare `node` would declare it twice. Each embedded schema's anchors, and the `#node`
69
+ * references to them, are renamed to carry its key (`embedded-1-node`), whether it is hoisted into a document or
70
+ * embedded where it stands. A schema hoisted into a collection is a resource of its own by its `$id` (`id` for draft-04), which scopes
71
+ * its anchors, so they keep their names. Only a root fragment identifier (`$id: '#node'`, `id: '#node'` for draft-04) cannot stay: the entry's identifier is its
72
+ * assigned URI, so the root's anchor is dropped and its `#node` references become `#`, as
73
+ * {@link withoutRootAnchor} explains.
74
+ *
75
+ * Only the schema's root resource is rewritten. A schema with its own `$id` (`id` for draft-04), and any subschema that sets one, resolves
76
+ * its pointers and anchors against that and is left as it is.
77
+ *
78
+ * A definition written into a document by `embedDefinition(own, owner)` is named, and its anchors renamed, by the
79
+ * owner, so that no two owners share a name:
80
+ *
81
+ * - A definition `name` of the schema's own `$defs` (`definitions` for a draft-07 target) is lifted to
82
+ * `<owner>.<name>`, with the owner written so that it holds no `.`: each `.` and `_` in it is replaced by `_` and its
83
+ * code point in hex and `_` again (`_2e_`, `_5f_`), any other character stays. Owner `element:a.b` with a definition
84
+ * `c` is lifted as `element:a_2e_b.c` and owner `element:a` with `b.c` as `element:a.b.c`, so the first `.` of a
85
+ * lifted name always ends the owner.
86
+ * - An anchor `name` is renamed `<owner part>.<name>`, where the owner part is `o` followed by the owner with every
87
+ * character outside `[A-Za-z0-9-]` (`.` and `_` included) replaced by `_`, its code point in hex and `_` (`ui/button`
88
+ * becomes `oui_2f_button`, `a.b` becomes `oa_2e_b`, `my-card` stays `omy-card`). The part starts with a letter, holds
89
+ * only characters every dialect allows in an anchor, and no `.`, so the first `.` ends it, and the keys of `embed`
90
+ * (`embedded-1`) never start with `o`.
91
+ *
92
+ * In both, the owner part never holds a `.`, and a `_` in the owner is always escaped, so the first `.` ends the owner
93
+ * and its `_<hex>_` tokens decode one way. Both are one-to-one, so two owners never write the same name. A name that is still taken, by another definition
94
+ * lifted into the document or one `finish` finds in the document it completes, is a `RegistryError` with the code
95
+ * `'definition-name-clash'`; nothing is overwritten.
96
+ */
97
+ export declare function createJSONSchemaEmbedder(target: JSONSchemaTarget, placement: JSONSchemaPlacement): JSONSchemaEmbedder;
98
+ /**
99
+ * A JSON Pointer reference token, written for a URI fragment: `~` and `/` escaped, then percent-encoded for what a
100
+ * fragment does not allow.
101
+ *
102
+ * @remarks
103
+ * RFC 3986 lets a fragment hold the `pchar` characters raw, so `:`, `@` and the sub-delimiters (`$&+,;=` and `!'()*`)
104
+ * stay as they are and a name like `element:tab` reads as written.
105
+ */
106
+ export declare function pointerToken(name: string): string;
107
+ /**
108
+ * The `$ref` to the definition of a name in a document written for a target: `#/$defs/<name>` for `'draft-2020-12'`,
109
+ * `#/definitions/<name>` for every other target, with the name written as a JSON Pointer token.
110
+ *
111
+ * @param target - The target the document is written for, which decides the keyword of its definitions
112
+ * @param name - The key of the definition, for example `'ui/button'`
113
+ *
114
+ * @remarks
115
+ * A name holds any character. `~` and `/` are escaped as the pointer syntax requires, and what a URI fragment does not
116
+ * allow (a space, `%`, `#`, `?`) is percent-encoded, so a tool that resolves the reference finds the key whatever the
117
+ * name is.
118
+ */
119
+ export declare function definitionRef(target: JSONSchemaTarget, name: string): string;
@@ -0,0 +1 @@
1
+ import{a,b,c,d,e,f}from"../chunk-NSLF54GM.js";import"../chunk-GX6PAJLU.js";import"../chunk-XG4EZ5VP.js";export{d as createJSONSchemaEmbedder,f as definitionRef,c as definitionsKeyword,a as idKeyword,e as pointerToken,b as startsResource};
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
+ }
@@ -0,0 +1,5 @@
1
+ export { createJSONSchemaEmbedder, definitionRef, definitionsKeyword, idKeyword, startsResource } from './embedder.js';
2
+ export type { JSONSchemaEmbedder, JSONSchemaPlacement } from './embedder.js';
3
+ export { readStandardJSONSchema } from './standard.js';
4
+ export { replaceChildSlot } from './slot.js';
5
+ export { assertJSONSchemaTarget } from './standard.js';
@@ -0,0 +1 @@
1
+ import{a as S}from"../chunk-FMWSZRDV.js";import{a as o,b as t,c as d,d as a,f as m}from"../chunk-NSLF54GM.js";import{a as e,b as r}from"../chunk-GX6PAJLU.js";import"../chunk-XG4EZ5VP.js";export{e as assertJSONSchemaTarget,a as createJSONSchemaEmbedder,m as definitionRef,d as definitionsKeyword,o as idKeyword,r as readStandardJSONSchema,S as replaceChildSlot,t as startsResource};
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
+ }
@@ -0,0 +1,46 @@
1
+ import { type JSONSchemaObject, type JSONSchemaTarget } from './standard.js';
2
+ /**
3
+ * Replace the slot a child path names in a registration's JSON Schema, and nothing around it. The definition is
4
+ * changed in place.
5
+ *
6
+ * @param definition - The registration's JSON Schema, as the definition of its `$type`
7
+ * @param path - The registration's `$type`, for the error, and the `keys` and `list` of the child path whose slot to
8
+ * replace
9
+ * @param union - The schema every child must match, already resolved: each `$ref` is written as the document it goes in
10
+ * refers to a definition. Each place the slot takes it gets its own copy
11
+ * @param options - The `target` the schema is written for, which decides the identifier (`$id`, or `id` for draft-04)
12
+ * that makes a schema a resource of its own
13
+ * @throws {RegistryError} Code `'unreachable-child-path'` when the slot is not where the definition declares it, is
14
+ * nullable, is not an object, an array or open, or is an object where the path sets `list`, and `'invalid-child-paths'` when `path.keys` is empty
15
+ *
16
+ * @remarks
17
+ * `required`, list bounds and every sibling keyword of the slot's parent stay, since the registration's schema owns
18
+ * them. The slot is read by what it is: an array (`type: 'array'`, `items` or `prefixItems`) gets its items replaced, an
19
+ * object (`type: 'object'` or `properties`) becomes the union, and an open slot (no keyword but annotations) becomes the
20
+ * union or a list of it, matching a child or a list of children. A path that sets `list` holds a list and never one
21
+ * node, so its open slot becomes an array of the union, and an object slot is unreachable, since the schema would refuse
22
+ * the list the document holds. An array slot of such a path that declares only array keywords (`items`) gets
23
+ * `type: 'array'`, so an object is refused as the walk refuses it, and one whose `type` is another is unreachable.
24
+ * Annotations of the slot are kept. A slot inside a
25
+ * schema with an `$id` (`id` for draft-04) of its own cannot be reached: a `$ref` there resolves against that `$id`, not against the
26
+ * document. That holds for each schema between the definition and the slot. The definition's own root is the caller's
27
+ * to check, since only the caller knows whether the references it writes resolve against that root: a collection entry
28
+ * has an `$id` that is its address, and its references are written for that collection. A nullable slot (`type: ['array', 'null']` or an `anyOf` with `null`, or `nullable: true` as OpenAPI 3.0
29
+ * writes it) cannot be reached either, since `safeParseTree` refuses `null` at a slot and keeping the keyword would
30
+ * leave the slot accepting it.
31
+ *
32
+ * A tuple is an array whose every item is a child, as `safeParseTree` reads it, so each position takes the union:
33
+ * every `prefixItems` entry (2020-12), every array-form `items` entry and `additionalItems` (draft-07), and the
34
+ * `items` that follows `prefixItems`. A rest that the schema closes with `false` stays closed, since the tuple's arity
35
+ * is the registration's to decide. Throwing instead would refuse a fixed-arity pair of children that is valid, while
36
+ * replacing only `items` would leave the prefix positions accepting any `$type`.
37
+ *
38
+ * The check that the union's names are registered belongs to the caller, as it does in `exportTreeSchema`.
39
+ */
40
+ export declare function replaceChildSlot(definition: JSONSchemaObject, path: {
41
+ readonly $type: string;
42
+ readonly keys: readonly string[];
43
+ readonly list?: true;
44
+ }, union: JSONSchemaObject, options: {
45
+ readonly target: JSONSchemaTarget;
46
+ }): void;
@@ -0,0 +1 @@
1
+ import{a}from"../chunk-FMWSZRDV.js";import"../chunk-NSLF54GM.js";import"../chunk-GX6PAJLU.js";import"../chunk-XG4EZ5VP.js";export{a as replaceChildSlot};
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
+ }
@@ -0,0 +1,44 @@
1
+ import type { StandardJSONSchemaV1 } from '@standard-schema/spec';
2
+ /**
3
+ * A JSON Schema as a plain object.
4
+ *
5
+ * @remarks
6
+ * The export reads and writes schemas of any dialect, so the type names no keyword: callers narrow what they inspect.
7
+ */
8
+ export type JSONSchemaObject = Record<string, unknown>;
9
+ declare const JSON_SCHEMA_TARGETS: readonly ["draft-2020-12", "draft-07", "draft-04", "openapi-3.0"];
10
+ /**
11
+ * The JSON Schema dialects a schema can be written for, by their Standard JSON Schema names.
12
+ *
13
+ * @remarks
14
+ * The set is closed, though the Standard JSON Schema type is open, so that each API names the targets it supports and
15
+ * refuses the others. A target added later widens the union, which is additive.
16
+ */
17
+ export type JSONSchemaTarget = (typeof JSON_SCHEMA_TARGETS)[number];
18
+ /**
19
+ * Throw unless `target` is a {@link JSONSchemaTarget}, and narrow it to one.
20
+ *
21
+ * @param target - A target as a caller wrote it, for example the `target` of a Standard JSON Schema converter's options,
22
+ * which the Standard JSON Schema type leaves open
23
+ * @throws {Error} When `target` is any other string, naming it and the four that are supported
24
+ */
25
+ export declare function assertJSONSchemaTarget(target: string): asserts target is JSONSchemaTarget;
26
+ /**
27
+ * The Standard JSON Schema converter of a schema (`~standard.jsonSchema`), when it provides one.
28
+ *
29
+ * @remarks
30
+ * {@link exportTreeSchema} reads each registration's schema through it unless the caller passes its own `resolve`.
31
+ */
32
+ export declare function readStandardJSONSchema(schema: unknown): StandardJSONSchemaV1.Converter | undefined;
33
+ /** Only ordinary records and null-prototype value maps are walked as schemas. */
34
+ export declare function isPlainObject(value: unknown): value is Record<string, unknown>;
35
+ /**
36
+ * The own entry of `record` under `key`, or `undefined`.
37
+ *
38
+ * @remarks
39
+ * A child path key is data, and `__proto__` is a valid key. Reading and writing keys through these two functions keeps
40
+ * every one on the document's own properties, so none reaches the prototype chain, whatever its name.
41
+ */
42
+ export declare function getOwn(record: Record<string, unknown>, key: string): unknown;
43
+ export declare function setOwn(record: Record<string, unknown>, key: string, value: unknown): void;
44
+ export {};
@@ -0,0 +1 @@
1
+ import{a,b,c,d,e}from"../chunk-GX6PAJLU.js";export{a as assertJSONSchemaTarget,d as getOwn,c as isPlainObject,b as readStandardJSONSchema,e as setOwn};
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
+ }
@@ -0,0 +1,131 @@
1
+ import type { StandardSchemaV1 } from '@standard-schema/spec';
2
+ import { type StandardSchemaIssue } from './standard-schema.js';
3
+ import type { BaseRegistration } from './types.js';
4
+ /**
5
+ * What {@link safeParseNode} needs of a registry: the registration of a `$type`, of which it reads the schema.
6
+ * Both registry kinds fit.
7
+ */
8
+ export interface NodeLookup<TRegistration extends Pick<BaseRegistration, 'schema'> = BaseRegistration> {
9
+ get(key: string): TRegistration | undefined;
10
+ /**
11
+ * The registry's label, for example `Widget`, as it is used in errors.
12
+ *
13
+ * @remarks
14
+ * {@link safeParseTree} names it in the `RegistryError` it throws when a schema contradicts its registration, and
15
+ * leaves it out for a lookup that has none, such as a function that tries several registries.
16
+ */
17
+ readonly kind?: string;
18
+ /**
19
+ * A sentence that follows the message of a `$type` `get` does not answer, such as where to register it.
20
+ *
21
+ * @remarks
22
+ * The message is `No registration for $type 'x'.`, a space and the hint. A registry built with the `hint` option
23
+ * has it.
24
+ */
25
+ hint?: string;
26
+ }
27
+ /**
28
+ * What {@link safeParseNode} returns: the registration with the node's validated configuration, or every issue found.
29
+ *
30
+ * @remarks
31
+ * The success branch has one member per registration of a typed registry, so checking `$type` (the node's, repeated at
32
+ * the top of the result because TypeScript narrows a union only by a property of the union itself) narrows
33
+ * `registration` and `data` together. A registration that does not declare its `$type`, such as a lookup typed by
34
+ * `{ schema }` alone, leaves it a `string`. A registry typed as plain `BaseRegistration` has no concrete schema to
35
+ * read, so its `data` is `unknown`.
36
+ */
37
+ export type SafeParseNodeResult<TRegistration extends Pick<BaseRegistration, 'schema'> = BaseRegistration> = (TRegistration extends unknown ? {
38
+ success: true;
39
+ $type: TRegistration extends {
40
+ $type: infer TType extends string;
41
+ } ? TType : string;
42
+ registration: TRegistration;
43
+ data: StandardSchemaV1.InferOutput<TRegistration['schema']>;
44
+ } : never) | {
45
+ success: false;
46
+ issues: RegistryIssue[];
47
+ };
48
+ /**
49
+ * The message for a `$type` no registration answers, as {@link safeParseNode} reports it.
50
+ *
51
+ * @param type - The `$type` that found nothing
52
+ * @param hint - A sentence that follows the message, as {@link NodeLookup.hint} gives it
53
+ *
54
+ * @remarks
55
+ * Exported so a caller that has to throw for the same case, instead of returning an issue, says the same thing.
56
+ */
57
+ export declare function unregisteredTypeMessage(type: string, hint?: string): string;
58
+ /**
59
+ * An issue of a node or a document, as {@link safeParseNode} and {@link safeParseTree} report it.
60
+ *
61
+ * @remarks
62
+ * The issues the engine creates itself carry a namespaced `code`, so a consumer can branch on it and a schema library's
63
+ * own `code`, which an issue from a schema keeps untouched, cannot be mistaken for one:
64
+ *
65
+ * - `'registry/not-an-object'`: the node is not an object.
66
+ * - `'registry/missing-type'`: the node has no string `$type`.
67
+ * - `'registry/unregistered-type'`: no registration answers the `$type` (`received`).
68
+ * - `'registry/not-accepted'`: the `$type` (`received`) is not in the `accepts` list (`expected`) of its position.
69
+ * - `'registry/not-a-node'`: a value at a child path is neither an object nor a list.
70
+ * - `'registry/not-a-list'`: a value at a child path that sets `list` is not an array.
71
+ * - `'registry/contains-itself'`: a node is reached again through its own ancestors.
72
+ *
73
+ * The messages are written for a person and may be reworded; the codes stay.
74
+ */
75
+ export interface RegistryIssue extends StandardSchemaIssue {
76
+ /** The namespaced code of an issue the engine created, or whatever the schema library put on its own issue. */
77
+ readonly code?: string;
78
+ /**
79
+ * What would have fitted, for `'registry/not-accepted'`: the `accepts` list.
80
+ *
81
+ * @remarks
82
+ * Set only on an issue the engine created. An issue from a schema keeps its library's own fields untouched, and some
83
+ * libraries have an `expected` or `received` of their own, so check `code` first.
84
+ */
85
+ readonly expected?: readonly string[];
86
+ /** What was there, for `'registry/not-accepted'` and `'registry/unregistered-type'`: the `$type`. */
87
+ readonly received?: string;
88
+ }
89
+ /**
90
+ * What a call of {@link safeParseNode} or {@link safeParseTree} may be told besides the node.
91
+ *
92
+ * @remarks
93
+ * For {@link safeParseTree}, `TType` is the `$type`s of the lookup, so a name it lacks is a type error and a literal
94
+ * `accepts` narrows the root.
95
+ */
96
+ export interface ParseOptions<TType extends string = string> {
97
+ /** Where the node sits in its document. Every issue's `path` starts with it. */
98
+ readonly path?: NonNullable<StandardSchemaIssue['path']>;
99
+ /**
100
+ * The `$type`s the node may have, as a position of a document accepts them. Not empty when present, or the call
101
+ * throws: an empty list is a caller bug, like an asynchronous schema.
102
+ */
103
+ readonly accepts?: readonly TType[];
104
+ }
105
+ /**
106
+ * Check one node against the registration its `$type` names, without throwing.
107
+ *
108
+ * @param registry - Where the node's `$type` finds its registration
109
+ * @param node - The node as it arrived, for example one entry of a parsed JSON document
110
+ * @param options - Where the node sits (`path`) and which `$type`s it may have (`accepts`), see {@link ParseOptions}
111
+ *
112
+ * @remarks
113
+ * An issue points at the value that is wrong, as a schema reports it, so an editor can mark that value and a YAML
114
+ * mapper finds its line: a node that is not an object is an issue at `path`; an object without a string `$type`, a
115
+ * `$type` outside `accepts` and a `$type` no registration answers are issues at `[...path, '$type']`, where a schema
116
+ * reports a missing required key. `accepts` is checked before the lookup, so a `$type` outside the list is not
117
+ * accepted whether or not it is registered, and the issue names what would fit. The lookup's `hint` adds to the message
118
+ * of an unregistered `$type` only.
119
+ * Otherwise the issues are those of the registration's schema, with `path` in front of the path the schema reported.
120
+ *
121
+ * `data` is what the schema returned, so its defaults and transforms apply. Its type is the schema output of the
122
+ * registration that `registration.$type` names, for a registry typed by registrations with literal `$type`s, and
123
+ * `unknown` for one typed as `BaseRegistration`. TypeScript cannot follow the runtime lookup from the node's string
124
+ * `$type` to a registration, so the function returns `data` through one cast to that type: it is the value the matched
125
+ * registration's own schema returned. The node is checked on its own, whatever its `childPaths` say, and its schema
126
+ * may omit `$type`: {@link safeParseTree} walks a tree with it and holds the schema to the registration's contract.
127
+ *
128
+ * @throws {Error} When the registration's schema validates asynchronously, as {@link safeParseStandardSchema} does,
129
+ * and when `accepts` is an empty list.
130
+ */
131
+ export declare function safeParseNode<TRegistration extends Pick<BaseRegistration, 'schema'>>(registry: NodeLookup<TRegistration>, node: unknown, options?: ParseOptions): SafeParseNodeResult<TRegistration>;
@@ -0,0 +1 @@
1
+ import{a,b}from"./chunk-O5CRFISG.js";import"./chunk-P3DDHPDA.js";export{b as safeParseNode,a as unregisteredTypeMessage};
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
+ }
@@ -0,0 +1,131 @@
1
+ import type { StandardSchemaV1 } from '@standard-schema/spec';
2
+ import { type NodeLookup, type ParseOptions, type RegistryIssue } from './parse-node.js';
3
+ import type { Accepted, AcceptedOf, CheckLookup, NodeData, TreeRegistration } from './tree-types.js';
4
+ import type { StandardSchemaIssue } from './standard-schema.js';
5
+ import type { ChildPath } from './types.js';
6
+ /**
7
+ * One node of a validated document: its registration, its data, where it sits and its children.
8
+ *
9
+ * @remarks
10
+ * `$type` is the node's own, repeated at the top for the same reason as on {@link SafeParseNodeResult}: checking it
11
+ * narrows `registration` and `data` together. `data` is the node's composed data: the schema output with each child
12
+ * path replaced by the data of the children found there, so it is the same object the parent's data holds at that
13
+ * position, and `tree.data` is the whole parsed document. `children` has one entry per child path the registration
14
+ * declares, in declaration order, each `{ keys, nodes }` with `nodes` empty when the node holds none there, in document
15
+ * order. Find an entry by its `keys`, compared key by key and never by index or by a joined string (`['a.b']` and
16
+ * `['a', 'b']` are two paths), or read `data`, which is typed per path. `TAll` is the whole union of registrations, so a child is typed by what its
17
+ * path accepts, or by the whole union without `accepts`.
18
+ */
19
+ export type ParsedNode<TAll extends TreeRegistration, TRegistration extends TreeRegistration = TAll> = TRegistration extends unknown ? {
20
+ $type: TRegistration['$type'];
21
+ registration: TRegistration;
22
+ data: NodeData<TAll, TRegistration>;
23
+ path: NonNullable<StandardSchemaIssue['path']>;
24
+ children: ParsedChildren<TAll, TRegistration['childPaths']>;
25
+ } : never;
26
+ /** The `children` of a node: a tuple over the declared child paths, each `nodes` typed by its `accepts`. */
27
+ type ParsedChildren<TAll extends TreeRegistration, TPaths> = TPaths extends readonly ChildPath[] ? {
28
+ readonly [I in keyof TPaths]: TPaths[I] extends ChildPath ? {
29
+ keys: TPaths[I]['keys'];
30
+ nodes: readonly ParsedNode<TAll, Accepted<TAll, TPaths[I]>>[];
31
+ } : never;
32
+ } : readonly [];
33
+ /**
34
+ * One node of a document that has issues: its registration, its own schema output, where it sits and the children that
35
+ * parsed.
36
+ *
37
+ * @remarks
38
+ * `output` is exactly what {@link safeParseNode} returned for the node. No child data is written into it, so a child
39
+ * slot holds whatever the parent's schema returned there (the authored items, for an opaque slot), including children
40
+ * that failed or were pruned. Read the children from `children`, never from a slot of `output`. There is no `data`:
41
+ * the composed document exists only when there are no issues, as `data` of a {@link ParsedNode}. `children` has one
42
+ * `{ keys, nodes }` entry per child path the registration declares, in declaration order, with `nodes` holding the
43
+ * children that parsed in document order, each with its own `path`. A child that failed is missing, so the issues, not
44
+ * the position in `nodes`, say where it was. A child is any `PartialNode<TAll>`, narrowed by its `$type`, not by
45
+ * `accepts`.
46
+ */
47
+ export type PartialNode<TAll extends TreeRegistration, TRegistration extends TreeRegistration = TAll> = TRegistration extends unknown ? {
48
+ $type: TRegistration['$type'];
49
+ registration: TRegistration;
50
+ output: StandardSchemaV1.InferOutput<TRegistration['schema']>;
51
+ path: NonNullable<StandardSchemaIssue['path']>;
52
+ children: readonly {
53
+ keys: readonly string[];
54
+ nodes: readonly PartialNode<TAll>[];
55
+ }[];
56
+ } : never;
57
+ /**
58
+ * What {@link safeParseTree} returns: the tree of a document with no issue, or every issue found and the nodes that
59
+ * parsed.
60
+ *
61
+ * @remarks
62
+ * `tree` on the failure branch is present exactly when the root's own schema passed, and is absent when the root
63
+ * failed, was not accepted or has no registration.
64
+ */
65
+ export type SafeParseTreeResult<TAll extends TreeRegistration, TRoot extends TreeRegistration = TAll> = {
66
+ success: true;
67
+ tree: ParsedNode<TAll, TRoot>;
68
+ } | {
69
+ success: false;
70
+ issues: RegistryIssue[];
71
+ tree?: PartialNode<TAll, TRoot>;
72
+ };
73
+ /**
74
+ * The registrations a root may be: those `accepts` names, but only when the options type has `accepts`, not when it
75
+ * only may.
76
+ */
77
+ type RootOf<TAll extends TreeRegistration, TOptions> = TOptions extends {
78
+ readonly accepts: readonly (infer TName)[];
79
+ } ? AcceptedOf<TAll, TName> : TAll;
80
+ /**
81
+ * Check a whole document, node by node, without throwing for a bad document.
82
+ *
83
+ * @param registry - Where each node's `$type` finds its registration
84
+ * @param root - The document's root node, as it arrived
85
+ * @param options - Where the root sits in a larger document (`path`, which starts every issue's `path`) and which
86
+ * `$type`s the root may have (`accepts`), see {@link ParseOptions}
87
+ *
88
+ * @remarks
89
+ * Each node goes through {@link safeParseNode} with its full path, and `accepts` of the position it sits in (the
90
+ * option for the root, the child path's own below), so the issues are those of one node, at the position the document
91
+ * has them. A registration says where its children are with `childPaths`, each path read from the node as authored:
92
+ *
93
+ * | Value at the child path | Result |
94
+ * | --- | --- |
95
+ * | key absent, or an intermediate absent or not an object | no children; `data` keeps the schema output there |
96
+ * | `undefined` | no children, as absent |
97
+ * | `null` or another primitive | issue `registry/not-a-node` at the path |
98
+ * | an object | one child |
99
+ * | an array | each item is a child, at `[...path, index]`; an item that is not an object is an issue |
100
+ * | an empty array | no children; `data` holds `[]` |
101
+ * | anything but an array or a missing value, on a path that sets `list` | issue `registry/not-a-list` at the path |
102
+ *
103
+ * A node that contains itself is `registry/contains-itself`. A node whose own schema fails is not walked further, but
104
+ * its siblings still are, so one call reports every issue of the document. When there are none, `tree.data` is the
105
+ * parsed document, each node's schema output with the children's data at its child paths, in document order. When
106
+ * there are issues, `tree` is a {@link PartialNode} of the nodes that parsed, present exactly when the root's own
107
+ * schema passed: every node whose own schema passed and whose parent was kept, with `output` and `children`, and
108
+ * without `data`. A failed node, a child the slot does not accept and the edge of a cycle are left out. Each child is
109
+ * parsed once, from the authored value, and the authored document and the schema outputs are never written to.
110
+ *
111
+ * The parent schema owns, at a child path, whether the value may be missing, list or single, and list bounds; it does
112
+ * not own the children, whose data replaces whatever the schema returned there. For each node it parses, `safeParseTree`
113
+ * holds the schema to that contract and throws a {@link RegistryError} when it breaks it, since that is a bug of the
114
+ * registration and no issue of the document: `'schema-changes-type'` when the output is not a plain object whose own
115
+ * `$type` equals the node's, and `'schema-breaks-child-path'` when, where the document fills a child path, the output
116
+ * lacks the keys down to it as plain objects or the same cardinality (an array of the same length for an array, an
117
+ * object for an object), or when, where the document leaves a path empty, the output holds anything but nothing,
118
+ * `undefined` or `[]`: a default that holds nodes. A transform of the parent at a filled slot (a reorder, say) is
119
+ * discarded, not detected, so slots stay opaque to the parent's schema.
120
+ *
121
+ * The result is typed from the registrations: with a literal `accepts` the root narrows, and `tree.data` types each
122
+ * child path by what it accepts. The walk itself reads registrations through one widened view of the lookup, and
123
+ * the function returns the tree through one cast to that type: every node in it was built from a registration of the
124
+ * lookup by its own `$type`, which TypeScript cannot follow at runtime.
125
+ *
126
+ * @throws {RegistryError} Code `'schema-changes-type'` or `'schema-breaks-child-path'`, as above
127
+ * @throws {Error} When a registration's schema validates asynchronously, as {@link safeParseNode} does, and when
128
+ * `accepts` is an empty list
129
+ */
130
+ export declare function safeParseTree<TAll extends TreeRegistration, const TOptions extends ParseOptions<TAll['$type']> = ParseOptions<TAll['$type']>>(registry: NodeLookup<TAll> & CheckLookup<TAll>, root: unknown, options?: TOptions): SafeParseTreeResult<TAll, RootOf<TAll, TOptions>>;
131
+ export {};
@@ -0,0 +1 @@
1
+ import{a}from"./chunk-QHVQ4S4D.js";import"./chunk-O5CRFISG.js";import"./chunk-P3DDHPDA.js";import"./chunk-XG4EZ5VP.js";export{a as safeParseTree};
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
+ }
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Why building or binding a registry, or parsing a tree, failed. Each code names one mistake in the registrations or
3
+ * bindings.
4
+ *
5
+ * @remarks
6
+ * The codes are the contract: depend on a code, not on the wording of `message`, which may change. A code is only
7
+ * added, never renamed or reused for another mistake.
8
+ *
9
+ * - `'invalid-schema'`: a registration's `schema` has no `~standard.validate` function.
10
+ * - `'invalid-child-paths'`: a registration's `childPaths` is not a list, or holds an entry that is not an object
11
+ * `{ keys, accepts?, list? }` with a non-empty list of non-empty string `keys`, no other property, an `accepts` that
12
+ * is absent or a non-empty list of non-empty strings without repeats, and a `list` that is absent or `true`.
13
+ * `replaceChildSlot` throws it too for a path with no keys. A domain package throws it too for a `childPaths` its own
14
+ * rules refuse.
15
+ * - `'duplicate-child-path'`: a registration declares the same child path (`childPath`) twice, key by key.
16
+ * - `'nested-child-path'`: a child path (`childPath`) runs through another child path (`outerChildPath`) of the same
17
+ * registration.
18
+ * - `'duplicate-type'`: a `$type` is registered twice while `duplicates` is `'error'`.
19
+ * - `'unknown-binding'`: {@link bindRegistry} got a binding whose key (`$type`) is no contract's `$type`.
20
+ * - `'missing-binding'`: {@link bindRegistry} got no binding for a contract.
21
+ * - `'binding-overrides-contract'`: a binding sets a key (`key`) its contract already defines.
22
+ * - `'schema-changes-type'`: {@link safeParseTree} parsed a node whose schema returned an output with another `$type`
23
+ * than the node's, or none.
24
+ * - `'schema-breaks-child-path'`: {@link safeParseTree} parsed a node whose schema returned an output that does not
25
+ * keep a child path (`childPath`) the document fills, or fills one the document leaves empty.
26
+ * - `'unreachable-child-path'`: {@link exportTreeSchema} found no slot for a child path (`childPath`) in the JSON
27
+ * Schema of a registration, because the schema does not declare its keys, or it sits behind `allOf`, a `$ref` or a
28
+ * union, or inside a schema with an `$id` of its own, or because the slot is nullable.
29
+ * - `'unknown-accepted-type'`: {@link exportTreeSchema} was given a lookup that lacks a `$type` a child path
30
+ * (`childPath`) accepts (`key`).
31
+ * - `'definition-name-clash'`: {@link exportTreeSchema}, or the JSON Schema embedder it writes through, found that a
32
+ * definition a schema brought along (`key`, brought by `$type`) takes the name of a `$type` or of another definition.
33
+ * - `'invalid-declaration'`: a domain package refuses a declaration of a registration other than its child paths.
34
+ */
35
+ export type RegistryErrorCode = 'invalid-schema' | 'invalid-child-paths' | 'duplicate-child-path' | 'nested-child-path' | 'duplicate-type' | 'unknown-binding' | 'missing-binding' | 'binding-overrides-contract' | 'schema-changes-type' | 'schema-breaks-child-path' | 'unreachable-child-path' | 'unknown-accepted-type' | 'definition-name-clash' | 'invalid-declaration';
36
+ /** Where a {@link RegistryError} happened, and what its code needs to be described without the message. */
37
+ export interface RegistryErrorDetails {
38
+ /**
39
+ * The registry's label, for example `Widget`.
40
+ *
41
+ * @remarks
42
+ * Absent when a parse failed through a lookup that is not a registry, a function say, which has no label.
43
+ */
44
+ readonly kind?: string;
45
+ /** The `$type` of the registration at fault, or the key of a binding that names no registration. */
46
+ readonly $type: string;
47
+ /**
48
+ * The key a binding sets that its contract defines, for `'binding-overrides-contract'`, the `$type` a child path
49
+ * accepts that the lookup lacks, for `'unknown-accepted-type'`, and the name of the definition that clashes, for
50
+ * `'definition-name-clash'`.
51
+ */
52
+ readonly key?: string;
53
+ /**
54
+ * The keys of the child path at fault, for `'duplicate-child-path'`, `'nested-child-path'` and
55
+ * `'schema-breaks-child-path'`, `'unreachable-child-path'` and `'unknown-accepted-type'`.
56
+ */
57
+ readonly childPath?: readonly string[];
58
+ /** The keys of the child path that `childPath` runs through, for `'nested-child-path'`. */
59
+ readonly outerChildPath?: readonly string[];
60
+ }
61
+ /**
62
+ * Thrown while a registry is built ({@link createRegistry}) or bound ({@link bindRegistry}) from registrations that
63
+ * break a rule, and while a tree is parsed ({@link safeParseTree}) when a schema contradicts its registration.
64
+ *
65
+ * @remarks
66
+ * Branch on `code`, not on `message`: the message is written for a person and may be reworded, while the codes of
67
+ * {@link RegistryErrorCode} stay. `code` with the details is enough to write the message again, in another language
68
+ * say, without parsing it. A parse throws a `RegistryError` only when a schema contradicts its registration, which is
69
+ * a bug of the registration and no issue of the document. Other errors raised while parsing a node or a document (an
70
+ * asynchronous Standard Schema, say), and by {@link assertStandardSchema} on its own, are not `RegistryError`s.
71
+ */
72
+ export declare class RegistryError extends Error implements RegistryErrorDetails {
73
+ readonly code: RegistryErrorCode;
74
+ readonly kind?: string;
75
+ readonly $type: string;
76
+ readonly key?: string;
77
+ readonly childPath?: readonly string[];
78
+ readonly outerChildPath?: readonly string[];
79
+ constructor(code: RegistryErrorCode, message: string, details: RegistryErrorDetails);
80
+ }
@@ -0,0 +1 @@
1
+ import{a}from"./chunk-XG4EZ5VP.js";export{a as RegistryError};