@atom8n/json-schema-to-zod 1.6.9 → 1.7.0

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 (76) hide show
  1. package/.turbo/turbo-build.log +2 -8
  2. package/dist/cjs/index.js +5 -0
  3. package/dist/cjs/json-schema-to-zod.js +12 -0
  4. package/dist/cjs/package.json +1 -0
  5. package/dist/cjs/parsers/parse-all-of.js +37 -0
  6. package/dist/cjs/parsers/parse-any-of.js +16 -0
  7. package/dist/cjs/parsers/parse-array.js +18 -0
  8. package/dist/cjs/parsers/parse-boolean.js +8 -0
  9. package/dist/cjs/parsers/parse-const.js +8 -0
  10. package/dist/cjs/parsers/parse-default.js +8 -0
  11. package/dist/cjs/parsers/parse-enum.js +18 -0
  12. package/dist/cjs/parsers/parse-if-then-else.js +23 -0
  13. package/dist/cjs/parsers/parse-multiple-type.js +9 -0
  14. package/dist/cjs/parsers/parse-not.js +12 -0
  15. package/dist/cjs/parsers/parse-null.js +8 -0
  16. package/dist/cjs/parsers/parse-nullable.js +12 -0
  17. package/dist/cjs/parsers/parse-number.js +49 -0
  18. package/dist/cjs/parsers/parse-object.js +206 -0
  19. package/dist/cjs/parsers/parse-one-of.js +32 -0
  20. package/dist/cjs/parsers/parse-schema.js +159 -0
  21. package/dist/cjs/parsers/parse-string.js +42 -0
  22. package/dist/cjs/types.js +2 -0
  23. package/dist/cjs/utils/extend-schema.js +11 -0
  24. package/dist/cjs/utils/half.js +7 -0
  25. package/dist/cjs/utils/its.js +23 -0
  26. package/dist/cjs/utils/omit.js +10 -0
  27. package/dist/esm/index.js +1 -0
  28. package/dist/esm/json-schema-to-zod.js +8 -0
  29. package/dist/esm/package.json +1 -0
  30. package/dist/esm/parsers/parse-all-of.js +34 -0
  31. package/dist/esm/parsers/parse-any-of.js +12 -0
  32. package/dist/esm/parsers/parse-array.js +14 -0
  33. package/dist/esm/parsers/parse-boolean.js +4 -0
  34. package/dist/esm/parsers/parse-const.js +4 -0
  35. package/dist/esm/parsers/parse-default.js +4 -0
  36. package/dist/esm/parsers/parse-enum.js +14 -0
  37. package/dist/esm/parsers/parse-if-then-else.js +19 -0
  38. package/dist/esm/parsers/parse-multiple-type.js +5 -0
  39. package/dist/esm/parsers/parse-not.js +8 -0
  40. package/dist/esm/parsers/parse-null.js +4 -0
  41. package/dist/esm/parsers/parse-nullable.js +8 -0
  42. package/dist/esm/parsers/parse-number.js +45 -0
  43. package/dist/esm/parsers/parse-object.js +170 -0
  44. package/dist/esm/parsers/parse-one-of.js +28 -0
  45. package/dist/esm/parsers/parse-schema.js +122 -0
  46. package/dist/esm/parsers/parse-string.js +38 -0
  47. package/dist/esm/types.js +1 -0
  48. package/dist/esm/utils/extend-schema.js +8 -0
  49. package/dist/esm/utils/half.js +3 -0
  50. package/dist/esm/utils/its.js +20 -0
  51. package/dist/esm/utils/omit.js +6 -0
  52. package/dist/types/index.d.ts +2 -0
  53. package/dist/types/json-schema-to-zod.d.ts +3 -0
  54. package/dist/types/parsers/parse-all-of.d.ts +5 -0
  55. package/dist/types/parsers/parse-any-of.d.ts +5 -0
  56. package/dist/types/parsers/parse-array.d.ts +5 -0
  57. package/dist/types/parsers/parse-boolean.d.ts +5 -0
  58. package/dist/types/parsers/parse-const.d.ts +5 -0
  59. package/dist/types/parsers/parse-default.d.ts +3 -0
  60. package/dist/types/parsers/parse-enum.d.ts +5 -0
  61. package/dist/types/parsers/parse-if-then-else.d.ts +7 -0
  62. package/dist/types/parsers/parse-multiple-type.d.ts +5 -0
  63. package/dist/types/parsers/parse-not.d.ts +5 -0
  64. package/dist/types/parsers/parse-null.d.ts +5 -0
  65. package/dist/types/parsers/parse-nullable.d.ts +7 -0
  66. package/dist/types/parsers/parse-number.d.ts +5 -0
  67. package/dist/types/parsers/parse-object.d.ts +5 -0
  68. package/dist/types/parsers/parse-one-of.d.ts +5 -0
  69. package/dist/types/parsers/parse-schema.d.ts +3 -0
  70. package/dist/types/parsers/parse-string.d.ts +5 -0
  71. package/dist/types/types.d.ts +66 -0
  72. package/dist/types/utils/extend-schema.d.ts +3 -0
  73. package/dist/types/utils/half.d.ts +1 -0
  74. package/dist/types/utils/its.d.ts +45 -0
  75. package/dist/types/utils/omit.d.ts +1 -0
  76. package/package.json +3 -3
