Hey,
I have an object that can is by default undefined, and accept one of null or a stringvalue.
I achieve the desired behavior using
````js
Yup.string()
.nullable()
.oneOf([null, 'female', 'male'])
.notOneOf([undefined])
````
that should be the same than:
Yup.string()
.nullable()
.required()
.oneOf([null, 'female', 'male', 'other'])
But this is not...
if I read the manual required is checking for undefined and nullable for null, but they seems to conflicts each other...
Can you check that ?
best regards
required actually checks "presence" which is undefined or null since that generally what folks mean by required. We should probably add a defined() check as well tho for the case where you just don't want it to be undefined. in the meantime it's easy to add
yup.addMethod(yup.mixed, 'defined', function () {
return this.test('defined', "{path} must be defined", value => value !== undefined)
})
That's nice ! Thanks for the report.
mixed.required(message: ?string): Schema
Mark the schema as required. All field values apart from undefined meet this requirement.
Could be nice to add this notice in the documentation, do you want me to make a PR for both those features ?
Best regards
do you want me to make a PR for both those features ?
that would be wonderful :)
Anybody wishing to use the "defined" method along with TypeScript should also add this:
declare module 'yup' {
export interface Schema<T> {
defined<U>(): this;
}
}
Most helpful comment
required actually checks "presence" which is
undefinedornullsince that generally what folks mean by required. We should probably add adefined()check as well tho for the case where you just don't want it to beundefined. in the meantime it's easy to add