Skip to content

Commit

Permalink
Introduced new target jsonSchema2019-09
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy2003 committed May 10, 2023
1 parent c0e8b39 commit 35f1785
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
6 changes: 4 additions & 2 deletions src/Options.ts
@@ -1,6 +1,8 @@
import { ZodSchema } from "zod";

export type Options<Target extends "jsonSchema7" | "openApi3" = "jsonSchema7"> =
export type Targets = "jsonSchema7" | "jsonSchema2019-09"| "openApi3"

export type Options<Target extends Targets = "jsonSchema7"> =
{
name: string | undefined;
$refStrategy: "root" | "relative" | "none";
Expand All @@ -27,7 +29,7 @@ export const defaultOptions: Options = {
errorMessages: false,
};

export const getDefaultOptions = <Target extends "jsonSchema7" | "openApi3">(
export const getDefaultOptions = <Target extends Targets>(
options: Partial<Options<Target>> | string | undefined
) =>
(typeof options === "string"
Expand Down
6 changes: 3 additions & 3 deletions src/Refs.ts
@@ -1,12 +1,12 @@
import { ZodTypeDef } from "zod";
import { getDefaultOptions, Options } from "./Options";
import { getDefaultOptions, Options, Targets } from "./Options";
import { JsonSchema7Type } from "./parseDef";

export type Refs = {
seen: Map<ZodTypeDef, Seen>;
currentPath: string[];
propertyPath: string[] | undefined;
} & Options<"jsonSchema7" | "openApi3">;
} & Options<Targets>;

export type Seen = {
def: ZodTypeDef;
Expand All @@ -15,7 +15,7 @@ export type Seen = {
};

export const getRefs = (
options?: string | Partial<Options<"jsonSchema7" | "openApi3">>
options?: string | Partial<Options<Targets>>
): Refs => {
const _options = getDefaultOptions(options);
const currentPath =
Expand Down
4 changes: 3 additions & 1 deletion src/parsers/intersection.ts
Expand Up @@ -29,7 +29,9 @@ export function parseIntersectionDef(
].filter((x): x is JsonSchema7Type => !!x);


let unevaluatedProperties: { unevaluatedProperties: boolean } | undefined = {unevaluatedProperties: false};
let unevaluatedProperties: Pick<JsonSchema7AllOfType, 'unevaluatedProperties'> | undefined =
refs.target === 'jsonSchema2019-09' ? {unevaluatedProperties: false} : undefined;

const mergedAllOf: JsonSchema7Type[] = []
// If either of the schemas is an allOf, merge them into a single allOf
allOf.forEach((schema) => {
Expand Down
8 changes: 5 additions & 3 deletions src/zodToJsonSchema.ts
@@ -1,17 +1,17 @@
import { ZodSchema } from "zod";
import { Options } from "./Options";
import { Options, Targets } from "./Options";
import { JsonSchema7Type, parseDef } from "./parseDef";
import { getRefs } from "./Refs";

const zodToJsonSchema = <
Target extends "jsonSchema7" | "openApi3" = "jsonSchema7"
Target extends Targets = "jsonSchema7"
>(
schema: ZodSchema<any>,
options?: Partial<Options<Target>> | string
): (Target extends "jsonSchema7" ? JsonSchema7Type : object) & {
$schema?: string;
definitions?: {
[key: string]: Target extends "jsonSchema7" ? JsonSchema7Type : object;
[key: string]: Target extends "jsonSchema7" ? JsonSchema7Type : Target extends "jsonSchema2019-09" ? JsonSchema7Type: object;
};
} => {
const refs = getRefs(options);
Expand Down Expand Up @@ -66,6 +66,8 @@ const zodToJsonSchema = <

if (refs.target === "jsonSchema7") {
combined.$schema = "http://json-schema.org/draft-07/schema#";
} else if (refs.target === "jsonSchema2019-09") {
combined.$schema = "https://json-schema.org/draft/2019-09/schema#";
}

return combined;
Expand Down
8 changes: 4 additions & 4 deletions test/parsers/intersection.test.ts
Expand Up @@ -47,7 +47,7 @@ describe("intersections", () => {
bar: z.string()
});
const intersection = z.intersection(schema1, schema2);
const jsonSchema = parseIntersectionDef(intersection._def, getRefs());
const jsonSchema = parseIntersectionDef(intersection._def, getRefs({target: "jsonSchema2019-09"}));

expect(jsonSchema).toStrictEqual({
allOf: [
Expand Down Expand Up @@ -82,7 +82,7 @@ describe("intersections", () => {
bar: z.string()
}).passthrough();
const intersection = z.intersection(schema1, schema2);
const jsonSchema = parseIntersectionDef(intersection._def, getRefs());
const jsonSchema = parseIntersectionDef(intersection._def, getRefs({target: "jsonSchema2019-09"}));

expect(jsonSchema).toStrictEqual({
allOf: [
Expand Down Expand Up @@ -120,7 +120,7 @@ describe("intersections", () => {
baz: z.string()
});
const intersection = schema1.and(schema2).and(schema3);
const jsonSchema = parseIntersectionDef(intersection._def, getRefs());
const jsonSchema = parseIntersectionDef(intersection._def, getRefs({target: "jsonSchema2019-09"}));

expect(jsonSchema).toStrictEqual({
allOf: [
Expand Down Expand Up @@ -167,7 +167,7 @@ describe("intersections", () => {
baz: z.string()
}).passthrough();
const intersection = schema1.and(schema2).and(schema3);
const jsonSchema = parseIntersectionDef(intersection._def, getRefs());
const jsonSchema = parseIntersectionDef(intersection._def, getRefs({target: "jsonSchema2019-09"}));

expect(jsonSchema).toStrictEqual({
allOf: [
Expand Down

0 comments on commit 35f1785

Please sign in to comment.