jquense / yup

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

What is the value in `getDefault({ value: ?? })` means? #2158

Closed Ridermansb closed 10 months ago

Ridermansb commented 10 months ago

I was trying to get the default value from schema and if the value is provided in the data get the data value instead...

Something like this..

const initialValue = schema.getDefault({ value: data });

But I couldn't find any documentation about this.

jquense commented 10 months ago

You can check the TS type for more details, it's a set of values that is used to resolve conditional schema. If you don't have issues using getDefault() without the options then you probably don't need to worry about it

Ridermansb commented 10 months ago

@jquense the reason for I'm asking is because I was wondering if there is any way to get the values from object and for the values that was not defined, pick the default one.

Something like this...

const mySchema = yup.object({ firstName: yup.string().default(''), lastName: yup.string().default('') });

const  me = {
  firstName: "Riderman"
}

const initialData =  mySchema.getDefault({ value: me }) // BUT NOT WORKING

Since there is no documentation about value and the Type definition is any I still don't know what is this for.

I endup doing something this ...

const defaultData = mySchema.getDefault(); 
const initialData =  {
   ...defaultData,
  ...me
}