@astrale-os/kernel-dsl 1.0.0-beta.12 → 1.0.0-beta.14

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 (152) hide show
  1. package/dist/v1/addressing/definition-ref.js +21 -37
  2. package/dist/v1/addressing/key.js +2 -1
  3. package/dist/v1/addressing/ordering.d.ts +2 -0
  4. package/dist/v1/addressing/ordering.js +4 -0
  5. package/dist/v1/addressing/patterns.d.ts +1 -1
  6. package/dist/v1/addressing/patterns.js +2 -6
  7. package/dist/v1/builder/class/authoring.js +5 -28
  8. package/dist/v1/builder/class/property.d.ts +3 -9
  9. package/dist/v1/builder/class/property.js +1 -0
  10. package/dist/v1/builder/lowering/class.js +5 -1
  11. package/dist/v1/builder/lowering/policy.js +1 -0
  12. package/dist/v1/builder/lowering/schema/context.js +2 -4
  13. package/dist/v1/builder/lowering/value/path.js +2 -4
  14. package/dist/v1/builder/lowering/value/zod.js +1 -3
  15. package/dist/v1/builder/policy/authoring.js +3 -2
  16. package/dist/v1/builder/schema/define.d.ts +0 -1
  17. package/dist/v1/builder/schema/diagnostic.d.ts +2 -1
  18. package/dist/v1/builder/schema/diagnostic.js +5 -10
  19. package/dist/v1/builder/schema/projection.d.ts +1 -1
  20. package/dist/v1/builder/value/full-match.d.ts +0 -1
  21. package/dist/v1/builder/value/full-match.js +0 -1
  22. package/dist/v1/bundle/codec.js +4 -6
  23. package/dist/v1/bundle/diagnostic.d.ts +2 -0
  24. package/dist/v1/bundle/diagnostic.js +9 -7
  25. package/dist/v1/bundle/structure.js +4 -6
  26. package/dist/v1/compiled/admission/diagnostic.d.ts +1 -1
  27. package/dist/v1/compiled/admission/diagnostic.js +1 -3
  28. package/dist/v1/compiled/admission/revision.d.ts +9 -1
  29. package/dist/v1/compiled/admission/revision.js +6 -3
  30. package/dist/v1/compiled/admission/semantics/class.d.ts +4 -1
  31. package/dist/v1/compiled/admission/semantics/class.js +4 -8
  32. package/dist/v1/compiled/admission/semantics/core.d.ts +1 -24
  33. package/dist/v1/compiled/admission/semantics/core.js +54 -34
  34. package/dist/v1/compiled/admission/semantics/edge.d.ts +2 -4
  35. package/dist/v1/compiled/admission/semantics/edge.js +6 -65
  36. package/dist/v1/compiled/admission/semantics/policy/callable.js +0 -2
  37. package/dist/v1/compiled/admission/semantics/policy/contracts.d.ts +5 -20
  38. package/dist/v1/compiled/admission/semantics/policy/contracts.js +8 -21
  39. package/dist/v1/compiled/admission/semantics/policy/parity.js +13 -22
  40. package/dist/v1/compiled/admission/semantics/policy/pattern.d.ts +4 -1
  41. package/dist/v1/compiled/admission/semantics/policy/pattern.js +4 -16
  42. package/dist/v1/compiled/admission/semantics/policy/typing.d.ts +2 -24
  43. package/dist/v1/compiled/admission/semantics/policy/typing.js +19 -201
  44. package/dist/v1/compiled/compiler/compile.js +6 -8
  45. package/dist/v1/compiled/diagnostic.js +2 -3
  46. package/dist/v1/compiled/loader/load.d.ts +3 -2
  47. package/dist/v1/compiled/loader/load.js +10 -44
  48. package/dist/v1/domain/create.js +3 -5
  49. package/dist/v1/domain/namespace/member-collection.js +4 -6
  50. package/dist/v1/domain/validation/create.d.ts +4 -2
  51. package/dist/v1/domain/validation/create.js +21 -69
  52. package/dist/v1/language/.spec/schemas/value-schema.schema.json +22 -0
  53. package/dist/v1/language/class/inheritance.d.ts +18 -0
  54. package/dist/v1/language/class/inheritance.js +73 -0
  55. package/dist/v1/language/class/model/class.d.ts +4 -4
  56. package/dist/v1/language/class/model/edge.d.ts +2 -2
  57. package/dist/v1/language/class/model/property.d.ts +16 -7
  58. package/dist/v1/language/class/property-values.js +2 -4
  59. package/dist/v1/language/core/facts.d.ts +72 -0
  60. package/dist/v1/language/core/facts.js +95 -0
  61. package/dist/v1/language/definition/metadata.d.ts +5 -0
  62. package/dist/v1/language/definition/metadata.js +7 -0
  63. package/dist/v1/language/policy/analysis/branches.d.ts +8 -0
  64. package/dist/v1/language/policy/analysis/branches.js +2 -1
  65. package/dist/v1/language/policy/analysis/budgets.d.ts +13 -1
  66. package/dist/v1/language/policy/analysis/budgets.js +104 -0
  67. package/dist/v1/language/policy/analysis/typing.d.ts +48 -0
  68. package/dist/v1/language/policy/analysis/typing.js +338 -0
  69. package/dist/v1/language/policy/model/policy.d.ts +5 -5
  70. package/dist/v1/language/policy/normalization/normalize.js +17 -16
  71. package/dist/v1/language/validation/diagnostic.d.ts +15 -1
  72. package/dist/v1/language/validation/diagnostic.js +21 -0
  73. package/dist/v1/language/validation/rule.d.ts +26 -0
  74. package/dist/v1/language/validation/rule.js +116 -0
  75. package/dist/v1/language/value/compile/fields.js +4 -8
  76. package/dist/v1/language/value/profile/meta-schema.compiled.js +4 -1
  77. package/dist/v1/language/value/profile/rules.js +17 -43
  78. package/dist/v1/language/value/profile/validate.js +1 -10
  79. package/dist/v1/language/value/profile/vocabulary.d.ts +35 -2
  80. package/dist/v1/language/value/profile/vocabulary.js +52 -5
  81. package/dist/v1/language/value/reference/recursion.js +2 -22
  82. package/dist/v1/language/value/reference/registry.index.js +2 -1
  83. package/dist/v1/language/value/syntax/keywords.d.ts +3 -3
  84. package/dist/v1/language/value/syntax/keywords.js +4 -20
  85. package/dist/v1/language/value/syntax/node-path/validate.js +2 -3
  86. package/dist/v1/language/value/syntax/pointer.d.ts +2 -0
  87. package/dist/v1/language/value/syntax/pointer.js +5 -2
  88. package/dist/v1/language/value/syntax/walk.d.ts +2 -0
  89. package/dist/v1/language/value/syntax/walk.js +19 -20
  90. package/dist/v1/schema/.spec/schemas/domain-schema.schema.json +2336 -0
  91. package/dist/v1/schema/analysis/class.js +33 -15
  92. package/dist/v1/schema/compatibility/api.d.ts +1 -1
  93. package/dist/v1/schema/compatibility/coordinate.js +3 -3
  94. package/dist/v1/schema/compatibility/diff.js +2 -4
  95. package/dist/v1/schema/compatibility/meaning/footprint.js +16 -50
  96. package/dist/v1/schema/compatibility/model.d.ts +5 -0
  97. package/dist/v1/schema/compatibility/structure/requirements.js +26 -58
  98. package/dist/v1/schema/compatibility/walk.d.ts +17 -0
  99. package/dist/v1/schema/compatibility/walk.js +41 -0
  100. package/dist/v1/schema/diagnostic.d.ts +17 -3
  101. package/dist/v1/schema/diagnostic.js +14 -11
  102. package/dist/v1/schema/index.d.ts +3 -1
  103. package/dist/v1/schema/index.js +1 -0
  104. package/dist/v1/schema/model/schema.d.ts +19 -1
  105. package/dist/v1/schema/normalization/domain.js +1 -0
  106. package/dist/v1/schema/resolution/closure.js +2 -1
  107. package/dist/v1/schema/resolution/context.js +6 -6
  108. package/dist/v1/schema/rule.d.ts +29 -0
  109. package/dist/v1/schema/rule.js +415 -0
  110. package/dist/v1/schema/validation/callable.d.ts +4 -1
  111. package/dist/v1/schema/validation/callable.js +13 -6
  112. package/dist/v1/schema/validation/class.js +46 -96
  113. package/dist/v1/schema/validation/core.js +68 -202
  114. package/dist/v1/schema/validation/domain.js +1 -10
  115. package/dist/v1/schema/validation/domain.json-schema.d.ts +15 -0
  116. package/dist/v1/schema/validation/domain.json-schema.js +183 -0
  117. package/dist/v1/schema/validation/domain.structure.d.ts +4018 -0
  118. package/dist/v1/schema/validation/domain.structure.js +343 -155
  119. package/dist/v1/schema/validation/json.json-schema.d.ts +24 -0
  120. package/dist/v1/schema/validation/json.json-schema.js +36 -0
  121. package/dist/v1/schema/validation/policy/budgets.js +5 -112
  122. package/dist/v1/schema/validation/policy/context.d.ts +5 -3
  123. package/dist/v1/schema/validation/policy/context.js +5 -3
  124. package/dist/v1/schema/validation/policy/objects.d.ts +1 -1
  125. package/dist/v1/schema/validation/policy/objects.js +6 -14
  126. package/dist/v1/schema/validation/policy/references.js +10 -27
  127. package/dist/v1/schema/validation/policy/typing.d.ts +3 -18
  128. package/dist/v1/schema/validation/policy/typing.js +22 -356
  129. package/dist/v1/schema/validation/policy/uses.d.ts +1 -1
  130. package/dist/v1/schema/validation/policy/uses.js +9 -25
  131. package/dist/v1/schema/validation/policy/validate.js +3 -2
  132. package/dist/v1/schema/validation/value-api.d.ts +12 -1
  133. package/dist/v1/schema/validation/value-api.js +12 -3
  134. package/dist/v1/schema/validation/value.js +8 -10
  135. package/dist/v1/schema/validation/value.json-schema.d.ts +51 -0
  136. package/dist/v1/schema/validation/value.json-schema.js +126 -0
  137. package/dist/v1/schema/validation/view.js +4 -8
  138. package/package.json +3 -1
  139. package/dist/v1/compiled/admission/semantics/cardinality.d.ts +0 -5
  140. package/dist/v1/compiled/admission/semantics/cardinality.js +0 -128
  141. package/dist/v1/compiled/admission/semantics/policy/equality.d.ts +0 -8
  142. package/dist/v1/compiled/admission/semantics/policy/equality.js +0 -27
  143. package/dist/v1/compiled/admission/semantics/policy/measure.d.ts +0 -9
  144. package/dist/v1/compiled/admission/semantics/policy/measure.js +0 -81
  145. package/dist/v1/compiled/admission/semantics/policy/variants.d.ts +0 -10
  146. package/dist/v1/compiled/admission/semantics/policy/variants.js +0 -65
  147. package/dist/v1/language/class/index.d.ts +0 -8
  148. package/dist/v1/language/class/index.js +0 -1
  149. package/dist/v1/language/core/index.d.ts +0 -2
  150. package/dist/v1/language/core/index.js +0 -1
  151. package/dist/v1/language/value/index.d.ts +0 -11
  152. package/dist/v1/language/value/index.js +0 -8
