@amritk/adapters 0.3.0 → 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { MJST_EXTENSION_KEY } from '@amritk/helpers/mjst-extension';
|
|
2
|
+
import { enforceTupleLength, normalizeDraftTuples } from './normalize-tuples.js';
|
|
2
3
|
/**
|
|
3
4
|
* Resolves `effect`'s `JSONSchema.make` from a runtime import, throwing a clear
|
|
4
5
|
* error when it is missing.
|
|
@@ -145,5 +146,11 @@ export const effectToJsonSchema = async (source) => {
|
|
|
145
146
|
// Reattach the hoisted definitions once the whole tree is assembled.
|
|
146
147
|
if (Object.keys(rootDefs).length > 0)
|
|
147
148
|
json['$defs'] = rootDefs;
|
|
149
|
+
// `JSONSchema.make` (and the structural rescue path) emit fixed tuples in
|
|
150
|
+
// draft-07 form (`items: [...]` + `additionalItems`), which the generators do
|
|
151
|
+
// not recognize as a tuple — element types and length would go unvalidated.
|
|
152
|
+
// Normalize to 2020-12 `prefixItems`, then restore the length bound.
|
|
153
|
+
normalizeDraftTuples(json);
|
|
154
|
+
enforceTupleLength(json);
|
|
148
155
|
return json;
|
|
149
156
|
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared tuple normalization for the adapters whose upstream converter emits
|
|
3
|
+
* JSON Schema in a shape the mjst pipeline does not key tuple validation off of.
|
|
4
|
+
* The pipeline recognizes a tuple only by 2020-12 `prefixItems`; a draft-07
|
|
5
|
+
* `items: [...]` array is treated as a plain array, so element types and length
|
|
6
|
+
* go unvalidated downstream. Both functions walk the whole tree.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Rewrites draft-07 tuples (`items` as an array, with an optional
|
|
10
|
+
* `additionalItems` rest element) into 2020-12 form: `items` becomes
|
|
11
|
+
* `prefixItems`, and `additionalItems` becomes `items` (its schema, or `false`).
|
|
12
|
+
* No-op on output that already uses `prefixItems`.
|
|
13
|
+
*/
|
|
14
|
+
export declare const normalizeDraftTuples: (node: unknown) => void;
|
|
15
|
+
/**
|
|
16
|
+
* A fixed tuple expressed as a bare `prefixItems` array (no length bound) accepts
|
|
17
|
+
* a too-short array (trailing positions unconstrained) and a too-long one
|
|
18
|
+
* (nothing forbids extras). Restore the length: `minItems` forces the fixed
|
|
19
|
+
* elements present, and — when the tuple has no rest element (no `items`) —
|
|
20
|
+
* `items: false` forbids extras. Applied to every `prefixItems` node; existing
|
|
21
|
+
* tighter bounds are never loosened.
|
|
22
|
+
*/
|
|
23
|
+
export declare const enforceTupleLength: (node: unknown) => void;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared tuple normalization for the adapters whose upstream converter emits
|
|
3
|
+
* JSON Schema in a shape the mjst pipeline does not key tuple validation off of.
|
|
4
|
+
* The pipeline recognizes a tuple only by 2020-12 `prefixItems`; a draft-07
|
|
5
|
+
* `items: [...]` array is treated as a plain array, so element types and length
|
|
6
|
+
* go unvalidated downstream. Both functions walk the whole tree.
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Rewrites draft-07 tuples (`items` as an array, with an optional
|
|
10
|
+
* `additionalItems` rest element) into 2020-12 form: `items` becomes
|
|
11
|
+
* `prefixItems`, and `additionalItems` becomes `items` (its schema, or `false`).
|
|
12
|
+
* No-op on output that already uses `prefixItems`.
|
|
13
|
+
*/
|
|
14
|
+
export const normalizeDraftTuples = (node) => {
|
|
15
|
+
if (node === null || typeof node !== 'object')
|
|
16
|
+
return;
|
|
17
|
+
if (Array.isArray(node)) {
|
|
18
|
+
for (const item of node)
|
|
19
|
+
normalizeDraftTuples(item);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const obj = node;
|
|
23
|
+
if (Array.isArray(obj['items'])) {
|
|
24
|
+
obj['prefixItems'] = obj['items'];
|
|
25
|
+
if ('additionalItems' in obj) {
|
|
26
|
+
// A rest element — its schema (or `false`) becomes `items`.
|
|
27
|
+
obj['items'] = obj['additionalItems'];
|
|
28
|
+
delete obj['additionalItems'];
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
// No rest element: drop `items` so `enforceTupleLength` forbids extras.
|
|
32
|
+
delete obj['items'];
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
for (const value of Object.values(obj))
|
|
36
|
+
normalizeDraftTuples(value);
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* A fixed tuple expressed as a bare `prefixItems` array (no length bound) accepts
|
|
40
|
+
* a too-short array (trailing positions unconstrained) and a too-long one
|
|
41
|
+
* (nothing forbids extras). Restore the length: `minItems` forces the fixed
|
|
42
|
+
* elements present, and — when the tuple has no rest element (no `items`) —
|
|
43
|
+
* `items: false` forbids extras. Applied to every `prefixItems` node; existing
|
|
44
|
+
* tighter bounds are never loosened.
|
|
45
|
+
*/
|
|
46
|
+
export const enforceTupleLength = (node) => {
|
|
47
|
+
if (node === null || typeof node !== 'object')
|
|
48
|
+
return;
|
|
49
|
+
if (Array.isArray(node)) {
|
|
50
|
+
for (const item of node)
|
|
51
|
+
enforceTupleLength(item);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
const obj = node;
|
|
55
|
+
if (Array.isArray(obj['prefixItems'])) {
|
|
56
|
+
const fixed = obj['prefixItems'].length;
|
|
57
|
+
const min = typeof obj['minItems'] === 'number' ? obj['minItems'] : 0;
|
|
58
|
+
if (min < fixed)
|
|
59
|
+
obj['minItems'] = fixed;
|
|
60
|
+
// No `items` keyword means no rest element: the array may not exceed the
|
|
61
|
+
// fixed tuple, so forbid additional items.
|
|
62
|
+
if (!('items' in obj))
|
|
63
|
+
obj['items'] = false;
|
|
64
|
+
}
|
|
65
|
+
for (const value of Object.values(obj))
|
|
66
|
+
enforceTupleLength(value);
|
|
67
|
+
};
|
|
@@ -51,6 +51,11 @@ export const valibotToJsonSchema = async (source, options) => {
|
|
|
51
51
|
try {
|
|
52
52
|
json = toJsonSchema(source, {
|
|
53
53
|
errorMode: 'ignore',
|
|
54
|
+
// The converter defaults to draft-07, which emits fixed tuples as
|
|
55
|
+
// `items: [...]`; the generators key tuple validation off 2020-12
|
|
56
|
+
// `prefixItems`, so a draft-07 tuple would be silently under-validated
|
|
57
|
+
// (element types and length unchecked). Target 2020-12 explicitly.
|
|
58
|
+
target: 'draft-2020-12',
|
|
54
59
|
overrideSchema: (ctx) => {
|
|
55
60
|
// Rescued constructs are represented, not widened — return before we
|
|
56
61
|
// record anything so `date`/`bigint` never show up as lossy.
|
|
@@ -77,7 +82,8 @@ export const valibotToJsonSchema = async (source, options) => {
|
|
|
77
82
|
// Surface everything that widened in one batched, branded notice (or throw in
|
|
78
83
|
// strict mode).
|
|
79
84
|
reportLossyConstructs('Valibot', droppedConstructs, options?.strict);
|
|
80
|
-
//
|
|
85
|
+
// Drop the dialect marker the converter emits; the generators infer the
|
|
86
|
+
// dialect from structure, not `$schema`.
|
|
81
87
|
delete json['$schema'];
|
|
82
88
|
return json;
|
|
83
89
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { MJST_EXTENSION_KEY } from '@amritk/helpers/mjst-extension';
|
|
2
|
+
import { enforceTupleLength, normalizeDraftTuples } from './normalize-tuples.js';
|
|
2
3
|
import { reportLossyConstructs } from './report-lossy-constructs.js';
|
|
3
4
|
// Zod types that have no JSON Schema equivalent and become "accept anything"
|
|
4
5
|
// (`{}`) — the same widening the Zod 4 path performs with `unrepresentable:
|
|
@@ -128,69 +129,6 @@ const convertWithFallback = (source, fallback, droppedTypes) => {
|
|
|
128
129
|
throw new Error(`Zod adapter failed to convert the schema. Is it a valid Zod schema?\n${String(error)}`);
|
|
129
130
|
}
|
|
130
131
|
};
|
|
131
|
-
/**
|
|
132
|
-
* `zod-to-json-schema` (the Zod 3 fallback) emits tuples in draft-07 form —
|
|
133
|
-
* `items` as an array plus `additionalItems` for the rest element. Rewrite those
|
|
134
|
-
* nodes into 2020-12 form (`prefixItems`, with `additionalItems` becoming
|
|
135
|
-
* `items`) so the rest of the pipeline — and {@link enforceTupleLength} below —
|
|
136
|
-
* sees the same shape the Zod 4 path produces. No-op on Zod 4 output, which
|
|
137
|
-
* already uses `prefixItems`.
|
|
138
|
-
*/
|
|
139
|
-
const normalizeDraftTuples = (node) => {
|
|
140
|
-
if (node === null || typeof node !== 'object')
|
|
141
|
-
return;
|
|
142
|
-
if (Array.isArray(node)) {
|
|
143
|
-
for (const item of node)
|
|
144
|
-
normalizeDraftTuples(item);
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
const obj = node;
|
|
148
|
-
if (Array.isArray(obj['items'])) {
|
|
149
|
-
obj['prefixItems'] = obj['items'];
|
|
150
|
-
if ('additionalItems' in obj) {
|
|
151
|
-
// A rest element (`.rest(...)`) — its schema (or `false`) becomes `items`.
|
|
152
|
-
obj['items'] = obj['additionalItems'];
|
|
153
|
-
delete obj['additionalItems'];
|
|
154
|
-
}
|
|
155
|
-
else {
|
|
156
|
-
// No rest element: drop `items` so `enforceTupleLength` forbids extras.
|
|
157
|
-
delete obj['items'];
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
for (const value of Object.values(obj))
|
|
161
|
-
normalizeDraftTuples(value);
|
|
162
|
-
};
|
|
163
|
-
/**
|
|
164
|
-
* Zod 4's `toJSONSchema` emits a fixed tuple as a bare `prefixItems` array with
|
|
165
|
-
* no length bound, so the result accepts a too-short array (positions past the
|
|
166
|
-
* end are simply unconstrained) and a too-long one (nothing forbids extra
|
|
167
|
-
* items). A Zod tuple requires exactly its fixed elements, so we restore that:
|
|
168
|
-
* `minItems` forces the fixed elements to be present, and — when the tuple has
|
|
169
|
-
* no `.rest(...)` (no `items`) — `items: false` forbids extras. Applied to every
|
|
170
|
-
* `prefixItems` node in the tree. Existing tighter bounds are never loosened.
|
|
171
|
-
*/
|
|
172
|
-
const enforceTupleLength = (node) => {
|
|
173
|
-
if (node === null || typeof node !== 'object')
|
|
174
|
-
return;
|
|
175
|
-
if (Array.isArray(node)) {
|
|
176
|
-
for (const item of node)
|
|
177
|
-
enforceTupleLength(item);
|
|
178
|
-
return;
|
|
179
|
-
}
|
|
180
|
-
const obj = node;
|
|
181
|
-
if (Array.isArray(obj['prefixItems'])) {
|
|
182
|
-
const fixed = obj['prefixItems'].length;
|
|
183
|
-
const min = typeof obj['minItems'] === 'number' ? obj['minItems'] : 0;
|
|
184
|
-
if (min < fixed)
|
|
185
|
-
obj['minItems'] = fixed;
|
|
186
|
-
// No `items` keyword means no rest element: the array may not exceed the
|
|
187
|
-
// fixed tuple, so forbid additional items.
|
|
188
|
-
if (!('items' in obj))
|
|
189
|
-
obj['items'] = false;
|
|
190
|
-
}
|
|
191
|
-
for (const value of Object.values(obj))
|
|
192
|
-
enforceTupleLength(value);
|
|
193
|
-
};
|
|
194
132
|
// Keys a branch may carry and still be treated as a plain "closed object" that
|
|
195
133
|
// is safe to merge. Anything else (patternProperties, a nested allOf, $ref, a
|
|
196
134
|
// conditional, …) makes the merge unsafe, so we leave such an allOf alone.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amritk/adapters",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Convert schemas from external libraries (TypeBox, Zod, ...) into JSON Schema for mjst.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -37,7 +37,7 @@
|
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"json-schema-typed": "^8.0.1",
|
|
40
|
-
"@amritk/helpers": "0.13.
|
|
40
|
+
"@amritk/helpers": "0.13.1"
|
|
41
41
|
},
|
|
42
42
|
"exports": {
|
|
43
43
|
"./package.json": "./package.json",
|