jquense / yup

Dead simple Object schema validation
MIT License
22.95k stars 935 forks source link

How to set all fields as required? #2064

Closed beratbayram closed 1 year ago

beratbayram commented 1 year ago

Hi! I am new to yup and I feel like I might be missing something obvious so sorry about that in advance.

How do I set every field as required?

For example:

I have this schema but it look cluttered with all those ".required()" calls.

const schema = object({
  field1: number().required(),
  field2: string().required(),
  field3: string().required(),
}).required();

Is there someting like this to eliminate those calls?, for example:

const schema = object({
  field1: number(),
  field2: string(),
  field3: string(),
}).requiredAll();
jquense commented 1 year ago

You aren't missing anything, you need to mark them as required. You can wrap them and report if you want to change the defaults.

export const string = () => yup.string().required
beratbayram commented 1 year ago

@jquense thank you for your answer but wouldnt that make the object itself required rather than its fields?

jquense commented 1 year ago

You'd need to re-export every type you want to change the defaults for, yeah