jquense / yup

Dead simple Object schema validation
MIT License
22.93k stars 934 forks source link

Make all fields required by default #2134

Closed Fedinjo closed 12 months ago

Fedinjo commented 1 year ago

I just had to migrate from Zod to Yup and I've noticed that Yup doesn't set all the fields as required by default:

With this schema the validation passes even though all the fields are missing:

export const ContattaciBffPayloadSchema = yup.object().shape({
  datiCliente: yup.object({
    nomeCognome: yup.string(),
    codiceFiscale: yup.string().matches(Regexes.CodiceFiscale),
    testo: yup.string(),
  }),
  recapitoAgenzia: yup.string().email(),
});

Adding .required() everywhere fixes it:

export const ContattaciBffPayloadSchema = yup.object().shape({
  datiCliente: yup.object({
    nomeCognome: yup.string().required(),
    codiceFiscale: yup.string().matches(Regexes.CodiceFiscale).required(),
    testo: yup.string().required(),
  }),
  recapitoAgenzia: yup.string().email().required(),
}).required();

My question is: is there a way to make all the fields required by default so that you don't need to add .required() in every place?

jquense commented 12 months ago

There is not a way to change the default. you can wrap the schema factories and set things to required and reexport them if you want.