question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

An empty string is considered as a valid email

See original GitHub issue

Describe the bug

yup.string().email().validateSync('') throws no errors and returns an empty string.

And

var yup = require("yup")

const schema =  yup.object().shape({
    email: yup.string().email(),
});

schema.validateSync({email: ''})

Also works without errors.

To Reproduce

Just run the code

var yup = require("yup")
yup.string().email().validateSync('')

const schema =  yup.object().shape({
    email: yup.string().email(),
});

schema.validateSync({email: ''})

here https://npm.runkit.com/yup

Expected behavior

An error that the email is invalid must be thrown.

Platform (please complete the following information):

Node.js 10.15.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jquensecommented, Nov 6, 2019

an empty string is considered a missing value for strings, so most validations besides required ignore it. If you want to check empty strings as invalid, mark the field as required as noted above thanks!

1reaction
RussCodercommented, Oct 23, 2019

@akmjenkins You should read my previous message again and run the code:

const schema =  yup.object().shape({
    email: yup.string().required().email(),
    name: yup.string().ensure(),
    lastname: yup.string().ensure(),
});

const res = schema.validateSync({name: 'ads'}, {
    strict: true, 
    abortEarly: false, 
    stripUnknown: true
});

Email isn’t a required field for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript regex for email and empty string - Stack Overflow
I'm using this regex /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/ for validating email format but when I leave the the email field blank it shows an error ...
Read more >
Error while sending email through Webmail | Blue Prism Product
It means you have been trying to add an empty string as an email address, which is not valid. You may want to...
Read more >
How to verify that strings are in valid email format
The IsValidEmail method merely determines whether the email format is valid for an email address; it doesn't validate that the email exists.
Read more >
Validation of property with EmailAddressAttribute fails on empty
When a string property is decorated with an EmailAddressAttribute validation fails when removing the address. This occurs on a fresh Alloy ...
Read more >
<input type="email"> - HTML: HyperText Markup Language
Notice that it's considered valid when empty and when a single validly-formatted email address is entered, but is otherwise not considered valid ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found