@elaraai/east 1.0.70 → 1.0.72
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/expr/libs/string.d.ts +15 -2
- package/dist/src/expr/libs/string.d.ts.map +1 -1
- package/dist/src/expr/libs/string.js +15 -2
- package/dist/src/expr/libs/string.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/index.d.ts +2 -0
- package/dist/src/serialization/index.d.ts.map +1 -1
- package/dist/src/serialization/index.js +2 -0
- package/dist/src/serialization/index.js.map +1 -1
- package/dist/src/serialization/json.d.ts +43 -2
- package/dist/src/serialization/json.d.ts.map +1 -1
- package/dist/src/serialization/json.js +138 -9
- package/dist/src/serialization/json.js.map +1 -1
- package/dist/src/serialization/json.spec.js +99 -15
- package/dist/src/serialization/json.spec.js.map +1 -1
- package/dist/src/serialization/json_schema.d.ts +81 -0
- package/dist/src/serialization/json_schema.d.ts.map +1 -0
- package/dist/src/serialization/json_schema.js +305 -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 +427 -0
- package/dist/src/serialization/json_schema.spec.js.map +1 -0
- package/dist/src/serialization/json_schema_to_type.d.ts +76 -0
- package/dist/src/serialization/json_schema_to_type.d.ts.map +1 -0
- package/dist/src/serialization/json_schema_to_type.js +576 -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 +428 -0
- package/dist/src/serialization/json_schema_to_type.spec.js.map +1 -0
- package/package.json +2 -1
|
@@ -0,0 +1,81 @@
|
|
|
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 EastTypeValue } from "../type_of_type.js";
|
|
6
|
+
import type { EastType } from "../types.js";
|
|
7
|
+
/** A JSON value appearing inside a schema document. */
|
|
8
|
+
export type JsonSchemaValue = string | number | boolean | null | JsonSchemaValue[] | JsonSchema;
|
|
9
|
+
/** A JSON Schema document, as an ordinary JSON object. */
|
|
10
|
+
export type JsonSchema = {
|
|
11
|
+
[key: string]: JsonSchemaValue;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* The JSON Schema release a document is emitted in.
|
|
15
|
+
*
|
|
16
|
+
* @remarks
|
|
17
|
+
* A consumer's validator pins a release, so the published contract has to be
|
|
18
|
+
* emitted in the one they can actually read. This selects the spelling of the
|
|
19
|
+
* document, never the encoding it describes — East JSON is the only encoding.
|
|
20
|
+
*/
|
|
21
|
+
export type JsonSchemaDraft = "2020-12" | "draft-07" | "openapi-3.0";
|
|
22
|
+
/** Options for {@link jsonSchemaFor}. */
|
|
23
|
+
export interface JsonSchemaOptions {
|
|
24
|
+
/** Which release to emit. Defaults to `"2020-12"`. */
|
|
25
|
+
draft?: JsonSchemaDraft;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The exact lexical forms East JSON's scalar encodings take.
|
|
29
|
+
*
|
|
30
|
+
* @remarks
|
|
31
|
+
* Published so a reader can enforce precisely what {@link jsonSchemaFor}
|
|
32
|
+
* describes — the contract and the check are then one definition, not two that
|
|
33
|
+
* have to be kept in step by hand. Each is stricter than the historic decoder,
|
|
34
|
+
* which also accepts hexadecimal and whitespace-padded integers, a `Z` suffix
|
|
35
|
+
* or any numeric offset on a timestamp, and uppercase hex blobs.
|
|
36
|
+
*/
|
|
37
|
+
export declare const EAST_JSON_PATTERNS: {
|
|
38
|
+
/** Decimal i64, no leading zeros, no sign on zero. */
|
|
39
|
+
readonly integer: string;
|
|
40
|
+
/** RFC 3339 in UTC with three fractional digits and an explicit `+00:00`. */
|
|
41
|
+
readonly datetime: string;
|
|
42
|
+
/** `0x` followed by an even count of lowercase hex digits. */
|
|
43
|
+
readonly blob: "^0x(?:[0-9a-f]{2})*$";
|
|
44
|
+
/** The non-finite floats, as strings, in the order the schema lists them. */
|
|
45
|
+
readonly floatSpecials: readonly string[];
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Emits a JSON Schema describing the East-JSON encoding of an East type.
|
|
49
|
+
*
|
|
50
|
+
* @param type - The East type to describe
|
|
51
|
+
* @param options - Which release to emit
|
|
52
|
+
* @returns A JSON Schema document
|
|
53
|
+
* @throws {Error} When the type has no JSON form — `Never`, `Function` or
|
|
54
|
+
* `AsyncFunction` — naming the offending type
|
|
55
|
+
*
|
|
56
|
+
* @remarks
|
|
57
|
+
* The schema describes what `East.String.printJson` emits and what a strict
|
|
58
|
+
* reader accepts, so a producer validating against it cannot send a payload
|
|
59
|
+
* that would then be rejected. It pins the **encoder's** canonical output
|
|
60
|
+
* rather than the decoder's tolerance: the decoder accepts hexadecimal and
|
|
61
|
+
* whitespace-padded integers, a `Z` suffix on timestamps and uppercase hex
|
|
62
|
+
* blobs, and none of those appear here.
|
|
63
|
+
*
|
|
64
|
+
* An `Option<T>` whose payload can never encode as `null` is described as it
|
|
65
|
+
* encodes — `oneOf` the draft's `null` and `T`'s own schema, annotated
|
|
66
|
+
* `x-east-type: "Option"` — and only `Option<Null>` and `Option<Option<T>>`,
|
|
67
|
+
* the two payloads that can themselves be `null`, keep the tagged object.
|
|
68
|
+
*
|
|
69
|
+
* The document is deterministic — key order, `$defs` names and case order are
|
|
70
|
+
* fixed by the type, not by process state — so the TypeScript and Python
|
|
71
|
+
* implementations emit byte-identical bytes for the same type and release.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* const ReadingType = StructType({ sensor: StringType, litres: IntegerType });
|
|
76
|
+
* const schema = jsonSchemaFor(ArrayType(ReadingType), { draft: "draft-07" });
|
|
77
|
+
* writeFileSync("contract.schema.json", JSON.stringify(schema, null, 2));
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export declare function jsonSchemaFor(type: EastType | EastTypeValue, options?: JsonSchemaOptions): JsonSchema;
|
|
81
|
+
//# sourceMappingURL=json_schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json_schema.d.ts","sourceRoot":"","sources":["../../../src/serialization/json_schema.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAmB,KAAK,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACzE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AAI5C,uDAAuD;AACvD,MAAM,MAAM,eAAe,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,eAAe,EAAE,GAAG,UAAU,CAAC;AAEhG,0DAA0D;AAC1D,MAAM,MAAM,UAAU,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,eAAe,CAAA;CAAE,CAAC;AAE5D;;;;;;;GAOG;AACH,MAAM,MAAM,eAAe,GAAG,SAAS,GAAG,UAAU,GAAG,aAAa,CAAC;AAErE,yCAAyC;AACzC,MAAM,WAAW,iBAAiB;IAChC,sDAAsD;IACtD,KAAK,CAAC,EAAE,eAAe,CAAC;CACzB;AAmFD;;;;;;;;;GASG;AACH,eAAO,MAAM,kBAAkB;IAC7B,sDAAsD;sBACvC,MAAM;IACrB,6EAA6E;;IAE7E,8DAA8D;;IAE9D,6EAA6E;4BAC5C,SAAS,MAAM,EAAE;CAC1C,CAAC;AAwBX;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,QAAQ,GAAG,aAAa,EAAE,OAAO,GAAE,iBAAsB,GAAG,UAAU,CAazG"}
|
|
@@ -0,0 +1,305 @@
|
|
|
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 { toEastTypeValue } from "../type_of_type.js";
|
|
6
|
+
import { isVariant } from "../containers/variant.js";
|
|
7
|
+
import { jsonFlatOptionPayload } from "./json.js";
|
|
8
|
+
const SCHEMA_URI = {
|
|
9
|
+
"2020-12": "https://json-schema.org/draft/2020-12/schema",
|
|
10
|
+
"draft-07": "http://json-schema.org/draft-07/schema#",
|
|
11
|
+
// OpenAPI 3.0 schema objects live inside an OpenAPI document and carry no
|
|
12
|
+
// $schema of their own; stamping one would make the fragment invalid.
|
|
13
|
+
"openapi-3.0": null,
|
|
14
|
+
};
|
|
15
|
+
/** i64 bounds, as the decoder enforces them. */
|
|
16
|
+
const I64_MAX = "9223372036854775807";
|
|
17
|
+
const I64_MIN_ABS = "9223372036854775808";
|
|
18
|
+
/**
|
|
19
|
+
* A regex alternation matching every decimal string from `"1"` to `max`, with
|
|
20
|
+
* no leading zeros. Zero is excluded so the sign can be attached without
|
|
21
|
+
* admitting `"-0"`, which the encoder never emits.
|
|
22
|
+
*
|
|
23
|
+
* @param max - The inclusive upper bound, as decimal digits
|
|
24
|
+
* @returns An un-anchored alternation body
|
|
25
|
+
*
|
|
26
|
+
* @remarks
|
|
27
|
+
* Generated rather than hand-written because the obvious approximation —
|
|
28
|
+
* `[1-9][0-9]{0,18}` for i64 — accepts every 19-digit value up to
|
|
29
|
+
* 9999999999999999999, so an unsigned 64-bit id passes a producer's validator
|
|
30
|
+
* and then fails on receipt. The construction fixes each prefix of `max` in
|
|
31
|
+
* turn and lets the digit at that position range below `max`'s.
|
|
32
|
+
*/
|
|
33
|
+
function boundedDigitPattern(max) {
|
|
34
|
+
const k = max.length;
|
|
35
|
+
const alts = [];
|
|
36
|
+
// Every shorter length is unconditionally below the bound; there are none
|
|
37
|
+
// to add when the bound is itself a single digit.
|
|
38
|
+
if (k >= 2)
|
|
39
|
+
alts.push(k === 2 ? "[1-9]" : `[1-9][0-9]{0,${k - 2}}`);
|
|
40
|
+
let prefix = "";
|
|
41
|
+
for (let i = 0; i < k; i++) {
|
|
42
|
+
const digit = max.charCodeAt(i) - 48;
|
|
43
|
+
const lo = i === 0 ? 1 : 0;
|
|
44
|
+
if (digit > lo) {
|
|
45
|
+
const cls = digit - 1 === lo ? `${lo}` : `[${lo}-${digit - 1}]`;
|
|
46
|
+
const rest = k - i - 1;
|
|
47
|
+
alts.push(`${prefix}${cls}${rest === 0 ? "" : rest === 1 ? "[0-9]" : `[0-9]{${rest}}`}`);
|
|
48
|
+
}
|
|
49
|
+
prefix += max[i];
|
|
50
|
+
}
|
|
51
|
+
alts.push(max);
|
|
52
|
+
return alts.join("|");
|
|
53
|
+
}
|
|
54
|
+
/** The exact accepted form of East JSON's `Integer` encoding. */
|
|
55
|
+
function integerPattern() {
|
|
56
|
+
return `^(?:0|(?:${boundedDigitPattern(I64_MAX)})|-(?:${boundedDigitPattern(I64_MIN_ABS)}))$`;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The canonical text `DateTime` encodes to — always UTC, always three
|
|
60
|
+
* fractional digits, always an explicit `+00:00` offset.
|
|
61
|
+
*
|
|
62
|
+
* @remarks
|
|
63
|
+
* Stricter than the decoder, deliberately: the decoder also accepts a `Z`
|
|
64
|
+
* suffix and any numeric offset, neither of which the encoder ever emits.
|
|
65
|
+
* The year is pinned to `0001`–`9999`, the range every runtime reads (python's
|
|
66
|
+
* datetime starts at year 1), so `0000` is refused by the validator rather
|
|
67
|
+
* than only by the reader. Every digit class is spelled `[0-9]`, never `\d`:
|
|
68
|
+
* a validator built on python's `re` reads `\d` as any Unicode digit, so a
|
|
69
|
+
* timestamp written in Arabic-Indic digits would pass a partner's check and
|
|
70
|
+
* then fail on receipt. Calendar-impossible dates such as `2026-02-30` still
|
|
71
|
+
* match — no regex a schema can carry rules them out — and are rejected when
|
|
72
|
+
* the date is constructed.
|
|
73
|
+
*/
|
|
74
|
+
const DATETIME_PATTERN = "^(?:000[1-9]|00[1-9][0-9]|0[1-9][0-9]{2}|[1-9][0-9]{3})" +
|
|
75
|
+
"-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12][0-9]|3[01])" +
|
|
76
|
+
"T(?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]\\.[0-9]{3}\\+00:00$";
|
|
77
|
+
/** The canonical text `Blob` encodes to — `0x` and an even count of lowercase hex. */
|
|
78
|
+
const BLOB_PATTERN = "^0x(?:[0-9a-f]{2})*$";
|
|
79
|
+
/** The non-finite floats JSON cannot hold, as the encoder spells them. Sorted for determinism. */
|
|
80
|
+
const FLOAT_SPECIALS = ["-0.0", "-Infinity", "Infinity", "NaN"];
|
|
81
|
+
/**
|
|
82
|
+
* The exact lexical forms East JSON's scalar encodings take.
|
|
83
|
+
*
|
|
84
|
+
* @remarks
|
|
85
|
+
* Published so a reader can enforce precisely what {@link jsonSchemaFor}
|
|
86
|
+
* describes — the contract and the check are then one definition, not two that
|
|
87
|
+
* have to be kept in step by hand. Each is stricter than the historic decoder,
|
|
88
|
+
* which also accepts hexadecimal and whitespace-padded integers, a `Z` suffix
|
|
89
|
+
* or any numeric offset on a timestamp, and uppercase hex blobs.
|
|
90
|
+
*/
|
|
91
|
+
export const EAST_JSON_PATTERNS = {
|
|
92
|
+
/** Decimal i64, no leading zeros, no sign on zero. */
|
|
93
|
+
get integer() { return integerPattern(); },
|
|
94
|
+
/** RFC 3339 in UTC with three fractional digits and an explicit `+00:00`. */
|
|
95
|
+
datetime: DATETIME_PATTERN,
|
|
96
|
+
/** `0x` followed by an even count of lowercase hex digits. */
|
|
97
|
+
blob: BLOB_PATTERN,
|
|
98
|
+
/** The non-finite floats, as strings, in the order the schema lists them. */
|
|
99
|
+
floatSpecials: FLOAT_SPECIALS,
|
|
100
|
+
};
|
|
101
|
+
/** The draft's spelling of `Null` — also the alternative a flat Option's `none` takes. */
|
|
102
|
+
function nullSchema(draft) {
|
|
103
|
+
// OpenAPI 3.0 predates the "null" type; `nullable` plus a closed enum is
|
|
104
|
+
// the documented equivalent.
|
|
105
|
+
return draft === "openapi-3.0" ? { nullable: true, enum: [null] } : { type: "null" };
|
|
106
|
+
}
|
|
107
|
+
/** Where definitions live, and how they are referenced, in a given release. */
|
|
108
|
+
function defsKeyword(draft) {
|
|
109
|
+
return draft === "2020-12" ? "$defs" : "definitions";
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Emits a JSON Schema describing the East-JSON encoding of an East type.
|
|
113
|
+
*
|
|
114
|
+
* @param type - The East type to describe
|
|
115
|
+
* @param options - Which release to emit
|
|
116
|
+
* @returns A JSON Schema document
|
|
117
|
+
* @throws {Error} When the type has no JSON form — `Never`, `Function` or
|
|
118
|
+
* `AsyncFunction` — naming the offending type
|
|
119
|
+
*
|
|
120
|
+
* @remarks
|
|
121
|
+
* The schema describes what `East.String.printJson` emits and what a strict
|
|
122
|
+
* reader accepts, so a producer validating against it cannot send a payload
|
|
123
|
+
* that would then be rejected. It pins the **encoder's** canonical output
|
|
124
|
+
* rather than the decoder's tolerance: the decoder accepts hexadecimal and
|
|
125
|
+
* whitespace-padded integers, a `Z` suffix on timestamps and uppercase hex
|
|
126
|
+
* blobs, and none of those appear here.
|
|
127
|
+
*
|
|
128
|
+
* An `Option<T>` whose payload can never encode as `null` is described as it
|
|
129
|
+
* encodes — `oneOf` the draft's `null` and `T`'s own schema, annotated
|
|
130
|
+
* `x-east-type: "Option"` — and only `Option<Null>` and `Option<Option<T>>`,
|
|
131
|
+
* the two payloads that can themselves be `null`, keep the tagged object.
|
|
132
|
+
*
|
|
133
|
+
* The document is deterministic — key order, `$defs` names and case order are
|
|
134
|
+
* fixed by the type, not by process state — so the TypeScript and Python
|
|
135
|
+
* implementations emit byte-identical bytes for the same type and release.
|
|
136
|
+
*
|
|
137
|
+
* @example
|
|
138
|
+
* ```ts
|
|
139
|
+
* const ReadingType = StructType({ sensor: StringType, litres: IntegerType });
|
|
140
|
+
* const schema = jsonSchemaFor(ArrayType(ReadingType), { draft: "draft-07" });
|
|
141
|
+
* writeFileSync("contract.schema.json", JSON.stringify(schema, null, 2));
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
export function jsonSchemaFor(type, options = {}) {
|
|
145
|
+
const draft = options.draft ?? "2020-12";
|
|
146
|
+
const typeValue = isVariant(type) ? type : toEastTypeValue(type);
|
|
147
|
+
const ctx = { draft, defs: {}, names: new Map(), inners: new Map() };
|
|
148
|
+
const body = schemaOf(typeValue, ctx);
|
|
149
|
+
const out = {};
|
|
150
|
+
const uri = SCHEMA_URI[draft];
|
|
151
|
+
if (uri !== null)
|
|
152
|
+
out["$schema"] = uri;
|
|
153
|
+
for (const [k, v] of Object.entries(body))
|
|
154
|
+
out[k] = v;
|
|
155
|
+
if (Object.keys(ctx.defs).length > 0)
|
|
156
|
+
out[defsKeyword(draft)] = ctx.defs;
|
|
157
|
+
return out;
|
|
158
|
+
}
|
|
159
|
+
/** The schema for one type node, accumulating any recursive definitions into `ctx`. */
|
|
160
|
+
function schemaOf(t, ctx) {
|
|
161
|
+
switch (t.type) {
|
|
162
|
+
case "Never":
|
|
163
|
+
throw new Error("jsonSchemaFor cannot describe Never — it has no values, so no JSON document satisfies it");
|
|
164
|
+
case "Function":
|
|
165
|
+
throw new Error("jsonSchemaFor cannot describe Function — JSON has no function form");
|
|
166
|
+
case "AsyncFunction":
|
|
167
|
+
throw new Error("jsonSchemaFor cannot describe AsyncFunction — JSON has no function form");
|
|
168
|
+
case "Null":
|
|
169
|
+
return nullSchema(ctx.draft);
|
|
170
|
+
case "Boolean":
|
|
171
|
+
return { type: "boolean" };
|
|
172
|
+
case "String":
|
|
173
|
+
return { type: "string" };
|
|
174
|
+
case "Integer":
|
|
175
|
+
// A JSON number cannot round-trip the upper half of i64, so East JSON
|
|
176
|
+
// encodes Integer as a decimal string and the pattern pins the range.
|
|
177
|
+
return { type: "string", pattern: integerPattern(), "x-east-type": "Integer" };
|
|
178
|
+
case "Float":
|
|
179
|
+
return {
|
|
180
|
+
oneOf: [{ type: "number" }, { type: "string", enum: [...FLOAT_SPECIALS] }],
|
|
181
|
+
"x-east-type": "Float",
|
|
182
|
+
};
|
|
183
|
+
case "DateTime":
|
|
184
|
+
return {
|
|
185
|
+
type: "string",
|
|
186
|
+
format: "date-time",
|
|
187
|
+
pattern: DATETIME_PATTERN,
|
|
188
|
+
"x-east-type": "DateTime",
|
|
189
|
+
};
|
|
190
|
+
case "Blob":
|
|
191
|
+
return { type: "string", pattern: BLOB_PATTERN, "x-east-type": "Blob" };
|
|
192
|
+
case "Array":
|
|
193
|
+
return { type: "array", items: schemaOf(t.value, ctx) };
|
|
194
|
+
case "Set":
|
|
195
|
+
return {
|
|
196
|
+
type: "array",
|
|
197
|
+
items: schemaOf(t.value, ctx),
|
|
198
|
+
uniqueItems: true,
|
|
199
|
+
"x-east-type": "Set",
|
|
200
|
+
};
|
|
201
|
+
case "Vector":
|
|
202
|
+
return {
|
|
203
|
+
type: "array",
|
|
204
|
+
items: schemaOf(t.value, ctx),
|
|
205
|
+
"x-east-type": "Vector",
|
|
206
|
+
};
|
|
207
|
+
case "Matrix":
|
|
208
|
+
// Rows are equal-length, which no release can express without $data; the
|
|
209
|
+
// reader enforces it.
|
|
210
|
+
return {
|
|
211
|
+
type: "array",
|
|
212
|
+
items: { type: "array", items: schemaOf(t.value, ctx) },
|
|
213
|
+
"x-east-type": "Matrix",
|
|
214
|
+
};
|
|
215
|
+
case "Ref": {
|
|
216
|
+
// A Ref encodes as a one-element array. The encoder ALSO writes
|
|
217
|
+
// `{"$ref": …}` once the same target has been written before, but no
|
|
218
|
+
// reader resolves that back — a streaming reader cannot, having discarded
|
|
219
|
+
// what the pointer refers to — so advertising it would describe documents
|
|
220
|
+
// the reader then rejects. It is excluded for the same reason aliasing is
|
|
221
|
+
// excluded for Array/Set/Dict, and with the same consequence, stated in
|
|
222
|
+
// the docs: a value with shared references does not validate against its
|
|
223
|
+
// own published schema.
|
|
224
|
+
return {
|
|
225
|
+
type: "array",
|
|
226
|
+
items: schemaOf(t.value, ctx),
|
|
227
|
+
minItems: 1,
|
|
228
|
+
maxItems: 1,
|
|
229
|
+
"x-east-type": "Ref",
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
case "Dict": {
|
|
233
|
+
const d = t.value;
|
|
234
|
+
return {
|
|
235
|
+
type: "array",
|
|
236
|
+
items: {
|
|
237
|
+
type: "object",
|
|
238
|
+
properties: { key: schemaOf(d.key, ctx), value: schemaOf(d.value, ctx) },
|
|
239
|
+
required: ["key", "value"],
|
|
240
|
+
additionalProperties: false,
|
|
241
|
+
},
|
|
242
|
+
uniqueItems: true,
|
|
243
|
+
"x-east-type": "Dict",
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
case "Struct": {
|
|
247
|
+
const fields = t.value;
|
|
248
|
+
const properties = {};
|
|
249
|
+
for (const f of fields)
|
|
250
|
+
properties[f.name] = schemaOf(f.type, ctx);
|
|
251
|
+
return {
|
|
252
|
+
type: "object",
|
|
253
|
+
properties,
|
|
254
|
+
required: fields.map(f => f.name),
|
|
255
|
+
additionalProperties: false,
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
case "Variant": {
|
|
259
|
+
const flat = jsonFlatOptionPayload(t, ctx.inners);
|
|
260
|
+
if (flat !== null) {
|
|
261
|
+
// A flat Option: `null` never satisfies the payload's schema, so the
|
|
262
|
+
// oneOf is exact, and the annotation names what it came from.
|
|
263
|
+
return { oneOf: [nullSchema(ctx.draft), schemaOf(flat, ctx)], "x-east-type": "Option" };
|
|
264
|
+
}
|
|
265
|
+
const cases = t.value;
|
|
266
|
+
return {
|
|
267
|
+
oneOf: cases.map(c => ({
|
|
268
|
+
type: "object",
|
|
269
|
+
properties: {
|
|
270
|
+
// draft-04 (and so OpenAPI 3.0) has no `const`; a single-valued
|
|
271
|
+
// enum asserts the same thing.
|
|
272
|
+
type: ctx.draft === "openapi-3.0" ? { enum: [c.name] } : { const: c.name },
|
|
273
|
+
value: schemaOf(c.type, ctx),
|
|
274
|
+
},
|
|
275
|
+
required: ["type", "value"],
|
|
276
|
+
additionalProperties: false,
|
|
277
|
+
})),
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
case "Recursive": {
|
|
281
|
+
const rec = t.value;
|
|
282
|
+
if (rec.type === "ref") {
|
|
283
|
+
const name = ctx.names.get(rec.value);
|
|
284
|
+
if (name === undefined) {
|
|
285
|
+
throw new Error(`jsonSchemaFor: unresolved recursive reference ${rec.value}`);
|
|
286
|
+
}
|
|
287
|
+
return { $ref: `#/${defsKeyword(ctx.draft)}/${name}` };
|
|
288
|
+
}
|
|
289
|
+
const w = rec.value;
|
|
290
|
+
// Named by first-encounter order, never by type id: ids come from a
|
|
291
|
+
// process-global counter, so using them would make the document differ
|
|
292
|
+
// between runs and between languages.
|
|
293
|
+
const name = `Recursive${ctx.names.size + 1}`;
|
|
294
|
+
ctx.names.set(w.id, name);
|
|
295
|
+
ctx.inners.set(w.id, w.inner);
|
|
296
|
+
// Reserve the slot before recursing so a back-reference resolves.
|
|
297
|
+
ctx.defs[name] = {};
|
|
298
|
+
ctx.defs[name] = schemaOf(w.inner, ctx);
|
|
299
|
+
return { $ref: `#/${defsKeyword(ctx.draft)}/${name}` };
|
|
300
|
+
}
|
|
301
|
+
default:
|
|
302
|
+
throw new Error(`jsonSchemaFor: unhandled type ${t.type}`);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
//# sourceMappingURL=json_schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"json_schema.js","sourceRoot":"","sources":["../../../src/serialization/json_schema.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,eAAe,EAAsB,MAAM,oBAAoB,CAAC;AAEzE,OAAO,EAAE,SAAS,EAAE,MAAM,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