Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

zod-to-json-schema

Package Overview
Dependencies
Maintainers
1
Versions
81
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zod-to-json-schema - npm Package Compare versions

Comparing version 3.11.3 to 3.14.0-alpha.0

src/parsers/optional.d.ts

0

changelog.md

@@ -0,0 +0,0 @@ # Changelog

import { zodToJsonSchema } from "./src/zodToJsonSchema";
export default zodToJsonSchema;
export { zodToJsonSchema };

@@ -0,0 +0,0 @@ "use strict";

6

package.json
{
"name": "zod-to-json-schema",
"version": "3.11.3",
"version": "3.14.0-alpha.0",
"description": "Converts Zod schemas to Json Schemas",

@@ -31,4 +31,4 @@ "main": "index.js",

"license": "ISC",
"dependencies": {
"zod": "^3.11.6"
"peerDependencies": {
"zod": "^3.14.4"
},

@@ -35,0 +35,0 @@ "devDependencies": {

@@ -76,3 +76,3 @@ # Zod to Json Schema

- When using `.transform`, the return type is inferred from the supplied function. In other words, there is no schema for the return type, and there is no way to convert it in runtime. Currently the JSON schema will therefore reflect the input side of the Zod schema and not necessarily the output (the latter aka. `z.infer`). If this causes problems with your schema, consider using the effectStrategy "any", which will allow any type of output.
- JSON Schemas does not support any other key type than strings for objects. When using `z.record` with any other key type, this will be ignored.
- JSON Schemas does not support any other key type than strings for objects. When using `z.record` with any other key type, this will be ignored. An exception to this rule is `z.enum` as is supported since 3.11.3
- Relative JSON pointers, while published alongside [JSON schema draft 2020-12](https://json-schema.org/specification.html), is not technically a part of it. Currently, most resolvers do not handle them at all.

@@ -79,0 +79,0 @@

@@ -0,0 +0,0 @@ import { ZodTypeDef } from "zod";

@@ -5,2 +5,3 @@ "use strict";

const zod_1 = require("zod");
const optional_1 = require("./parsers/optional");
const any_1 = require("./parsers/any");

@@ -97,2 +98,3 @@ const array_1 = require("./parsers/array");

case zod_1.ZodFirstPartyTypeKind.ZodUnion:
case zod_1.ZodFirstPartyTypeKind.ZodDiscriminatedUnion:
return (0, union_1.parseUnionDef)(def, refs);

@@ -114,3 +116,3 @@ case zod_1.ZodFirstPartyTypeKind.ZodIntersection:

case zod_1.ZodFirstPartyTypeKind.ZodOptional:
return parseDef(def.innerType._def, refs);
return (0, optional_1.parseOptionalDef)(def, refs);
case zod_1.ZodFirstPartyTypeKind.ZodMap:

@@ -124,2 +126,3 @@ return (0, map_1.parseMapDef)(def, refs);

return (0, promise_1.parsePromiseDef)(def, refs);
case zod_1.ZodFirstPartyTypeKind.ZodNaN:
case zod_1.ZodFirstPartyTypeKind.ZodNever:

@@ -126,0 +129,0 @@ return (0, never_1.parseNeverDef)();

export declare type JsonSchema7AnyType = {};
export declare function parseAnyDef(): JsonSchema7AnyType;

@@ -0,0 +0,0 @@ "use strict";

@@ -0,0 +0,0 @@ import { ZodArrayDef } from "zod";

@@ -0,0 +0,0 @@ "use strict";

@@ -0,0 +0,0 @@ export declare type JsonSchema7BigintType = {

@@ -0,0 +0,0 @@ "use strict";

@@ -0,0 +0,0 @@ export declare type JsonSchema7BooleanType = {

@@ -0,0 +0,0 @@ "use strict";

@@ -0,0 +0,0 @@ export declare type JsonSchema7DateType = {

@@ -0,0 +0,0 @@ "use strict";

@@ -0,0 +0,0 @@ import { ZodDefaultDef } from "zod";

@@ -0,0 +0,0 @@ "use strict";

@@ -0,0 +0,0 @@ import { ZodEffectsDef } from "zod";

@@ -0,0 +0,0 @@ "use strict";

@@ -0,0 +0,0 @@ import { ZodEnumDef } from "zod";

@@ -0,0 +0,0 @@ "use strict";

@@ -0,0 +0,0 @@ import { ZodIntersectionDef } from "zod";

@@ -0,0 +0,0 @@ "use strict";

@@ -0,0 +0,0 @@ import { ZodLiteralDef } from "zod";

@@ -0,0 +0,0 @@ "use strict";

@@ -0,0 +0,0 @@ import { ZodMapDef } from "zod";

@@ -0,0 +0,0 @@ "use strict";

@@ -0,0 +0,0 @@ import { ZodNativeEnumDef } from "zod";

@@ -0,0 +0,0 @@ "use strict";

@@ -0,0 +0,0 @@ export declare type JsonSchema7NeverType = {

@@ -0,0 +0,0 @@ "use strict";

@@ -0,0 +0,0 @@ import { References } from "../References";

@@ -0,0 +0,0 @@ "use strict";

@@ -0,0 +0,0 @@ import { ZodNullableDef } from "zod";

@@ -0,0 +0,0 @@ "use strict";

@@ -0,0 +0,0 @@ import { ZodNumberDef } from "zod";

@@ -0,0 +0,0 @@ "use strict";

@@ -0,0 +0,0 @@ import { ZodObjectDef } from "zod";

@@ -10,3 +10,3 @@ "use strict";

return acc;
const parsedDef = (0, parseDef_1.parseDef)(propDef._def, refs.addToPath("properties", propName));
const parsedDef = (0, parseDef_1.parseDef)(propDef._def, refs.addToPathAsProperty("properties", propName));
if (parsedDef === undefined)

@@ -13,0 +13,0 @@ return acc;

@@ -0,0 +0,0 @@ import { ZodPromiseDef } from "zod";

@@ -0,0 +0,0 @@ "use strict";

@@ -0,0 +0,0 @@ import { ZodRecordDef, ZodTypeAny } from "zod";

@@ -0,0 +0,0 @@ "use strict";

@@ -7,3 +7,5 @@ import { ZodSetDef } from "zod";

items?: JsonSchema7Type;
minItems?: number;
maxItems?: number;
};
export declare function parseSetDef(def: ZodSetDef, refs: References): JsonSchema7SetType;

@@ -7,4 +7,14 @@ "use strict";

const items = (0, parseDef_1.parseDef)(def.valueType._def, refs.addToPath("items"));
return { type: "array", items };
const schema = {
type: "array",
items
};
if (def.minSize) {
schema.minItems = def.minSize.value;
}
if (def.maxSize) {
schema.maxItems = def.maxSize.value;
}
return schema;
}
exports.parseSetDef = parseSetDef;

@@ -0,0 +0,0 @@ import { ZodStringDef } from "zod";

@@ -0,0 +0,0 @@ "use strict";

@@ -0,0 +0,0 @@ import { ZodTupleDef, ZodTupleItems, ZodTypeAny } from "zod";

@@ -0,0 +0,0 @@ "use strict";

@@ -0,0 +0,0 @@ export declare type JsonSchema7UndefinedType = {

@@ -0,0 +0,0 @@ "use strict";

@@ -1,2 +0,2 @@

import { ZodUnionDef } from "zod";
import { ZodDiscriminatedUnionDef, ZodUnionDef } from "zod";
import { JsonSchema7Type } from "../parseDef";

@@ -22,3 +22,3 @@ import { References } from "../References";

};
export declare function parseUnionDef(def: ZodUnionDef, refs: References): JsonSchema7PrimitiveUnionType | JsonSchema7AnyOfType | undefined;
export declare function parseUnionDef(def: ZodUnionDef | ZodDiscriminatedUnionDef<any, any, any>, refs: References): JsonSchema7PrimitiveUnionType | JsonSchema7AnyOfType | undefined;
export {};

@@ -15,7 +15,8 @@ "use strict";

return asAnyOf(def, refs);
const options = def.options instanceof Map ? Array.from(def.options.values()) : def.options;
// This blocks tries to look ahead a bit to produce nicer looking schemas with type array instead of anyOf.
if (def.options.every((x) => x._def.typeName in exports.primitiveMappings &&
if (options.every((x) => x._def.typeName in exports.primitiveMappings &&
(!x._def.checks || !x._def.checks.length))) {
// all types in union are primitive and lack checks, so might as well squash into {type: [...]}
const types = def.options.reduce((types, x) => {
const types = options.reduce((types, x) => {
const type = exports.primitiveMappings[x._def.typeName]; //Can be safely casted due to row 43

@@ -28,5 +29,5 @@ return type && !types.includes(type) ? [...types, type] : types;

}
else if (def.options.every((x) => x._def.typeName === "ZodLiteral")) {
else if (options.every((x) => x._def.typeName === "ZodLiteral")) {
// all options literals
const types = def.options.reduce((acc, x) => {
const types = options.reduce((acc, x) => {
const type = typeof x._def.value;

@@ -50,3 +51,3 @@ switch (type) {

}, []);
if (types.length === def.options.length) {
if (types.length === options.length) {
// all the literals are primitive, as far as null can be considered primitive

@@ -56,3 +57,3 @@ const uniqueTypes = types.filter((x, i, a) => a.indexOf(x) === i);

type: uniqueTypes.length > 1 ? uniqueTypes : uniqueTypes[0],
enum: def.options.reduce((acc, x) => {
enum: options.reduce((acc, x) => {
return acc.includes(x._def.value) ? acc : [...acc, x._def.value];

@@ -63,6 +64,6 @@ }, []),

}
else if (def.options.every((x) => x._def.typeName === "ZodEnum")) {
else if (options.every((x) => x._def.typeName === "ZodEnum")) {
return {
type: "string",
enum: def.options.reduce((acc, x) => [
enum: options.reduce((acc, x) => [
...acc,

@@ -77,3 +78,3 @@ ...x._def.values.filter((x) => !acc.includes(x)),

const asAnyOf = (def, refs) => {
const anyOf = def.options
const anyOf = (def.options instanceof Map ? Array.from(def.options.values()) : def.options)
.map((x, i) => (0, parseDef_1.parseDef)(x._def, refs.addToPath("anyOf", i.toString())))

@@ -80,0 +81,0 @@ .filter((x) => !!x);

export declare type JsonSchema7UnknownType = {};
export declare function parseUnknownDef(): JsonSchema7UnknownType;

@@ -0,0 +0,0 @@ "use strict";

@@ -9,4 +9,6 @@ import { ZodTypeDef } from "zod";

target: Target;
constructor(path?: string[], items?: Item[], $refStrategy?: $refStrategy, effectStrategy?: EffectStrategy, target?: Target);
propertyPath: string[];
constructor(path?: string[], items?: Item[], $refStrategy?: $refStrategy, effectStrategy?: EffectStrategy, target?: Target, propertyPath?: string[]);
addToPath(...path: string[]): References;
addToPathAsProperty(...path: string[]): References;
}

@@ -13,0 +15,0 @@ export declare type Item = {

@@ -5,3 +5,3 @@ "use strict";

class References {
constructor(path = ["#"], items = [], $refStrategy = "root", effectStrategy = "input", target = "jsonSchema7") {
constructor(path = ["#"], items = [], $refStrategy = "root", effectStrategy = "input", target = "jsonSchema7", propertyPath = []) {
this.currentPath = path;

@@ -12,7 +12,11 @@ this.items = items;

this.target = target;
this.propertyPath = propertyPath;
}
addToPath(...path) {
return new References([...this.currentPath, ...path], this.items, this.$refStrategy, this.effectStrategy, this.target);
return new References([...this.currentPath, ...path], this.items, this.$refStrategy, this.effectStrategy, this.target, this.propertyPath);
}
addToPathAsProperty(...path) {
return new References([...this.currentPath, ...path], this.items, this.$refStrategy, this.effectStrategy, this.target, [...this.currentPath, ...path]);
}
}
exports.References = References;

@@ -0,0 +0,0 @@ import { ZodSchema } from "zod";

@@ -0,0 +0,0 @@ "use strict";

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc