In my Yup schema I was my String field name
to allow you to pass in any string, an empty string, or nothing at all. If you pass in a string, it passes. If you pass in an empty string or nothing, I want to convert to a default value.
This is the schema that I thought would cover it:
const mySchema = yup.object().shape({
name: yup.string('Name must be a string').max(100, 'Name has a max of 100 characters').default('John Doe')
});
However, if I pass in an empty string ''
, it does not trigger the default conversion and it just passed through as an empty string. I've tried adding required()
but that just makes the line fail if I pass an empty string. I've tried nullable()
and trim()
but nothing seems to work.
How can I make the default value replace empty strings?
''
) is a string and would not trigger the default. – Gerber