@@ -0,0 +1,183 @@
1
+ import { toJSONSchema } from 'zod';
2
+ import { VALUE_RULES } from '../../language/validation/rule.js';
3
+ import { escapePointerToken } from '../../language/value/syntax/pointer.js';
4
+ import { RULES } from '../rule.js';
5
+ import { domainSchemaStructure, documentJsonSchema, structureRules, } from './domain.structure.js';
6
+ import { jsonValueJsonSchema, jsonValueRules } from './json.json-schema.js';
7
+ import { valueSchemaJsonSchema, valueSchemaRules, } from './value.json-schema.js';
8
+ /** The checks the published document names, each with its stage and the positions it constrains. */
9
+ const namedRules = { ...structureRules, ...jsonValueRules, ...valueSchemaRules };
10
+ /**
11
+ * The JSON Schema of an authored Domain schema document, rendered from the structure the Kernel
12
+ * enforces. Zod derives what it reads; `documentJsonSchema` states each refinement it cannot read,
13
+ * or names the rule JSON Schema cannot state. A refinement neither stated nor named fails the
14
+ * rendering, so that no check of the structure is dropped silently. Every value schema is in the
15
+ * published dialect and states the profile rules JSON Schema can state; the others are named.
16
+ */
17
+ export function domainSchemaJsonSchema() {
18
+ assertChecksStated(domainSchemaStructure);
19
+ const { $schema, $defs = {}, ...root } = toJSONSchema(domainSchemaStructure, {
20
+ metadata: documentJsonSchema,
21
+ io: 'input',
22
+ // Every opaque part is stated or named, as `assertChecksStated` proved.
23
+ unrepresentable: 'any',
24
+ override: ({ zodSchema, jsonSchema: rendered }) => {
25
+ // Zod states the items of a tuple but not its length.
26
+ const def = zodSchema._zod.def;
27
+ if (def.type !== 'tuple')
28
+ return;
29
+ if (def.rest !== null || def.items.some((item) => item._zod.optin === 'optional')) {
30
+ throw new Error('A tuple of the structure has a fixed length.');
31
+ }
32
+ rendered.minItems = def.items.length;
33
+ rendered.maxItems = def.items.length;
34
+ },
35
+ });
36
+ const unnamed = Object.keys($defs).filter((name) => name.startsWith('__schema'));
37
+ if (unnamed.length > 0) {
38
+ throw new Error(`Every shared part of the structure is named: ${unnamed.join(', ')} is not.`);
39
+ }
40
+ const json = jsonValueJsonSchema();
41
+ const values = valueSchemaJsonSchema();
42
+ const valueSchema = $defs['value-schema'];
43
+ const nodeProperty = $defs['node-property-definition'];
44
+ if (root.allOf !== undefined ||
45
+ valueSchema === undefined ||
46
+ nodeProperty === undefined ||
47
+ valueSchema.allOf !== undefined ||
48
+ nodeProperty.allOf !== undefined ||
49
+ [...Object.keys(json.definitions), ...Object.keys(values.definitions)].some((name) => name in $defs)) {
50
+ throw new Error('Portable JSON extends the root, and the value language the value-schema and Node Property definitions.');
51
+ }
52
+ const definitions = Object.fromEntries(Object.entries({
53
+ ...$defs,
54
+ 'value-schema': { ...valueSchema, allOf: [...values.root] },
55
+ 'node-property-definition': { ...nodeProperty, allOf: [values.nodeProperty] },
56
+ ...json.definitions,
57
+ ...values.definitions,
58
+ }).sort(([left], [right]) => (left < right ? -1 : left > right ? 1 : 0)));
59
+ const document = { ...root, allOf: [json.root] };
60
+ const positions = rulePositions({ ...document, $defs: definitions });
61
+ return {
62
+ $schema,
63
+ $id: 'https://schemas.astrale.ai/dsl/v1/domain-schema',
64
+ title: 'V1 DomainSchema',
65
+ $comment: 'Generated from the structure the Kernel enforces; value schemas are in the dialect https://schemas.astrale.ai/dsl/v1/value-schema. Each check JSON Schema cannot state is named under x-astrale-rules, with its stage and the positions it constrains.',
66
+ 'x-astrale-rules': Object.fromEntries(Object.keys(namedRules).sort().map((name) => {
67
+ const { statement, ...rule } = namedRules[name];
68
+ const named = { ...rule, stage: stageOf(rule), statement };
69
+ return [name, { ...named, positions: positions.get(name) ?? [] }];
70
+ })),
71
+ ...document,
72
+ $defs: definitions,
73
+ };
74
+ }
75
+ /**
76
+ * What a named rule reads, in the stages of `schema.RULES`: its code's, or for a value code the
77
+ * value rule's own, which reads the document alone unless it reads the context.
78
+ */
79
+ function stageOf(rule) {
80
+ if (rule.valueCode === undefined)
81
+ return RULES[rule.code].stage;
82
+ return VALUE_RULES[rule.valueCode].stage === 'context' ? 'context' : 'rule';
83
+ }
84
+ /**
85
+ * Every refinement and custom part of the structure carries its statement in `documentJsonSchema`:
86
+ * the keywords that state it, or its named rule. Only parts zod renders take part.
87
+ */
88
+ export function assertChecksStated(schema, visited = new Set()) {
89
+ if (visited.has(schema))
90
+ return;
91
+ visited.add(schema);
92
+ const part = schema;
93
+ const def = part._zod.def;
94
+ for (const check of def.checks ?? []) {
95
+ if (check._zod.def.check === 'custom' && !isStated(schema, check)) {
96
+ throw new Error('A refinement of the structure is neither stated nor named.');
97
+ }
98
+ }
99
+ switch (def.type) {
100
+ case 'boolean':
101
+ case 'enum':
102
+ case 'literal':
103
+ case 'null':
104
+ case 'number':
105
+ case 'string':
106
+ return;
107
+ case 'custom':
108
+ if (statementOf(schema) === undefined) {
109
+ throw new Error('A custom part of the structure is neither stated nor named.');
110
+ }
111
+ return;
112
+ case 'array':
113
+ return assertChecksStated(def.element, visited);
114
+ case 'lazy':
115
+ return assertChecksStated(part._zod.innerType, visited);
116
+ case 'object':
117
+ for (const field of Object.values(def.shape))
118
+ assertChecksStated(field, visited);
119
+ return;
120
+ case 'optional':
121
+ return assertChecksStated(def.innerType, visited);
122
+ case 'record':
123
+ assertChecksStated(def.keyType, visited);
124
+ return assertChecksStated(def.valueType, visited);
125
+ case 'tuple':
126
+ for (const item of def.items)
127
+ assertChecksStated(item, visited);
128
+ return;
129
+ case 'union':
130
+ for (const option of def.options)
131
+ assertChecksStated(option, visited);
132
+ return;
133
+ default:
134
+ throw new Error(`The structure has a ${def.type} part, which this rendering does not read.`);
135
+ }
136
+ }
137
+ /** A check is stated where it was added, or on a refinement of that schema. */
138
+ function isStated(schema, check) {
139
+ for (let current = schema; current !== undefined;) {
140
+ if (statementOf(current) !== undefined)
141
+ return true;
142
+ const parent = current._zod.parent;
143
+ if (parent?._zod.def.checks?.includes(check) !== true)
144
+ return false;
145
+ current = parent;
146
+ }
147
+ return false;
148
+ }
149
+ /** The keywords or rule registered on this very schema, which `get` would inherit from a parent. */
150
+ function statementOf(schema) {
151
+ const { id: _id, description: _description, ...statement } = documentJsonSchema._map.get(schema) ?? {};
152
+ return Object.keys(statement).length > 0 ? statement : undefined;
153
+ }
154
+ /**
155
+ * Where each rule applies, as JSON Pointers into the document. The marker a named check leaves is
156
+ * removed; every `maxLength` counts UTF-16 code units, the length the structure measures.
157
+ */
158
+ function rulePositions(document) {
159
+ const positions = new Map();
160
+ const note = (rule, pointer) => positions.set(rule, [...(positions.get(rule) ?? []), `#${pointer}`]);
161
+ const visit = (value, pointer) => {
162
+ if (Array.isArray(value)) {
163
+ value.forEach((item, index) => visit(item, `${pointer}/${index}`));
164
+ return;
165
+ }
166
+ if (value === null || typeof value !== 'object')
167
+ return;
168
+ const schema = value;
169
+ const rules = schema['x-astrale-rule'];
170
+ if (rules !== undefined) {
171
+ for (const rule of [rules].flat())
172
+ note(rule, pointer);
173
+ delete schema['x-astrale-rule'];
174
+ }
175
+ if (typeof schema.maxLength === 'number')
176
+ note('length-in-utf16-code-units', pointer);
177
+ for (const [key, entry] of Object.entries(schema)) {
178
+ visit(entry, `${pointer}/${escapePointerToken(key)}`);
179
+ }
180
+ };
181
+ visit(document, '');
182
+ return positions;
183
+ }