@@ -1,11 +1,5 @@
1
1
 
2
2
  
3
- > @n8n/json-schema-to-zod@1.6.7 build /Users/Shared/Data/Syncthing/MyApp/n8n-solution/n8n/packages/@n8n/json-schema-to-zod
4
- > rimraf ./dist && pnpm run build:types && pnpm run build:cjs && pnpm run build:esm
3
+ > @n8n/json-schema-to-zod@1.6.4 build /Users/Shared/Data/Syncthing/MyApp/n8n-solution/n8n/packages/@n8n/json-schema-to-zod
4
+ > rimraf ./dist && tsc -p tsconfig.types.json && sleep 2 && tsc -p tsconfig.cjs.json && node postcjs.cjs && sleep 2 && tsc -p tsconfig.esm.json && node postesm.cjs
5
5
 
6
-
7
- > @n8n/json-schema-to-zod@1.6.7 build:types /Users/Shared/Data/Syncthing/MyApp/n8n-solution/n8n/packages/@n8n/json-schema-to-zod
8
- > tsc -p tsconfig.types.json
9
-
10
-  ELIFECYCLE  Command failed.
11
-  ELIFECYCLE  Command failed.
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.jsonSchemaToZod = void 0;
4
+ var json_schema_to_zod_1 = require("./json-schema-to-zod");
5
+ Object.defineProperty(exports, "jsonSchemaToZod", { enumerable: true, get: function () { return json_schema_to_zod_1.jsonSchemaToZod; } });
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.jsonSchemaToZod = void 0;
4
+ const parse_schema_1 = require("./parsers/parse-schema");
5
+ const jsonSchemaToZod = (schema, options = {}) => {
6
+ return (0, parse_schema_1.parseSchema)(schema, {
7
+ path: [],
8
+ seen: new Map(),
9
+ ...options,
10
+ });
11
+ };
12
+ exports.jsonSchemaToZod = jsonSchemaToZod;
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseAllOf = parseAllOf;
4
+ const zod_1 = require("zod");
5
+ const parse_schema_1 = require("./parse-schema");
6
+ const half_1 = require("../utils/half");
7
+ const originalIndex = Symbol('Original index');
8
+ const ensureOriginalIndex = (arr) => {
9
+ const newArr = [];
10
+ for (let i = 0; i < arr.length; i++) {
11
+ const item = arr[i];
12
+ if (typeof item === 'boolean') {
13
+ newArr.push(item ? { [originalIndex]: i } : { [originalIndex]: i, not: {} });
14
+ }
15
+ else if (originalIndex in item) {
16
+ return arr;
17
+ }
18
+ else {
19
+ newArr.push({ ...item, [originalIndex]: i });
20
+ }
21
+ }
22
+ return newArr;
23
+ };
24
+ function parseAllOf(jsonSchema, refs) {
25
+ if (jsonSchema.allOf.length === 0) {
26
+ return zod_1.z.never();
27
+ }
28
+ if (jsonSchema.allOf.length === 1) {
29
+ const item = jsonSchema.allOf[0];
30
+ return (0, parse_schema_1.parseSchema)(item, {
31
+ ...refs,
32
+ path: [...refs.path, 'allOf', item[originalIndex]],
33
+ });
34
+ }
35
+ const [left, right] = (0, half_1.half)(ensureOriginalIndex(jsonSchema.allOf));
36
+ return zod_1.z.intersection(parseAllOf({ allOf: left }, refs), parseAllOf({ allOf: right }, refs));
37
+ }
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseAnyOf = void 0;
4
+ const zod_1 = require("zod");
5
+ const parse_schema_1 = require("./parse-schema");
6
+ const parseAnyOf = (jsonSchema, refs) => {
7
+ return jsonSchema.anyOf.length
8
+ ? jsonSchema.anyOf.length === 1
9
+ ? (0, parse_schema_1.parseSchema)(jsonSchema.anyOf[0], {
10
+ ...refs,
11
+ path: [...refs.path, 'anyOf', 0],
12
+ })
13
+ : zod_1.z.union(jsonSchema.anyOf.map((schema, i) => (0, parse_schema_1.parseSchema)(schema, { ...refs, path: [...refs.path, 'anyOf', i] })))
14
+ : zod_1.z.any();
15
+ };
16
+ exports.parseAnyOf = parseAnyOf;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseArray = void 0;
4
+ const zod_1 = require("zod");
5
+ const parse_schema_1 = require("./parse-schema");
6
+ const extend_schema_1 = require("../utils/extend-schema");
7
+ const parseArray = (jsonSchema, refs) => {
8
+ if (Array.isArray(jsonSchema.items)) {
9
+ return zod_1.z.tuple(jsonSchema.items.map((v, i) => (0, parse_schema_1.parseSchema)(v, { ...refs, path: [...refs.path, 'items', i] })));
10
+ }
11
+ let zodSchema = !jsonSchema.items
12
+ ? zod_1.z.array(zod_1.z.any())
13
+ : zod_1.z.array((0, parse_schema_1.parseSchema)(jsonSchema.items, { ...refs, path: [...refs.path, 'items'] }));
14
+ zodSchema = (0, extend_schema_1.extendSchemaWithMessage)(zodSchema, jsonSchema, 'minItems', (zs, minItems, errorMessage) => zs.min(minItems, errorMessage));
15
+ zodSchema = (0, extend_schema_1.extendSchemaWithMessage)(zodSchema, jsonSchema, 'maxItems', (zs, maxItems, errorMessage) => zs.max(maxItems, errorMessage));
16
+ return zodSchema;
17
+ };
18
+ exports.parseArray = parseArray;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseBoolean = void 0;
4
+ const zod_1 = require("zod");
5
+ const parseBoolean = (_jsonSchema) => {
6
+ return zod_1.z.boolean();
7
+ };
8
+ exports.parseBoolean = parseBoolean;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseConst = void 0;
4
+ const zod_1 = require("zod");
5
+ const parseConst = (jsonSchema) => {
6
+ return zod_1.z.literal(jsonSchema.const);
7
+ };
8
+ exports.parseConst = parseConst;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseDefault = void 0;
4
+ const zod_1 = require("zod");
5
+ const parseDefault = (_jsonSchema) => {
6
+ return zod_1.z.any();
7
+ };
8
+ exports.parseDefault = parseDefault;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseEnum = void 0;
4
+ const zod_1 = require("zod");
5
+ const parseEnum = (jsonSchema) => {
6
+ if (jsonSchema.enum.length === 0) {
7
+ return zod_1.z.never();
8
+ }
9
+ if (jsonSchema.enum.length === 1) {
10
+ // union does not work when there is only one element
11
+ return zod_1.z.literal(jsonSchema.enum[0]);
12
+ }
13
+ if (jsonSchema.enum.every((x) => typeof x === 'string')) {
14
+ return zod_1.z.enum(jsonSchema.enum);
15
+ }
16
+ return zod_1.z.union(jsonSchema.enum.map((x) => zod_1.z.literal(x)));
17
+ };
18
+ exports.parseEnum = parseEnum;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseIfThenElse = void 0;
4
+ const zod_1 = require("zod");
5
+ const parse_schema_1 = require("./parse-schema");
6
+ const parseIfThenElse = (jsonSchema, refs) => {
7
+ const $if = (0, parse_schema_1.parseSchema)(jsonSchema.if, { ...refs, path: [...refs.path, 'if'] });
8
+ const $then = (0, parse_schema_1.parseSchema)(jsonSchema.then, {
9
+ ...refs,
10
+ path: [...refs.path, 'then'],
11
+ });
12
+ const $else = (0, parse_schema_1.parseSchema)(jsonSchema.else, {
13
+ ...refs,
14
+ path: [...refs.path, 'else'],
15
+ });
16
+ return zod_1.z.union([$then, $else]).superRefine((value, ctx) => {
17
+ const result = $if.safeParse(value).success ? $then.safeParse(value) : $else.safeParse(value);
18
+ if (!result.success) {
19
+ result.error.errors.forEach((error) => ctx.addIssue(error));
20
+ }
21
+ });
22
+ };
23
+ exports.parseIfThenElse = parseIfThenElse;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseMultipleType = void 0;
4
+ const zod_1 = require("zod");
5
+ const parse_schema_1 = require("./parse-schema");
6
+ const parseMultipleType = (jsonSchema, refs) => {
7
+ return zod_1.z.union(jsonSchema.type.map((type) => (0, parse_schema_1.parseSchema)({ ...jsonSchema, type }, refs)));
8
+ };
9
+ exports.parseMultipleType = parseMultipleType;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseNot = void 0;
4
+ const zod_1 = require("zod");
5
+ const parse_schema_1 = require("./parse-schema");
6
+ const parseNot = (jsonSchema, refs) => {
7
+ return zod_1.z.any().refine((value) => !(0, parse_schema_1.parseSchema)(jsonSchema.not, {
8
+ ...refs,
9
+ path: [...refs.path, 'not'],
10
+ }).safeParse(value).success, 'Invalid input: Should NOT be valid against schema');
11
+ };
12
+ exports.parseNot = parseNot;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseNull = void 0;
4
+ const zod_1 = require("zod");
5
+ const parseNull = (_jsonSchema) => {
6
+ return zod_1.z.null();
7
+ };
8
+ exports.parseNull = parseNull;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseNullable = void 0;
4
+ const parse_schema_1 = require("./parse-schema");
5
+ const omit_1 = require("../utils/omit");
6
+ /**
7
+ * For compatibility with open api 3.0 nullable
8
+ */
9
+ const parseNullable = (jsonSchema, refs) => {
10
+ return (0, parse_schema_1.parseSchema)((0, omit_1.omit)(jsonSchema, 'nullable'), refs, true).nullable();
11
+ };
12
+ exports.parseNullable = parseNullable;
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseNumber = void 0;
4
+ const zod_1 = require("zod");
5
+ const extend_schema_1 = require("../utils/extend-schema");
6
+ const parseNumber = (jsonSchema) => {
7
+ let zodSchema = zod_1.z.number();
8
+ let isInteger = false;
9
+ if (jsonSchema.type === 'integer') {
10
+ isInteger = true;
11
+ zodSchema = (0, extend_schema_1.extendSchemaWithMessage)(zodSchema, jsonSchema, 'type', (zs, _, errorMsg) => zs.int(errorMsg));
12
+ }
13
+ else if (jsonSchema.format === 'int64') {
14
+ isInteger = true;
15
+ zodSchema = (0, extend_schema_1.extendSchemaWithMessage)(zodSchema, jsonSchema, 'format', (zs, _, errorMsg) => zs.int(errorMsg));
16
+ }
17
+ zodSchema = (0, extend_schema_1.extendSchemaWithMessage)(zodSchema, jsonSchema, 'multipleOf', (zs, multipleOf, errorMsg) => {
18
+ if (multipleOf === 1) {
19
+ if (isInteger)
20
+ return zs;
21
+ return zs.int(errorMsg);
22
+ }
23
+ return zs.multipleOf(multipleOf, errorMsg);
24
+ });
25
+ if (typeof jsonSchema.minimum === 'number') {
26
+ if (jsonSchema.exclusiveMinimum === true) {
27
+ zodSchema = (0, extend_schema_1.extendSchemaWithMessage)(zodSchema, jsonSchema, 'minimum', (zs, minimum, errorMsg) => zs.gt(minimum, errorMsg));
28
+ }
29
+ else {
30
+ zodSchema = (0, extend_schema_1.extendSchemaWithMessage)(zodSchema, jsonSchema, 'minimum', (zs, minimum, errorMsg) => zs.gte(minimum, errorMsg));
31
+ }
32
+ }
33
+ else if (typeof jsonSchema.exclusiveMinimum === 'number') {
34
+ zodSchema = (0, extend_schema_1.extendSchemaWithMessage)(zodSchema, jsonSchema, 'exclusiveMinimum', (zs, exclusiveMinimum, errorMsg) => zs.gt(exclusiveMinimum, errorMsg));
35
+ }
36
+ if (typeof jsonSchema.maximum === 'number') {
37
+ if (jsonSchema.exclusiveMaximum === true) {
38
+ zodSchema = (0, extend_schema_1.extendSchemaWithMessage)(zodSchema, jsonSchema, 'maximum', (zs, maximum, errorMsg) => zs.lt(maximum, errorMsg));
39
+ }
40
+ else {
41
+ zodSchema = (0, extend_schema_1.extendSchemaWithMessage)(zodSchema, jsonSchema, 'maximum', (zs, maximum, errorMsg) => zs.lte(maximum, errorMsg));
42
+ }
43
+ }
44
+ else if (typeof jsonSchema.exclusiveMaximum === 'number') {
45
+ zodSchema = (0, extend_schema_1.extendSchemaWithMessage)(zodSchema, jsonSchema, 'exclusiveMaximum', (zs, exclusiveMaximum, errorMsg) => zs.lt(exclusiveMaximum, errorMsg));
46
+ }
47
+ return zodSchema;
48
+ };
49
+ exports.parseNumber = parseNumber;
@@ -0,0 +1,206 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.parseObject = parseObject;
37
+ const z = __importStar(require("zod"));
38
+ const parse_all_of_1 = require("./parse-all-of");
39
+ const parse_any_of_1 = require("./parse-any-of");
40
+ const parse_one_of_1 = require("./parse-one-of");
41
+ const parse_schema_1 = require("./parse-schema");
42
+ const its_1 = require("../utils/its");
43
+ function parseObjectProperties(objectSchema, refs) {
44
+ if (!objectSchema.properties) {
45
+ return undefined;
46
+ }
47
+ const propertyKeys = Object.keys(objectSchema.properties);
48
+ if (propertyKeys.length === 0) {
49
+ return z.object({});
50
+ }
51
+ const properties = {};
52
+ for (const key of propertyKeys) {
53
+ const propJsonSchema = objectSchema.properties[key];
54
+ const propZodSchema = (0, parse_schema_1.parseSchema)(propJsonSchema, {
55
+ ...refs,
56
+ path: [...refs.path, 'properties', key],
57
+ });
58
+ const hasDefault = typeof propJsonSchema === 'object' && propJsonSchema.default !== undefined;
59
+ const required = Array.isArray(objectSchema.required)
60
+ ? objectSchema.required.includes(key)
61
+ : typeof propJsonSchema === 'object' && propJsonSchema.required === true;
62
+ const isOptional = !hasDefault && !required;
63
+ properties[key] = isOptional ? propZodSchema.optional() : propZodSchema;
64
+ }
65
+ return z.object(properties);
66
+ }
67
+ function parseObject(objectSchema, refs) {
68
+ const hasPatternProperties = Object.keys(objectSchema.patternProperties ?? {}).length > 0;
69
+ const propertiesSchema = parseObjectProperties(objectSchema, refs);
70
+ let zodSchema = propertiesSchema;
71
+ const additionalProperties = objectSchema.additionalProperties !== undefined
72
+ ? (0, parse_schema_1.parseSchema)(objectSchema.additionalProperties, {
73
+ ...refs,
74
+ path: [...refs.path, 'additionalProperties'],
75
+ })
76
+ : undefined;
77
+ if (objectSchema.patternProperties) {
78
+ const parsedPatternProperties = Object.fromEntries(Object.entries(objectSchema.patternProperties).map(([key, value]) => {
79
+ return [
80
+ key,
81
+ (0, parse_schema_1.parseSchema)(value, {
82
+ ...refs,
83
+ path: [...refs.path, 'patternProperties', key],
84
+ }),
85
+ ];
86
+ }));
87
+ const patternPropertyValues = Object.values(parsedPatternProperties);
88
+ if (propertiesSchema) {
89
+ if (additionalProperties) {
90
+ zodSchema = propertiesSchema.catchall(z.union([...patternPropertyValues, additionalProperties]));
91
+ }
92
+ else if (Object.keys(parsedPatternProperties).length > 1) {
93
+ zodSchema = propertiesSchema.catchall(z.union(patternPropertyValues));
94
+ }
95
+ else {
96
+ zodSchema = propertiesSchema.catchall(patternPropertyValues[0]);
97
+ }
98
+ }
99
+ else {
100
+ if (additionalProperties) {
101
+ zodSchema = z.record(z.union([...patternPropertyValues, additionalProperties]));
102
+ }
103
+ else if (patternPropertyValues.length > 1) {
104
+ zodSchema = z.record(z.union(patternPropertyValues));
105
+ }
106
+ else {
107
+ zodSchema = z.record(patternPropertyValues[0]);
108
+ }
109
+ }
110
+ const objectPropertyKeys = new Set(Object.keys(objectSchema.properties ?? {}));
111
+ zodSchema = zodSchema.superRefine((value, ctx) => {
112
+ for (const key in value) {
113
+ let wasMatched = objectPropertyKeys.has(key);
114
+ for (const patternPropertyKey in objectSchema.patternProperties) {
115
+ const regex = new RegExp(patternPropertyKey);
116
+ if (key.match(regex)) {
117
+ wasMatched = true;
118
+ const result = parsedPatternProperties[patternPropertyKey].safeParse(value[key]);
119
+ if (!result.success) {
120
+ ctx.addIssue({
121
+ path: [...ctx.path, key],
122
+ code: 'custom',
123
+ message: `Invalid input: Key matching regex /${key}/ must match schema`,
124
+ params: {
125
+ issues: result.error.issues,
126
+ },
127
+ });
128
+ }
129
+ }
130
+ }
131
+ if (!wasMatched && additionalProperties) {
132
+ const result = additionalProperties.safeParse(value[key]);
133
+ if (!result.success) {
134
+ ctx.addIssue({
135
+ path: [...ctx.path, key],
136
+ code: 'custom',
137
+ message: 'Invalid input: must match catchall schema',
138
+ params: {
139
+ issues: result.error.issues,
140
+ },
141
+ });
142
+ }
143
+ }
144
+ }
145
+ });
146
+ }
147
+ let output;
148
+ if (propertiesSchema) {
149
+ if (hasPatternProperties) {
150
+ output = zodSchema;
151
+ }
152
+ else if (additionalProperties) {
153
+ if (additionalProperties instanceof z.ZodNever) {
154
+ output = propertiesSchema.strict();
155
+ }
156
+ else {
157
+ output = propertiesSchema.catchall(additionalProperties);
158
+ }
159
+ }
160
+ else {
161
+ output = zodSchema;
162
+ }
163
+ }
164
+ else {
165
+ if (hasPatternProperties) {
166
+ output = zodSchema;
167
+ }
168
+ else if (additionalProperties) {
169
+ output = z.record(additionalProperties);
170
+ }
171
+ else {
172
+ output = z.record(z.any());
173
+ }
174
+ }
175
+ if (its_1.its.an.anyOf(objectSchema)) {
176
+ output = output.and((0, parse_any_of_1.parseAnyOf)({
177
+ ...objectSchema,
178
+ anyOf: objectSchema.anyOf.map((x) => typeof x === 'object' &&
179
+ !x.type &&
180
+ (x.properties ?? x.additionalProperties ?? x.patternProperties)
181
+ ? { ...x, type: 'object' }
182
+ : x),
183
+ }, refs));
184
+ }
185
+ if (its_1.its.a.oneOf(objectSchema)) {
186
+ output = output.and((0, parse_one_of_1.parseOneOf)({
187
+ ...objectSchema,
188
+ oneOf: objectSchema.oneOf.map((x) => typeof x === 'object' &&
189
+ !x.type &&
190
+ (x.properties ?? x.additionalProperties ?? x.patternProperties)
191
+ ? { ...x, type: 'object' }
192
+ : x),
193
+ }, refs));
194
+ }
195
+ if (its_1.its.an.allOf(objectSchema)) {
196
+ output = output.and((0, parse_all_of_1.parseAllOf)({
197
+ ...objectSchema,
198
+ allOf: objectSchema.allOf.map((x) => typeof x === 'object' &&
199
+ !x.type &&
200
+ (x.properties ?? x.additionalProperties ?? x.patternProperties)
201
+ ? { ...x, type: 'object' }
202
+ : x),
203
+ }, refs));
204
+ }
205
+ return output;
206
+ }
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseOneOf = void 0;
4
+ const zod_1 = require("zod");
5
+ const parse_schema_1 = require("./parse-schema");
6
+ const parseOneOf = (jsonSchema, refs) => {
7
+ if (!jsonSchema.oneOf.length) {
8
+ return zod_1.z.any();
9
+ }
10
+ if (jsonSchema.oneOf.length === 1) {
11
+ return (0, parse_schema_1.parseSchema)(jsonSchema.oneOf[0], {
12
+ ...refs,
13
+ path: [...refs.path, 'oneOf', 0],
14
+ });
15
+ }
16
+ return zod_1.z.any().superRefine((x, ctx) => {
17
+ const schemas = jsonSchema.oneOf.map((schema, i) => (0, parse_schema_1.parseSchema)(schema, {
18
+ ...refs,
19
+ path: [...refs.path, 'oneOf', i],
20
+ }));
21
+ const unionErrors = schemas.reduce((errors, schema) => ((result) => (result.error ? [...errors, result.error] : errors))(schema.safeParse(x)), []);
22
+ if (schemas.length - unionErrors.length !== 1) {
23
+ ctx.addIssue({
24
+ path: ctx.path,
25
+ code: 'invalid_union',
26
+ unionErrors,
27
+ message: 'Invalid input: Should pass single schema',
28
+ });
29
+ }
30
+ });
31
+ };
32
+ exports.parseOneOf = parseOneOf;