Yup: Run custom validation for an optional field only if it's set

Created on 19 Apr 2018  路  1Comment  路  Source: jquense/yup

Given I have a not required field, which I wanna validate only if it's set, how do I do that?

field: {
  yup.string().notRequired().test('key', 'message', fn)
}

fails, nevertheless it has notRequired() - when field is not set if my tester fn doesn't return true for undefined. Can I omit tuning the function to handle that case and just make it not called for not required fields if they're not set?

>All comments

test functions are all independent from each other, if you want to ignore unset values in it you should return true for undefined values

Was this page helpful?
0 / 5 - 0 ratings