colinhacks / zod

TypeScript-first schema validation with static type inference
https://zod.dev
MIT License
33.09k stars 1.15k forks source link

Feature Proposal: Allow Support for alternative field names #3732

Open mduesterhoeft opened 3 weeks ago

mduesterhoeft commented 3 weeks ago

What

Extend zod by a possibility to provide alternative or secondary field names. Those secondary field names are considered when parsing an object and the primary field name is not found in an object. This would be a great addition to ease renaming fields in a schema without backward incompatible changes

This could look like this

const schema = z.object({
  myField: z.string().secondaryName('myOldField'),
});

Parsing the following objects would result in the same output

schema.parse({myField:'some'}) // result is {myField:'some'}

schema.parse({myOldField:'some'}) // result is {myField:'some'}