zod-openapi
Advanced tools
Comparing version 0.4.2 to 0.4.3
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createNumberSchema = void 0; | ||
exports.mapMinimum = exports.mapMaximum = exports.mapNumberChecks = exports.createNumberSchema = void 0; | ||
const openapi_1 = require("../../openapi"); | ||
const createNumberSchema = (zodNumber, components) => { | ||
const zodNumberChecks = getZodNumberChecks(zodNumber); | ||
const minimum = (0, exports.mapMinimum)(zodNumberChecks, components.openapi); | ||
const maximum = (0, exports.mapMaximum)(zodNumberChecks, components.openapi); | ||
return { | ||
type: mapNumberType(zodNumberChecks), | ||
...mapRanges(zodNumberChecks, components.openapi), // union types are too pesky to drill through | ||
...(minimum && minimum), | ||
...(maximum && maximum), | ||
}; | ||
}; | ||
exports.createNumberSchema = createNumberSchema; | ||
const mapRanges = (zodNumberChecks, openapi) => { | ||
const minimum = zodNumberChecks.min?.value; | ||
const maximum = zodNumberChecks.max?.value; | ||
const exclusiveMinimum = zodNumberChecks.min?.inclusive; | ||
const exclusiveMaximum = zodNumberChecks.max?.inclusive; | ||
return { | ||
...(minimum !== undefined && { minimum }), | ||
...(maximum !== undefined && { maximum }), | ||
...(exclusiveMinimum && { | ||
exclusiveMinimum: (0, openapi_1.satisfiesVersion)(openapi, '3.1.0') ? minimum : true, | ||
}), | ||
...(exclusiveMaximum && { | ||
exclusiveMaximum: (0, openapi_1.satisfiesVersion)(openapi, '3.1.0') ? maximum : true, | ||
}), | ||
}; | ||
const mapNumberChecks = () => { }; | ||
exports.mapNumberChecks = mapNumberChecks; | ||
const mapMaximum = (zodNumberCheck, openapi) => { | ||
if (!zodNumberCheck.max) { | ||
return undefined; | ||
} | ||
const maximum = zodNumberCheck.max.value; | ||
if (zodNumberCheck.max.inclusive) { | ||
return { ...(maximum !== undefined && { maximum }) }; | ||
} | ||
if ((0, openapi_1.satisfiesVersion)(openapi, '3.1.0')) { | ||
return { exclusiveMaximum: maximum }; | ||
} | ||
return { maximum, exclusiveMaximum: true }; | ||
}; | ||
exports.mapMaximum = mapMaximum; | ||
const mapMinimum = (zodNumberCheck, openapi) => { | ||
if (!zodNumberCheck.min) { | ||
return undefined; | ||
} | ||
const minimum = zodNumberCheck.min.value; | ||
if (zodNumberCheck.min.inclusive) { | ||
return { ...(minimum !== undefined && { minimum }) }; | ||
} | ||
if ((0, openapi_1.satisfiesVersion)(openapi, '3.1.0')) { | ||
return { exclusiveMinimum: minimum }; | ||
} | ||
return { minimum, exclusiveMinimum: true }; | ||
}; | ||
exports.mapMinimum = mapMinimum; | ||
const getZodNumberChecks = (zodNumber) => zodNumber._def.checks.reduce((acc, check) => { | ||
@@ -30,0 +47,0 @@ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
import { satisfiesVersion } from '../../openapi'; | ||
export const createNumberSchema = (zodNumber, components) => { | ||
const zodNumberChecks = getZodNumberChecks(zodNumber); | ||
const minimum = mapMinimum(zodNumberChecks, components.openapi); | ||
const maximum = mapMaximum(zodNumberChecks, components.openapi); | ||
return { | ||
type: mapNumberType(zodNumberChecks), | ||
...mapRanges(zodNumberChecks, components.openapi), // union types are too pesky to drill through | ||
...(minimum && minimum), | ||
...(maximum && maximum), | ||
}; | ||
}; | ||
const mapRanges = (zodNumberChecks, openapi) => { | ||
const minimum = zodNumberChecks.min?.value; | ||
const maximum = zodNumberChecks.max?.value; | ||
const exclusiveMinimum = zodNumberChecks.min?.inclusive; | ||
const exclusiveMaximum = zodNumberChecks.max?.inclusive; | ||
return { | ||
...(minimum !== undefined && { minimum }), | ||
...(maximum !== undefined && { maximum }), | ||
...(exclusiveMinimum && { | ||
exclusiveMinimum: satisfiesVersion(openapi, '3.1.0') ? minimum : true, | ||
}), | ||
...(exclusiveMaximum && { | ||
exclusiveMaximum: satisfiesVersion(openapi, '3.1.0') ? maximum : true, | ||
}), | ||
}; | ||
export const mapNumberChecks = () => { }; | ||
export const mapMaximum = (zodNumberCheck, openapi) => { | ||
if (!zodNumberCheck.max) { | ||
return undefined; | ||
} | ||
const maximum = zodNumberCheck.max.value; | ||
if (zodNumberCheck.max.inclusive) { | ||
return { ...(maximum !== undefined && { maximum }) }; | ||
} | ||
if (satisfiesVersion(openapi, '3.1.0')) { | ||
return { exclusiveMaximum: maximum }; | ||
} | ||
return { maximum, exclusiveMaximum: true }; | ||
}; | ||
export const mapMinimum = (zodNumberCheck, openapi) => { | ||
if (!zodNumberCheck.min) { | ||
return undefined; | ||
} | ||
const minimum = zodNumberCheck.min.value; | ||
if (zodNumberCheck.min.inclusive) { | ||
return { ...(minimum !== undefined && { minimum }) }; | ||
} | ||
if (satisfiesVersion(openapi, '3.1.0')) { | ||
return { exclusiveMinimum: minimum }; | ||
} | ||
return { minimum, exclusiveMinimum: true }; | ||
}; | ||
const getZodNumberChecks = (zodNumber) => zodNumber._def.checks.reduce((acc, check) => { | ||
@@ -26,0 +40,0 @@ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment |
@@ -1,4 +0,14 @@ | ||
import { oas31 } from 'openapi3-ts'; | ||
import { ZodNumber } from 'zod'; | ||
import { oas30, oas31 } from 'openapi3-ts'; | ||
import { ZodNumber, ZodNumberCheck } from 'zod'; | ||
import { ComponentsObject } from '../components'; | ||
import { ZodOpenApiVersion } from '../document'; | ||
export declare const createNumberSchema: (zodNumber: ZodNumber, components: ComponentsObject) => oas31.SchemaObject; | ||
export declare const mapNumberChecks: () => void; | ||
export declare const mapMaximum: (zodNumberCheck: ZodNumberCheckMap, openapi: ZodOpenApiVersion) => Pick<oas31.SchemaObject | oas30.SchemaObject, 'maximum' | 'exclusiveMaximum'> | undefined; | ||
export declare const mapMinimum: (zodNumberCheck: ZodNumberCheckMap, openapi: ZodOpenApiVersion) => Pick<oas31.SchemaObject | oas30.SchemaObject, 'minimum' | 'exclusiveMinimum'> | undefined; | ||
type ZodNumberCheckMap = { | ||
[kind in ZodNumberCheck['kind']]?: Extract<ZodNumberCheck, { | ||
kind: kind; | ||
}>; | ||
}; | ||
export {}; |
{ | ||
"name": "zod-openapi", | ||
"version": "0.4.2", | ||
"version": "0.4.3", | ||
"description": "A library to create full OpenAPI documents from your Zod types", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/samchungy/zod-openapi#readme", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
187192
2325