zod icon indicating copy to clipboard operation
zod copied to clipboard

Feature request. Enable or disable .strict() by argument

Open max-mykhailenko opened this issue 3 years ago • 2 comments

For the staging environment, I want to provide additional protocol validation with strict parsing, but it can break something on production, so I see the next solution .strict(process.env.NODE_ENV === 'development') and it gives

  • .strict() — strict mode enabled
  • .strict(true) — strict mode enabled
  • .strict(false) — strict mode disabled

Is it hard to implement?

max-mykhailenko avatar Sep 11 '22 09:09 max-mykhailenko

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Nov 10 '22 09:11 stale[bot]

Please do not stale this issue :) I hope that PR will be merged soon. @brunocrosier thank you for participating.

max-mykhailenko avatar Nov 10 '22 09:11 max-mykhailenko

Is this what you are looking for? https://github.com/colinhacks/zod/pull/1484#issuecomment-1284640538

JacobWeisenburger avatar Jan 05 '23 03:01 JacobWeisenburger

@JacobWeisenburger hi

const rawSchema = z.object({ ... });
const schema = process.env.NODE_ENV === 'development' ? rawSchema : rawSchema.strict()

I have a lot of different schemas and don't want to create add condition before each.

max-mykhailenko avatar Jan 05 '23 10:01 max-mykhailenko

Does this work for you?

function makeSchemaStrict<ObjSchema extends z.ZodObject<z.ZodRawShape>> ( objSchema: ObjSchema ) {
    return process.env.NODE_ENV === 'production' ? objSchema : objSchema.strict()
}

JacobWeisenburger avatar Jan 05 '23 15:01 JacobWeisenburger

@JacobWeisenburger thank you for participation, I understand how to improve it, but it's look less natural and also in this case I need to recheck generation of TS types based on schema

max-mykhailenko avatar Jan 05 '23 16:01 max-mykhailenko