@amritk/adapters 0.4.3 → 0.4.4

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,10 +1,3 @@
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
1
  /**
9
2
  * Rewrites draft-07 tuples (`items` as an array, with an optional
10
3
  * `additionalItems` rest element) into 2020-12 form: `items` becomes
@@ -17,7 +10,11 @@ export declare const normalizeDraftTuples: (node: unknown) => void;
17
10
  * a too-short array (trailing positions unconstrained) and a too-long one
18
11
  * (nothing forbids extras). Restore the length: `minItems` forces the fixed
19
12
  * 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.
13
+ * `items: false` forbids extras.
14
+ *
15
+ * Only a *missing* `minItems` is filled in. An explicit one is the author
16
+ * saying which trailing positions are optional — Effect's `optionalElement`
17
+ * emits exactly that — and raising it to the tuple's length made those
18
+ * positions required, rejecting arrays the source schema accepts.
22
19
  */
23
20
  export declare const enforceTupleLength: (node: unknown) => void;
@@ -1,43 +1,44 @@
1
- const normalizeDraftTuples = (node) => {
1
+ import { entersSchemaMap, isDataPosition } from "@amritk/helpers/build-resource-registry";
2
+ const walkSchema = (node, transform, inSchemaMap = false) => {
2
3
  if (node === null || typeof node !== "object")
3
4
  return;
4
5
  if (Array.isArray(node)) {
5
6
  for (const item of node)
6
- normalizeDraftTuples(item);
7
+ walkSchema(item, transform, inSchemaMap);
7
8
  return;
8
9
  }
9
10
  const obj = node;
10
- if (Array.isArray(obj["items"])) {
11
+ if (!inSchemaMap)
12
+ transform(obj);
13
+ for (const key of Object.keys(obj)) {
14
+ if (isDataPosition(key, inSchemaMap))
15
+ continue;
16
+ walkSchema(obj[key], transform, entersSchemaMap(key, inSchemaMap));
17
+ }
18
+ };
19
+ const normalizeDraftTuples = (node) => {
20
+ walkSchema(node, (obj) => {
21
+ if (!Object.hasOwn(obj, "items") || !Array.isArray(obj["items"]))
22
+ return;
11
23
  obj["prefixItems"] = obj["items"];
12
- if ("additionalItems" in obj) {
24
+ if (Object.hasOwn(obj, "additionalItems")) {
13
25
  obj["items"] = obj["additionalItems"];
14
26
  delete obj["additionalItems"];
15
27
  } else {
16
28
  delete obj["items"];
17
29
  }
18
- }
19
- for (const value of Object.values(obj))
20
- normalizeDraftTuples(value);
30
+ });
21
31
  };
22
32
  const enforceTupleLength = (node) => {
23
- if (node === null || typeof node !== "object")
24
- return;
25
- if (Array.isArray(node)) {
26
- for (const item of node)
27
- enforceTupleLength(item);
28
- return;
29
- }
30
- const obj = node;
31
- if (Array.isArray(obj["prefixItems"])) {
33
+ walkSchema(node, (obj) => {
34
+ if (!Object.hasOwn(obj, "prefixItems") || !Array.isArray(obj["prefixItems"]))
35
+ return;
32
36
  const fixed = obj["prefixItems"].length;
33
- const min = typeof obj["minItems"] === "number" ? obj["minItems"] : 0;
34
- if (min < fixed)
37
+ if (fixed > 0 && (!Object.hasOwn(obj, "minItems") || typeof obj["minItems"] !== "number"))
35
38
  obj["minItems"] = fixed;
36
- if (!("items" in obj))
39
+ if (!Object.hasOwn(obj, "items"))
37
40
  obj["items"] = false;
38
- }
39
- for (const value of Object.values(obj))
40
- enforceTupleLength(value);
41
+ });
41
42
  };
42
43
  export {
43
44
  enforceTupleLength,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/adapters",
3
- "version": "0.4.3",
3
+ "version": "0.4.4",
4
4
  "description": "Convert schemas from external libraries (TypeBox, Zod, ...) into JSON Schema for mjst.",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -43,7 +43,7 @@
43
43
  },
44
44
  "dependencies": {
45
45
  "json-schema-typed": "^8.0.1",
46
- "@amritk/helpers": "^0.15.3"
46
+ "@amritk/helpers": "^0.15.4"
47
47
  },
48
48
  "exports": {
49
49
  "./package.json": "./package.json",