Skip to content

Commit

Permalink
fix(zod): handle schema type array correctly (#1311)
Browse files Browse the repository at this point in the history
* fix(zod): handle schema type array correctly

* fix(format): check
  • Loading branch information
anymaniax authored Apr 16, 2024
1 parent 25267ae commit b1d66dc
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions packages/zod/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,26 @@ const ZOD_DEPENDENCIES: GeneratorDependency[] = [

export const getZodDependencies = () => ZOD_DEPENDENCIES;

const possibleSchemaTypes = [
'integer',
'number',
'string',
'boolean',
'object',
'null',
'array',
];

const resolveZodType = (schemaTypeValue: SchemaObject['type']) => {
switch (schemaTypeValue) {
const type = Array.isArray(schemaTypeValue)
? schemaTypeValue.find((t) => possibleSchemaTypes.includes(t))
: schemaTypeValue;

switch (type) {
case 'integer':
return 'number';
default:
return schemaTypeValue ?? 'any';
return type ?? 'any';
}
};

Expand All @@ -65,7 +79,9 @@ const generateZodValidationSchemaDefinition = (
const functions: [string, any][] = [];
const type = resolveZodType(schema.type);
const required = schema.default !== undefined ? false : _required ?? false;
const nullable = schema.nullable ?? false;
const nullable =
schema.nullable ??
(Array.isArray(schema.type) && schema.type.includes('null'));
const min =
schema.minimum ??
schema.exclusiveMinimum ??
Expand Down

0 comments on commit b1d66dc

Please sign in to comment.