Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

yup.string().required().optional() thinks empty string is invalid. #2188

Open
houkanshan opened this issue Mar 22, 2024 · 0 comments
Open

yup.string().required().optional() thinks empty string is invalid. #2188

houkanshan opened this issue Mar 22, 2024 · 0 comments

Comments

@houkanshan
Copy link

Describe the bug

Depends on the definition, it may not be a Bug.

string().required().optional() thinks empty string is invalid. Because undefined is valid in this case, treating empty string as invalid is strange.

I understand that string().defined().optional() and string().required().notRequired() are the correct usages. But I think it is still nice to make string().required().optional() to work properly.

Also, .partial() and .deepPartial() always call .optional() so object({ str: yup.string().required() }).partial() could also be an issue.

To Reproduce

https://stackblitz.com/edit/vitejs-vite-pwmcyx?file=main.js

import * as yup from 'yup';

const schema = yup.string().required().optional();
const result = {
  undefined: schema.isValidSync(), // valid
  '': schema.isValidSync(''), // invalid
  a: schema.isValidSync('a'), // valid
};
console.log(result);

Expected behavior

yup.string().required().optional().validate('') should be valid.

Platform (please complete the following information):

  • Browser: any
  • Version: 1.4.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant