@elaraai/east 1.0.69 → 1.0.71
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/builtins.d.ts +1 -1
- package/dist/src/builtins.d.ts.map +1 -1
- package/dist/src/builtins.js +7 -0
- package/dist/src/builtins.js.map +1 -1
- package/dist/src/codegen/spellings.d.ts.map +1 -1
- package/dist/src/codegen/spellings.js +1 -0
- package/dist/src/codegen/spellings.js.map +1 -1
- package/dist/src/compile.d.ts.map +1 -1
- package/dist/src/compile.js +50 -2
- package/dist/src/compile.js.map +1 -1
- package/dist/src/compile.open_beast.spec.d.ts +6 -0
- package/dist/src/compile.open_beast.spec.d.ts.map +1 -0
- package/dist/src/compile.open_beast.spec.js +99 -0
- package/dist/src/compile.open_beast.spec.js.map +1 -0
- package/dist/src/expr/blob.d.ts +41 -1
- package/dist/src/expr/blob.d.ts.map +1 -1
- package/dist/src/expr/blob.js +52 -0
- package/dist/src/expr/blob.js.map +1 -1
- package/dist/src/expr/libs/datetime.d.ts +11 -0
- package/dist/src/expr/libs/datetime.d.ts.map +1 -1
- package/dist/src/expr/libs/datetime.js +11 -0
- package/dist/src/expr/libs/datetime.js.map +1 -1
- package/dist/src/location.d.ts +12 -0
- package/dist/src/location.d.ts.map +1 -1
- package/dist/src/location.js +15 -3
- package/dist/src/location.js.map +1 -1
- package/dist/src/serialization/beast2/index.d.ts +16 -1
- package/dist/src/serialization/beast2/index.d.ts.map +1 -1
- package/dist/src/serialization/beast2/index.js +24 -3
- package/dist/src/serialization/beast2/index.js.map +1 -1
- package/dist/src/serialization/beast2/v4/container.d.ts +10 -0
- package/dist/src/serialization/beast2/v4/container.d.ts.map +1 -1
- package/dist/src/serialization/beast2/v4/container.js +13 -0
- package/dist/src/serialization/beast2/v4/container.js.map +1 -1
- package/dist/src/serialization/beast2/v5/frames.d.ts +17 -0
- package/dist/src/serialization/beast2/v5/frames.d.ts.map +1 -1
- package/dist/src/serialization/beast2/v5/frames.js +40 -0
- package/dist/src/serialization/beast2/v5/frames.js.map +1 -1
- package/dist/src/serialization/beast2/v5/geometry.d.ts +10 -113
- package/dist/src/serialization/beast2/v5/geometry.d.ts.map +1 -1
- package/dist/src/serialization/beast2/v5/geometry.js +7 -151
- package/dist/src/serialization/beast2/v5/geometry.js.map +1 -1
- package/dist/src/serialization/beast2/v5/lazy.d.ts +8 -2
- package/dist/src/serialization/beast2/v5/lazy.d.ts.map +1 -1
- package/dist/src/serialization/beast2/v5/lazy.js +9 -3
- package/dist/src/serialization/beast2/v5/lazy.js.map +1 -1
- package/dist/src/serialization/beast2/v5/range.d.ts +201 -0
- package/dist/src/serialization/beast2/v5/range.d.ts.map +1 -0
- package/dist/src/serialization/beast2/v5/range.js +252 -0
- package/dist/src/serialization/beast2/v5/range.js.map +1 -0
- package/dist/src/serialization/beast2/v5/stream.d.ts +29 -5
- package/dist/src/serialization/beast2/v5/stream.d.ts.map +1 -1
- package/dist/src/serialization/beast2/v5/stream.js +86 -19
- package/dist/src/serialization/beast2/v5/stream.js.map +1 -1
- package/dist/src/serialization/beast2/v5/sync-reader.spec.d.ts +6 -0
- package/dist/src/serialization/beast2/v5/sync-reader.spec.d.ts.map +1 -0
- package/dist/src/serialization/beast2/v5/sync-reader.spec.js +228 -0
- package/dist/src/serialization/beast2/v5/sync-reader.spec.js.map +1 -0
- package/dist/src/serialization/index.d.ts +3 -1
- package/dist/src/serialization/index.d.ts.map +1 -1
- package/dist/src/serialization/index.js +3 -1
- package/dist/src/serialization/index.js.map +1 -1
- package/dist/src/serialization/json.d.ts.map +1 -1
- package/dist/src/serialization/json.js +31 -1
- package/dist/src/serialization/json.js.map +1 -1
- package/dist/src/serialization/json_schema.d.ts +76 -0
- package/dist/src/serialization/json_schema.d.ts.map +1 -0
- package/dist/src/serialization/json_schema.js +290 -0
- package/dist/src/serialization/json_schema.js.map +1 -0
- package/dist/src/serialization/json_schema.spec.d.ts +2 -0
- package/dist/src/serialization/json_schema.spec.d.ts.map +1 -0
- package/dist/src/serialization/json_schema.spec.js +327 -0
- package/dist/src/serialization/json_schema.spec.js.map +1 -0
- package/dist/src/serialization/json_schema_to_type.d.ts +74 -0
- package/dist/src/serialization/json_schema_to_type.d.ts.map +1 -0
- package/dist/src/serialization/json_schema_to_type.js +500 -0
- package/dist/src/serialization/json_schema_to_type.js.map +1 -0
- package/dist/src/serialization/json_schema_to_type.spec.d.ts +2 -0
- package/dist/src/serialization/json_schema_to_type.spec.d.ts.map +1 -0
- package/dist/src/serialization/json_schema_to_type.spec.js +372 -0
- package/dist/src/serialization/json_schema_to_type.spec.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Elara AI Pty Ltd
|
|
3
|
+
* Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details.
|
|
4
|
+
*/
|
|
5
|
+
import { describe, test } from "node:test";
|
|
6
|
+
import assert from "node:assert/strict";
|
|
7
|
+
import { createHash } from "node:crypto";
|
|
8
|
+
import { ArrayType, AsyncFunctionType, BlobType, BooleanType, DateTimeType, DictType, FloatType, FunctionType, IntegerType, MatrixType, NeverType, NullType, OptionType, RecursiveType, RefType, SetType, StringType, StructType, VariantType, VectorType, } from "../types.js";
|
|
9
|
+
import { EAST_JSON_PATTERNS, jsonSchemaFor } from "./json_schema.js";
|
|
10
|
+
import { toJSONFor } from "./json.js";
|
|
11
|
+
/** The pattern a leaf type's schema pins, as a compiled regex. */
|
|
12
|
+
function patternOf(schema) {
|
|
13
|
+
const pattern = schema["pattern"];
|
|
14
|
+
assert.equal(typeof pattern, "string", "expected the schema to carry a pattern");
|
|
15
|
+
return new RegExp(pattern);
|
|
16
|
+
}
|
|
17
|
+
/** A schema with `$schema` dropped, for comparing the body alone. */
|
|
18
|
+
function body(schema) {
|
|
19
|
+
const { $schema: _drop, ...rest } = schema;
|
|
20
|
+
return rest;
|
|
21
|
+
}
|
|
22
|
+
describe("jsonSchemaFor", () => {
|
|
23
|
+
describe("primitives", () => {
|
|
24
|
+
test("describes Null, Boolean and String directly", () => {
|
|
25
|
+
assert.deepEqual(body(jsonSchemaFor(NullType)), { type: "null" });
|
|
26
|
+
assert.deepEqual(body(jsonSchemaFor(BooleanType)), { type: "boolean" });
|
|
27
|
+
assert.deepEqual(body(jsonSchemaFor(StringType)), { type: "string" });
|
|
28
|
+
});
|
|
29
|
+
test("describes Float as a number or one of the non-finite spellings", () => {
|
|
30
|
+
assert.deepEqual(body(jsonSchemaFor(FloatType)), {
|
|
31
|
+
oneOf: [
|
|
32
|
+
{ type: "number" },
|
|
33
|
+
{ type: "string", enum: ["-0.0", "-Infinity", "Infinity", "NaN"] },
|
|
34
|
+
],
|
|
35
|
+
"x-east-type": "Float",
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
test("stamps $schema for the releases that carry one", () => {
|
|
39
|
+
assert.equal(jsonSchemaFor(StringType)["$schema"], "https://json-schema.org/draft/2020-12/schema");
|
|
40
|
+
assert.equal(jsonSchemaFor(StringType, { draft: "draft-07" })["$schema"], "http://json-schema.org/draft-07/schema#");
|
|
41
|
+
// An OpenAPI 3.0 schema object lives inside an OpenAPI document and
|
|
42
|
+
// has no $schema of its own.
|
|
43
|
+
assert.equal(jsonSchemaFor(StringType, { draft: "openapi-3.0" })["$schema"], undefined);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
describe("Integer", () => {
|
|
47
|
+
const pattern = patternOf(jsonSchemaFor(IntegerType));
|
|
48
|
+
test("accepts exactly the i64 range", () => {
|
|
49
|
+
for (const ok of ["0", "1", "-1", "42", "9223372036854775807", "-9223372036854775808"]) {
|
|
50
|
+
assert.ok(pattern.test(ok), `${ok} should be accepted`);
|
|
51
|
+
}
|
|
52
|
+
for (const bad of ["9223372036854775808", "-9223372036854775809"]) {
|
|
53
|
+
assert.ok(!pattern.test(bad), `${bad} should be rejected`);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
test("rejects an unsigned 64-bit id", () => {
|
|
57
|
+
// The naive `^(0|-?[1-9][0-9]{0,18})$` admits this, so a producer
|
|
58
|
+
// would validate it and we would then reject it on receipt.
|
|
59
|
+
assert.ok(!pattern.test("18446744073709551615"));
|
|
60
|
+
assert.ok(!pattern.test("9999999999999999999"));
|
|
61
|
+
});
|
|
62
|
+
test("rejects everything the decoder tolerates but the encoder never emits", () => {
|
|
63
|
+
// BigInt() accepts all of these; the published contract must not.
|
|
64
|
+
for (const bad of ["0x10", "0b101", "0o17", " 7 ", "+7", "007", "-0", "", "1e3", "7.5"]) {
|
|
65
|
+
assert.ok(!pattern.test(bad), `${JSON.stringify(bad)} should be rejected`);
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
test("accepts everything the encoder emits", () => {
|
|
69
|
+
const encode = toJSONFor(IntegerType);
|
|
70
|
+
const values = [
|
|
71
|
+
0n, 1n, -1n, 7n, -7n, 10n, 99n, 100n, 12345n, -12345n,
|
|
72
|
+
9007199254740993n, -9007199254740993n,
|
|
73
|
+
9223372036854775807n, -9223372036854775808n,
|
|
74
|
+
];
|
|
75
|
+
for (const v of values) {
|
|
76
|
+
assert.ok(pattern.test(encode(v)), `encoder output for ${v} should validate`);
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
});
|
|
80
|
+
describe("DateTime", () => {
|
|
81
|
+
const pattern = patternOf(jsonSchemaFor(DateTimeType));
|
|
82
|
+
test("accepts the canonical form the encoder emits", () => {
|
|
83
|
+
const encode = toJSONFor(DateTimeType);
|
|
84
|
+
for (const d of [new Date(0), new Date("2022-06-29T13:43:00.123Z"), new Date("2026-12-31T23:59:59.999Z")]) {
|
|
85
|
+
assert.ok(pattern.test(encode(d)), `${encode(d)} should validate`);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
test("rejects the offsets the decoder tolerates", () => {
|
|
89
|
+
// The decoder takes a Z suffix or any numeric offset; the encoder
|
|
90
|
+
// only ever writes +00:00, so the contract pins that.
|
|
91
|
+
assert.ok(!pattern.test("2022-06-29T13:43:00.123Z"));
|
|
92
|
+
assert.ok(!pattern.test("2022-06-29T13:43:00.123+05:00"));
|
|
93
|
+
assert.ok(!pattern.test("2022-06-29T13:43:00.123-08:00"));
|
|
94
|
+
});
|
|
95
|
+
test("rejects out-of-range calendar fields", () => {
|
|
96
|
+
for (const bad of [
|
|
97
|
+
"0000-01-01T00:00:00.000+00:00", // year 0, below the range every runtime reads
|
|
98
|
+
"2022-13-29T13:43:00.123+00:00", // month 13
|
|
99
|
+
"2022-06-32T13:43:00.123+00:00", // day 32
|
|
100
|
+
"2022-06-29T24:43:00.123+00:00", // hour 24
|
|
101
|
+
"2022-06-29T13:60:00.123+00:00", // minute 60
|
|
102
|
+
"2022-06-29T13:43:60.123+00:00", // second 60
|
|
103
|
+
"2022-06-29T13:43:00+00:00", // no milliseconds
|
|
104
|
+
"2022-06-29 13:43:00.123+00:00", // space, not T
|
|
105
|
+
]) {
|
|
106
|
+
assert.ok(!pattern.test(bad), `${bad} should be rejected`);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
test("accepts the first and last years the reader does", () => {
|
|
110
|
+
assert.ok(pattern.test("0001-01-01T00:00:00.000+00:00"));
|
|
111
|
+
assert.ok(pattern.test("9999-12-31T23:59:59.999+00:00"));
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
describe("digit classes", () => {
|
|
115
|
+
test("spells every digit as [0-9], never \\d", () => {
|
|
116
|
+
// A validator built on python's `re` reads \d as any Unicode digit,
|
|
117
|
+
// so a timestamp in Arabic-Indic digits would pass a partner's check
|
|
118
|
+
// and then fail on receipt. The contract has to read the same on
|
|
119
|
+
// every regex engine a partner might use.
|
|
120
|
+
for (const p of [EAST_JSON_PATTERNS.integer, EAST_JSON_PATTERNS.datetime, EAST_JSON_PATTERNS.blob]) {
|
|
121
|
+
assert.ok(!p.includes("\\d"), `${p} must not use \\d`);
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
describe("Blob", () => {
|
|
126
|
+
const pattern = patternOf(jsonSchemaFor(BlobType));
|
|
127
|
+
test("accepts the lowercase hex the encoder emits", () => {
|
|
128
|
+
const encode = toJSONFor(BlobType);
|
|
129
|
+
assert.ok(pattern.test(encode(new Uint8Array([]))));
|
|
130
|
+
assert.ok(pattern.test(encode(new Uint8Array([1, 3, 3, 7]))));
|
|
131
|
+
assert.ok(pattern.test(encode(new Uint8Array([0xde, 0xad, 0xbe, 0xef]))));
|
|
132
|
+
});
|
|
133
|
+
test("rejects uppercase hex, which only the decoder allows", () => {
|
|
134
|
+
assert.ok(!pattern.test("0xDEADBEEF"));
|
|
135
|
+
assert.ok(!pattern.test("0xAb"));
|
|
136
|
+
});
|
|
137
|
+
test("rejects a missing prefix or an odd digit count", () => {
|
|
138
|
+
assert.ok(!pattern.test("deadbeef"));
|
|
139
|
+
assert.ok(!pattern.test("0x123"));
|
|
140
|
+
assert.ok(!pattern.test("0xgg"));
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
describe("collections", () => {
|
|
144
|
+
test("describes Array as an array of its element", () => {
|
|
145
|
+
assert.deepEqual(body(jsonSchemaFor(ArrayType(StringType))), {
|
|
146
|
+
type: "array",
|
|
147
|
+
items: { type: "string" },
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
test("marks Set unique", () => {
|
|
151
|
+
assert.deepEqual(body(jsonSchemaFor(SetType(StringType))), {
|
|
152
|
+
type: "array",
|
|
153
|
+
items: { type: "string" },
|
|
154
|
+
uniqueItems: true,
|
|
155
|
+
"x-east-type": "Set",
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
test("describes Dict as its array-of-entries encoding", () => {
|
|
159
|
+
assert.deepEqual(body(jsonSchemaFor(DictType(StringType, BooleanType))), {
|
|
160
|
+
type: "array",
|
|
161
|
+
items: {
|
|
162
|
+
type: "object",
|
|
163
|
+
properties: { key: { type: "string" }, value: { type: "boolean" } },
|
|
164
|
+
required: ["key", "value"],
|
|
165
|
+
additionalProperties: false,
|
|
166
|
+
},
|
|
167
|
+
uniqueItems: true,
|
|
168
|
+
"x-east-type": "Dict",
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
test("closes Struct to its declared fields, all required", () => {
|
|
172
|
+
const schema = body(jsonSchemaFor(StructType({ a: StringType, b: BooleanType })));
|
|
173
|
+
assert.equal(schema["type"], "object");
|
|
174
|
+
assert.deepEqual(schema["required"], ["a", "b"]);
|
|
175
|
+
assert.equal(schema["additionalProperties"], false);
|
|
176
|
+
});
|
|
177
|
+
test("describes Vector and Matrix as arrays", () => {
|
|
178
|
+
assert.deepEqual(body(jsonSchemaFor(VectorType(FloatType)))["type"], "array");
|
|
179
|
+
const m = body(jsonSchemaFor(MatrixType(IntegerType)));
|
|
180
|
+
assert.equal(m["type"], "array");
|
|
181
|
+
assert.equal(m["items"]["type"], "array");
|
|
182
|
+
});
|
|
183
|
+
test("describes Ref as a one-element array, without the aliasing form", () => {
|
|
184
|
+
// The encoder ALSO writes {"$ref": …} for a target it has already
|
|
185
|
+
// written, but no reader resolves that back — a streaming reader has
|
|
186
|
+
// discarded what the pointer refers to — so advertising it would
|
|
187
|
+
// describe documents the reader then rejects. Excluded for the same
|
|
188
|
+
// reason as Array/Set/Dict aliasing, with the same stated
|
|
189
|
+
// consequence: a value with shared references does not validate
|
|
190
|
+
// against its own published schema.
|
|
191
|
+
assert.deepEqual(body(jsonSchemaFor(RefType(StringType))), {
|
|
192
|
+
type: "array",
|
|
193
|
+
items: { type: "string" },
|
|
194
|
+
minItems: 1,
|
|
195
|
+
maxItems: 1,
|
|
196
|
+
"x-east-type": "Ref",
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
describe("Variant", () => {
|
|
201
|
+
test("pins each case's tag and closes the object", () => {
|
|
202
|
+
const schema = body(jsonSchemaFor(VariantType({ ok: IntegerType, err: StringType })));
|
|
203
|
+
const alternatives = schema["oneOf"];
|
|
204
|
+
// VariantType sorts its cases, so the order is fixed by the type.
|
|
205
|
+
assert.equal(alternatives.length, 2);
|
|
206
|
+
const tags = alternatives.map(a => a["properties"]["type"]["const"]);
|
|
207
|
+
assert.deepEqual(tags, ["err", "ok"]);
|
|
208
|
+
for (const a of alternatives) {
|
|
209
|
+
assert.deepEqual(a["required"], ["type", "value"]);
|
|
210
|
+
assert.equal(a["additionalProperties"], false);
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
test("uses a single-valued enum where the release has no const", () => {
|
|
214
|
+
const schema = body(jsonSchemaFor(OptionType(StringType), { draft: "openapi-3.0" }));
|
|
215
|
+
const alternatives = schema["oneOf"];
|
|
216
|
+
const tag = alternatives[0]["properties"]["type"];
|
|
217
|
+
assert.deepEqual(tag["enum"], ["none"]);
|
|
218
|
+
assert.equal(tag["const"], undefined);
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
describe("recursive types", () => {
|
|
222
|
+
const LinkedListType = RecursiveType((self) => VariantType({
|
|
223
|
+
nil: NullType,
|
|
224
|
+
cons: StructType({ head: IntegerType, tail: self }),
|
|
225
|
+
}));
|
|
226
|
+
test("lifts the body into $defs and refers to it", () => {
|
|
227
|
+
const schema = jsonSchemaFor(LinkedListType);
|
|
228
|
+
assert.deepEqual(body(schema)["$ref"], "#/$defs/Recursive1");
|
|
229
|
+
const defs = schema["$defs"];
|
|
230
|
+
assert.ok(defs["Recursive1"] !== undefined);
|
|
231
|
+
});
|
|
232
|
+
test("names definitions by encounter order, not by type id", () => {
|
|
233
|
+
// Type ids come from a process-global counter, so a document keyed
|
|
234
|
+
// on them would differ between runs and between languages.
|
|
235
|
+
const a = JSON.stringify(jsonSchemaFor(LinkedListType));
|
|
236
|
+
const b = JSON.stringify(jsonSchemaFor(LinkedListType));
|
|
237
|
+
assert.equal(a, b);
|
|
238
|
+
assert.ok(a.includes("Recursive1"));
|
|
239
|
+
assert.ok(!/Recursive[0-9]{2,}/.test(a));
|
|
240
|
+
});
|
|
241
|
+
test("uses the release's definitions keyword", () => {
|
|
242
|
+
const seven = jsonSchemaFor(LinkedListType, { draft: "draft-07" });
|
|
243
|
+
assert.deepEqual(body(seven)["$ref"], "#/definitions/Recursive1");
|
|
244
|
+
assert.ok(seven["definitions"] !== undefined);
|
|
245
|
+
assert.equal(seven["$defs"], undefined);
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
describe("releases", () => {
|
|
249
|
+
test("spells Null without a null type on OpenAPI 3.0", () => {
|
|
250
|
+
assert.deepEqual(body(jsonSchemaFor(NullType, { draft: "openapi-3.0" })), {
|
|
251
|
+
nullable: true,
|
|
252
|
+
enum: [null],
|
|
253
|
+
});
|
|
254
|
+
});
|
|
255
|
+
test("pins the same integer range in every release", () => {
|
|
256
|
+
const p2020 = jsonSchemaFor(IntegerType)["pattern"];
|
|
257
|
+
const p07 = jsonSchemaFor(IntegerType, { draft: "draft-07" })["pattern"];
|
|
258
|
+
const pOas = jsonSchemaFor(IntegerType, { draft: "openapi-3.0" })["pattern"];
|
|
259
|
+
assert.equal(p2020, p07);
|
|
260
|
+
assert.equal(p07, pOas);
|
|
261
|
+
});
|
|
262
|
+
});
|
|
263
|
+
describe("types with no JSON form", () => {
|
|
264
|
+
test("refuses Never, naming it", () => {
|
|
265
|
+
assert.throws(() => jsonSchemaFor(NeverType), /cannot describe Never/);
|
|
266
|
+
});
|
|
267
|
+
test("refuses functions, naming them", () => {
|
|
268
|
+
assert.throws(() => jsonSchemaFor(FunctionType([], IntegerType)), /cannot describe Function/);
|
|
269
|
+
assert.throws(() => jsonSchemaFor(AsyncFunctionType([], IntegerType)), /cannot describe AsyncFunction/);
|
|
270
|
+
});
|
|
271
|
+
test("refuses a function nested inside a collection", () => {
|
|
272
|
+
assert.throws(() => jsonSchemaFor(StructType({ f: FunctionType([], IntegerType) })), /cannot describe Function/);
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
test("matches the cross-language corpus digest", () => {
|
|
276
|
+
// The python twin asserts this same digest over the same corpus in the
|
|
277
|
+
// same order (east-py tests/serialization/test_json_schema.py). Two
|
|
278
|
+
// languages agreeing on one hash is what keeps a partner from being
|
|
279
|
+
// handed different contracts; changing the emitted bytes deliberately
|
|
280
|
+
// means updating both constants, which is the point.
|
|
281
|
+
const RecursiveCorpusType = RecursiveType((self) => VariantType({
|
|
282
|
+
nil: NullType,
|
|
283
|
+
cons: StructType({ head: IntegerType, tail: self }),
|
|
284
|
+
}));
|
|
285
|
+
const corpus = [
|
|
286
|
+
["Null", NullType], ["Boolean", BooleanType], ["Integer", IntegerType],
|
|
287
|
+
["Float", FloatType], ["String", StringType], ["DateTime", DateTimeType],
|
|
288
|
+
["Blob", BlobType],
|
|
289
|
+
["Array", ArrayType(IntegerType)], ["Set", SetType(StringType)],
|
|
290
|
+
["Dict", DictType(StringType, IntegerType)],
|
|
291
|
+
["Struct", StructType({ a: StringType, b: IntegerType, c: DateTimeType })],
|
|
292
|
+
["Variant", VariantType({ ok: IntegerType, err: StringType })],
|
|
293
|
+
["Option", OptionType(StringType)],
|
|
294
|
+
["Ref", RefType(IntegerType)],
|
|
295
|
+
["Vector", VectorType(FloatType)], ["Matrix", MatrixType(IntegerType)],
|
|
296
|
+
["nested", ArrayType(StructType({
|
|
297
|
+
id: IntegerType, tags: SetType(StringType),
|
|
298
|
+
note: OptionType(StringType), when: DateTimeType,
|
|
299
|
+
}))],
|
|
300
|
+
["recursive", RecursiveCorpusType],
|
|
301
|
+
["arrayRecursive", ArrayType(RecursiveCorpusType)],
|
|
302
|
+
];
|
|
303
|
+
const lines = [];
|
|
304
|
+
for (const draft of ["2020-12", "draft-07", "openapi-3.0"]) {
|
|
305
|
+
for (const [name, type] of corpus) {
|
|
306
|
+
lines.push(`${draft}|${name}=${JSON.stringify(jsonSchemaFor(type, { draft }))}`);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
assert.equal(lines.length, 57);
|
|
310
|
+
assert.equal(createHash("sha256").update(lines.join("\n")).digest("hex"), "7083a9ae6f830e8724c707c0f0636a57be01fe2085874e83fea00883401c1a6b");
|
|
311
|
+
});
|
|
312
|
+
test("emits byte-identical documents for the same type and release", () => {
|
|
313
|
+
const T = StructType({
|
|
314
|
+
id: IntegerType,
|
|
315
|
+
at: DateTimeType,
|
|
316
|
+
tags: SetType(StringType),
|
|
317
|
+
note: OptionType(StringType),
|
|
318
|
+
blob: BlobType,
|
|
319
|
+
});
|
|
320
|
+
for (const draft of ["2020-12", "draft-07", "openapi-3.0"]) {
|
|
321
|
+
const first = JSON.stringify(jsonSchemaFor(ArrayType(T), { draft }));
|
|
322
|
+
const second = JSON.stringify(jsonSchemaFor(ArrayType(T), { draft }));
|
|
323
|
+
assert.equal(first, second, `${draft} output should be stable`);
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
});
|
|
327
|
+
//# sourceMappingURL=json_schema.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json_schema.spec.js","sourceRoot":"","sources":["../../../src/serialization/json_schema.spec.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,MAAM,MAAM,oBAAoB,CAAC;AACxC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,OAAO,EACH,SAAS,EACT,iBAAiB,EACjB,QAAQ,EACR,WAAW,EACX,YAAY,EACZ,QAAQ,EACR,SAAS,EACT,YAAY,EACZ,WAAW,EACX,UAAU,EACV,SAAS,EACT,QAAQ,EACR,UAAU,EACV,aAAa,EACb,OAAO,EACP,OAAO,EACP,UAAU,EACV,UAAU,EACV,WAAW,EACX,UAAU,GAEb,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,kBAAkB,EAAE,aAAa,EAAmB,MAAM,kBAAkB,CAAC;AACtF,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAEtC,kEAAkE;AAClE,SAAS,SAAS,CAA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AAC;YACrF,CAAC;QACL,CAAC;QACD,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC;QAC/B,MAAM,CAAC,KAAK,CACR,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,EAC3D,kEAAkE,CAAC,CAAC;IAC5E,CAAC,CAAC,CAAC;IAEH,IAAI,CAAC,8DAA8D,EAAE,GAAG,EAAE;QACtE,MAAM,CAAC,GAAG,UAAU,CAAC;YACjB,EAAE,EAAE,WAAW;YACf,EAAE,EAAE,YAAY;YAChB,IAAI,EAAE,OAAO,CAAC,UAAU,CAAC;YACzB,IAAI,EAAE,UAAU,CAAC,UAAU,CAAC;YAC5B,IAAI,EAAE,QAAQ;SACjB,CAAC,CAAC;QACH,KAAK,MAAM,KAAK,IAAI,CAAC,SAAS,EAAE,UAAU,EAAE,aAAa,CAAU,EAAE,CAAC;YAClE,MAAM,KAAK,GAAG,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;YACrE,MAAM,MAAM,GAAG,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC;YACtE,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,KAAK,0BAA0B,CAAC,CAAC;QACpE,CAAC;IACL,CAAC,CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 Elara AI Pty Ltd
|
|
3
|
+
* Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details.
|
|
4
|
+
*/
|
|
5
|
+
import { type EastType } from "../types.js";
|
|
6
|
+
import type { JsonSchema } from "./json_schema.js";
|
|
7
|
+
/**
|
|
8
|
+
* Raised when a schema cannot be expressed as an East type.
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* Carries the RFC 6901 pointer to the offending node, because a contract
|
|
12
|
+
* document is large and "unsupported keyword" without a location is not
|
|
13
|
+
* actionable.
|
|
14
|
+
*/
|
|
15
|
+
export declare class JsonSchemaUnsupportedError extends Error {
|
|
16
|
+
/** RFC 6901 pointer to the schema node that could not be converted. */
|
|
17
|
+
readonly pointer: string;
|
|
18
|
+
constructor(message: string, pointer: string);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Builds an East type from a JSON Schema document.
|
|
22
|
+
*
|
|
23
|
+
* @param schema - The schema document
|
|
24
|
+
* @returns The East type the schema describes
|
|
25
|
+
* @throws {JsonSchemaUnsupportedError} When the schema uses a keyword East's
|
|
26
|
+
* type system cannot express, naming the keyword and its RFC 6901 pointer
|
|
27
|
+
*
|
|
28
|
+
* @remarks
|
|
29
|
+
* This is the one place the full JSON Schema vocabulary is confronted, and it
|
|
30
|
+
* runs at build time, so nothing it rejects can reach a runtime.
|
|
31
|
+
*
|
|
32
|
+
* A document emitted by {@link jsonSchemaFor} carries `x-east-type`
|
|
33
|
+
* annotations and inverts exactly — JSON Schema alone cannot tell `DateTime`
|
|
34
|
+
* from a `String` with `format: date-time`, `Set` from `Array`, or `Dict`
|
|
35
|
+
* from an array of two-property objects. A foreign document without those
|
|
36
|
+
* annotations still converts, under the structural mapping below, but does
|
|
37
|
+
* not promise to round-trip:
|
|
38
|
+
*
|
|
39
|
+
* | schema | East type |
|
|
40
|
+
* |---|---|
|
|
41
|
+
* | `{"type":"null"}`, or OpenAPI 3.0's `nullable` + `enum: [null]` | `Null` |
|
|
42
|
+
* | `{"type":"boolean"}` | `Boolean` |
|
|
43
|
+
* | `{"type":"string"}` | `String` |
|
|
44
|
+
* | `{"type":"number"}`, `{"type":"integer"}` | `Float`, `Integer` |
|
|
45
|
+
* | `{"type":"array","items":X}` | `Array<X>` |
|
|
46
|
+
* | a closed object with `required` covering every property | `Struct` |
|
|
47
|
+
* | `oneOf` of objects tagged by a constant `type` | `Variant` |
|
|
48
|
+
*
|
|
49
|
+
* OpenAPI 3.0's `nullable: true` beside a type is refused rather than dropped:
|
|
50
|
+
* East JSON has no bare null for any type but `Null`, so the only reading
|
|
51
|
+
* would silently admit a schema whose nulls the reader then rejects — model
|
|
52
|
+
* the value as an Option instead.
|
|
53
|
+
*
|
|
54
|
+
* Definitions are resolved through `$defs` or `definitions`, whichever the
|
|
55
|
+
* document uses. Cycles among definitions become `RecursiveType`s, one per
|
|
56
|
+
* cycle group: `Node → NodeList → Node`, the ordinary way a recursive schema is
|
|
57
|
+
* written, is the single type `RecursiveType(self => Struct({ children:
|
|
58
|
+
* Array(self) }))` with the alias inlined. A group whose cycles do not all pass
|
|
59
|
+
* through one definition would need two binders, which East does not support,
|
|
60
|
+
* and is refused naming the group.
|
|
61
|
+
*
|
|
62
|
+
* A `$schema` is honoured when present, and a release this cannot read — a
|
|
63
|
+
* draft-04 document, say — is refused by name instead of being structurally
|
|
64
|
+
* guessed at. It is not required: an OpenAPI 3.0 schema object is a fragment of
|
|
65
|
+
* a larger document and carries none, so demanding one would reject what
|
|
66
|
+
* `jsonSchemaFor(T, { draft: "openapi-3.0" })` emits.
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```ts
|
|
70
|
+
* const T = typeFromJsonSchema(JSON.parse(readFileSync("contract.schema.json", "utf-8")));
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export declare function typeFromJsonSchema(schema: JsonSchema): EastType;
|
|
74
|
+
//# sourceMappingURL=json_schema_to_type.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json_schema_to_type.d.ts","sourceRoot":"","sources":["../../../src/serialization/json_schema_to_type.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAiBL,KAAK,QAAQ,EAEd,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,UAAU,EAAoC,MAAM,kBAAkB,CAAC;AAErF;;;;;;;GAOG;AACH,qBAAa,0BAA2B,SAAQ,KAAK;IACnD,uEAAuE;IACvE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;gBAEb,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;CAK7C;AA2ID;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,UAAU,GAAG,QAAQ,CAuB/D"}
|