diff --git a/src/myzod/index.ts b/src/myzod/index.ts index ca44c10f..c42694b3 100644 --- a/src/myzod/index.ts +++ b/src/myzod/index.ts @@ -275,7 +275,8 @@ function generateFieldTypeMyZodSchema(config: ValidationSchemaPluginConfig, visi let appliedDirectivesGen = applyDirectives(config, field, gen); - if (field.kind === Kind.INPUT_VALUE_DEFINITION) { + const hasDefaultValue = field.kind === Kind.INPUT_VALUE_DEFINITION && field.defaultValue + if (hasDefaultValue) { const { defaultValue } = field; if (defaultValue?.kind === Kind.INT || defaultValue?.kind === Kind.FLOAT || defaultValue?.kind === Kind.BOOLEAN) @@ -285,7 +286,7 @@ function generateFieldTypeMyZodSchema(config: ValidationSchemaPluginConfig, visi appliedDirectivesGen = `${appliedDirectivesGen}.default("${defaultValue.value}")`; } - if (isNonNullType(parentType)) { + if (isNonNullType(parentType) || hasDefaultValue) { if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value)) return `${gen}.min(1)`; diff --git a/src/yup/index.ts b/src/yup/index.ts index 5998af5d..4bd51303 100644 --- a/src/yup/index.ts +++ b/src/yup/index.ts @@ -272,7 +272,8 @@ function shapeFields(fields: readonly (FieldDefinitionNode | InputValueDefinitio ?.map((field) => { let fieldSchema = generateFieldYupSchema(config, visitor, field, 2); - if (field.kind === Kind.INPUT_VALUE_DEFINITION) { + const hasDefaultValue = field.kind === Kind.INPUT_VALUE_DEFINITION && field.defaultValue + if (hasDefaultValue) { const { defaultValue } = field; if ( @@ -284,9 +285,11 @@ function shapeFields(fields: readonly (FieldDefinitionNode | InputValueDefinitio if (defaultValue?.kind === Kind.STRING || defaultValue?.kind === Kind.ENUM) fieldSchema = `${fieldSchema}.default("${defaultValue.value}")`; + + fieldSchema = fieldSchema.replace('.nullable()', '') } - if (isNonNullType(field.type)) + if (isNonNullType(field.type) || hasDefaultValue) return fieldSchema; return `${fieldSchema}.optional()`; diff --git a/src/zod/index.ts b/src/zod/index.ts index e5bd802f..2fbb02d5 100644 --- a/src/zod/index.ts +++ b/src/zod/index.ts @@ -288,7 +288,8 @@ function generateFieldTypeZodSchema(config: ValidationSchemaPluginConfig, visito let appliedDirectivesGen = applyDirectives(config, field, gen); - if (field.kind === Kind.INPUT_VALUE_DEFINITION) { + const hasDefaultValue = field.kind === Kind.INPUT_VALUE_DEFINITION && field.defaultValue + if (hasDefaultValue) { const { defaultValue } = field; if (defaultValue?.kind === Kind.INT || defaultValue?.kind === Kind.FLOAT || defaultValue?.kind === Kind.BOOLEAN) @@ -298,7 +299,7 @@ function generateFieldTypeZodSchema(config: ValidationSchemaPluginConfig, visito appliedDirectivesGen = `${appliedDirectivesGen}.default("${defaultValue.value}")`; } - if (isNonNullType(parentType)) { + if (isNonNullType(parentType) || hasDefaultValue) { if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value)) return `${appliedDirectivesGen}.min(1)`; diff --git a/tests/myzod.spec.ts b/tests/myzod.spec.ts index 1a53f782..96bd13be 100644 --- a/tests/myzod.spec.ts +++ b/tests/myzod.spec.ts @@ -1163,9 +1163,9 @@ describe('myzod', () => { expect(result.content).toContain('export function PageInputSchema(): myzod.Type'); expect(result.content).toContain('pageType: PageTypeSchema.default("PUBLIC")'); - expect(result.content).toContain('greeting: myzod.string().default("Hello").optional().nullable()'); - expect(result.content).toContain('score: myzod.number().default(100).optional().nullable()'); - expect(result.content).toContain('ratio: myzod.number().default(0.5).optional().nullable()'); - expect(result.content).toContain('isMember: myzod.boolean().default(true).optional().nullable()'); + expect(result.content).toContain('greeting: myzod.string().default("Hello")'); + expect(result.content).toContain('score: myzod.number().default(100)'); + expect(result.content).toContain('ratio: myzod.number().default(0.5)'); + expect(result.content).toContain('isMember: myzod.boolean().default(true)'); }); }); diff --git a/tests/yup.spec.ts b/tests/yup.spec.ts index 2593fc03..7fd441c1 100644 --- a/tests/yup.spec.ts +++ b/tests/yup.spec.ts @@ -1097,9 +1097,9 @@ describe('yup', () => { expect(result.content).toContain('export function PageInputSchema(): yup.ObjectSchema'); expect(result.content).toContain('pageType: PageTypeSchema.nonNullable().default("PUBLIC")'); - expect(result.content).toContain('greeting: yup.string().defined().nullable().default("Hello").optional()'); - expect(result.content).toContain('score: yup.number().defined().nullable().default(100).optional()'); - expect(result.content).toContain('ratio: yup.number().defined().nullable().default(0.5).optional()'); - expect(result.content).toContain('isMember: yup.boolean().defined().nullable().default(true).optional()'); + expect(result.content).toContain('greeting: yup.string().defined().default("Hello")'); + expect(result.content).toContain('score: yup.number().defined().default(100)'); + expect(result.content).toContain('ratio: yup.number().defined().default(0.5)'); + expect(result.content).toContain('isMember: yup.boolean().defined().default(true)'); }); }); diff --git a/tests/zod.spec.ts b/tests/zod.spec.ts index e297351a..f419d4db 100644 --- a/tests/zod.spec.ts +++ b/tests/zod.spec.ts @@ -427,10 +427,10 @@ describe('zod', () => { expect(result.content).toContain('export function PageInputSchema(): z.ZodObject>'); expect(result.content).toContain('pageType: PageTypeSchema.default("PUBLIC")'); - expect(result.content).toContain('greeting: z.string().default("Hello").nullish()'); - expect(result.content).toContain('score: z.number().default(100).nullish()'); - expect(result.content).toContain('ratio: z.number().default(0.5).nullish()'); - expect(result.content).toContain('isMember: z.boolean().default(true).nullish()'); + expect(result.content).toContain('greeting: z.string().default("Hello")'); + expect(result.content).toContain('score: z.number().default(100)'); + expect(result.content).toContain('ratio: z.number().default(0.5)'); + expect(result.content).toContain('isMember: z.boolean().default(true)'); }); describe('issues #19', () => {