next-rest-framework
Advanced tools
Comparing version
@@ -5,3 +5,2 @@ import { type OpenAPIV3_1 } from 'openapi-types'; | ||
export declare const isZodSchema: (schema: unknown) => schema is ZodSchema<any, import("zod").ZodTypeDef, any>; | ||
export declare const convertZodSchema: (schema: ZodSchema) => Record<string, unknown>; | ||
export declare const validateSchema: ({ schema, obj }: { | ||
@@ -8,0 +7,0 @@ schema: BaseSchemaType; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getSchemaKeys = exports.getJsonSchema = exports.validateSchema = exports.convertZodSchema = exports.isZodSchema = void 0; | ||
const chalk_1 = __importDefault(require("chalk")); | ||
exports.getSchemaKeys = exports.getJsonSchema = exports.validateSchema = exports.isZodSchema = void 0; | ||
const zod_to_json_schema_1 = require("zod-to-json-schema"); | ||
const isZodSchema = (schema) => !!schema && typeof schema === 'object' && '_def' in schema; | ||
@@ -18,151 +15,2 @@ exports.isZodSchema = isZodSchema; | ||
}; | ||
const zodTypeGuards = { | ||
string: (schema) => schema._def.typeName === 'ZodString', | ||
symbol: (schema) => schema._def.typeName === 'ZodSymbol', | ||
date: (schema) => schema._def.typeName === 'ZodDate', | ||
number: (schema) => schema._def.typeName === 'ZodNumber', | ||
bigint: (schema) => schema._def.typeName === 'ZodBigInt', | ||
boolean: (schema) => schema._def.typeName === 'ZodBoolean', | ||
undefined: (schema) => schema._def.typeName === 'ZodUndefined', | ||
null: (schema) => schema._def.typeName === 'ZodNull', | ||
void: (schema) => schema._def.typeName === 'ZodVoid', | ||
nan: (schema) => schema._def.typeName === 'ZodNaN', | ||
never: (schema) => schema._def.typeName === 'ZodNever', | ||
any: (schema) => schema._def.typeName === 'ZodAny', | ||
unknown: (schema) => schema._def.typeName === 'ZodUnknown', | ||
literal: (schema) => schema._def.typeName === 'ZodLiteral', | ||
enum: (schema) => schema._def.typeName === 'ZodEnum', | ||
nativeEnum: (schema) => schema._def.typeName === 'ZodNativeEnum', | ||
optional: (schema) => schema._def.typeName === 'ZodOptional', | ||
nullable: (schema) => schema._def.typeName === 'ZodNullable', | ||
array: (schema) => schema._def.typeName === 'ZodArray', | ||
object: (schema) => schema._def.typeName === 'ZodObject', | ||
union: (schema) => schema._def.typeName === 'ZodUnion', | ||
discriminatedUnion: (schema) => schema._def.typeName === 'ZodDiscriminatedUnion', | ||
intersection: (schema) => schema._def.typeName === 'ZodIntersection', | ||
tuple: (schema) => schema._def.typeName === 'ZodTuple', | ||
record: (schema) => schema._def.typeName === 'ZodRecord', | ||
map: (schema) => schema._def.typeName === 'ZodMap', | ||
set: (schema) => schema._def.typeName === 'ZodSet' | ||
}; | ||
const convertZodSchema = (schema) => { | ||
if (zodTypeGuards.string(schema) || zodTypeGuards.symbol(schema)) { | ||
return { | ||
type: 'string' | ||
}; | ||
} | ||
if (zodTypeGuards.date(schema)) { | ||
return { | ||
type: 'string', | ||
format: 'date-time' | ||
}; | ||
} | ||
if (zodTypeGuards.number(schema) || zodTypeGuards.bigint(schema)) { | ||
return { | ||
type: 'number' | ||
}; | ||
} | ||
if (zodTypeGuards.boolean(schema)) { | ||
return { | ||
type: 'boolean' | ||
}; | ||
} | ||
if (zodTypeGuards.undefined(schema) || | ||
zodTypeGuards.null(schema) || | ||
zodTypeGuards.void(schema) || | ||
zodTypeGuards.nan(schema) || | ||
zodTypeGuards.never(schema)) { | ||
return { | ||
type: 'null' | ||
}; | ||
} | ||
if (zodTypeGuards.any(schema) || zodTypeGuards.unknown(schema)) { | ||
return {}; | ||
} | ||
if (zodTypeGuards.literal(schema)) { | ||
return { | ||
type: 'string', | ||
enum: [schema._def.value] | ||
}; | ||
} | ||
if (zodTypeGuards.enum(schema)) { | ||
return { | ||
enum: schema._def.values | ||
}; | ||
} | ||
if (zodTypeGuards.nativeEnum(schema)) { | ||
return { | ||
enum: Object.values(schema._def.values) | ||
}; | ||
} | ||
if (zodTypeGuards.optional(schema) || zodTypeGuards.nullable(schema)) { | ||
return { | ||
type: [(0, exports.convertZodSchema)(schema._def.innerType).type, 'null'] | ||
}; | ||
} | ||
if (zodTypeGuards.object(schema)) { | ||
const jsonSchema = { | ||
type: 'object', | ||
properties: Object.entries(schema._def.shape()).reduce((properties, [key, val]) => ({ | ||
...properties, | ||
[key]: (0, exports.convertZodSchema)(val) | ||
}), {}) | ||
}; | ||
const hasAdditionalProperties = schema._def.catchall._def.typeName === 'ZodNever'; | ||
if (!hasAdditionalProperties) { | ||
jsonSchema.additionalProperties = (0, exports.convertZodSchema)(schema._def.catchall); | ||
} | ||
return jsonSchema; | ||
} | ||
if (zodTypeGuards.array(schema)) { | ||
return { | ||
type: 'array', | ||
items: (0, exports.convertZodSchema)(schema._def.type) | ||
}; | ||
} | ||
if (zodTypeGuards.tuple(schema)) { | ||
return { | ||
type: 'array', | ||
items: schema._def.items.map((val) => (0, exports.convertZodSchema)(val)) | ||
}; | ||
} | ||
if (zodTypeGuards.union(schema)) { | ||
return { | ||
anyOf: schema._def.options.map((option) => (0, exports.convertZodSchema)(option)) | ||
}; | ||
} | ||
if (zodTypeGuards.discriminatedUnion(schema)) { | ||
return { | ||
oneOf: schema._def.options.map((option) => (0, exports.convertZodSchema)(option)) | ||
}; | ||
} | ||
if (zodTypeGuards.record(schema) || zodTypeGuards.map(schema)) { | ||
return { | ||
type: 'object', | ||
additionalProperties: (0, exports.convertZodSchema)(schema._def.valueType) | ||
}; | ||
} | ||
if (zodTypeGuards.set(schema)) { | ||
return { | ||
type: 'array', | ||
items: (0, exports.convertZodSchema)(schema._def.valueType), | ||
uniqueItems: true | ||
}; | ||
} | ||
if (zodTypeGuards.intersection(schema)) { | ||
return { | ||
allOf: [ | ||
(0, exports.convertZodSchema)(schema._def.left), | ||
(0, exports.convertZodSchema)(schema._def.right) | ||
] | ||
}; | ||
} | ||
if (process.env.NODE_ENV !== 'production' && 'typeName' in schema._def) { | ||
console.warn(chalk_1.default.yellowBright(`Warning: Next REST Framework detected an unsupported schema type for schema: ${schema._def.typeName} | ||
If you think this schema type should be supported, please open an issue at: https://github.com/blomqma/next-rest-framework/issues`)); | ||
} | ||
return {}; | ||
}; | ||
exports.convertZodSchema = convertZodSchema; | ||
const validateSchema = async ({ schema, obj }) => { | ||
@@ -177,3 +25,5 @@ if ((0, exports.isZodSchema)(schema)) { | ||
if ((0, exports.isZodSchema)(schema)) { | ||
return (0, exports.convertZodSchema)(schema); | ||
return (0, zod_to_json_schema_1.zodToJsonSchema)(schema, { | ||
target: 'openApi3' | ||
}); | ||
} | ||
@@ -180,0 +30,0 @@ throw Error('Invalid schema.'); |
{ | ||
"name": "next-rest-framework", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "Next REST Framework - write type-safe, self-documenting REST APIs in Next.js", | ||
@@ -35,3 +35,4 @@ "keywords": [ | ||
"lodash": "4.17.21", | ||
"wait-on": "7.0.1" | ||
"wait-on": "7.0.1", | ||
"zod-to-json-schema": "3.21.4" | ||
}, | ||
@@ -38,0 +39,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
180830
0.29%4
-20%6
20%1452
-9.36%+ Added
+ Added
+ Added