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.

Allow empty string but fail on undefined

See original GitHub issue

According to README, it should be possible to require empty string:

To allow empty strings but fail on undefined values use: string().required().min(0)

However, this does not seem to work:

const yup = require("yup")

const okString = 'foo';
const shouldBeOkString = '';
let nokString;

const schema = yup.string().required().min(0);

console.log(await schema.isValid(okString)); // true
console.log(await schema.isValid(shouldBeOkString)); // false <--
console.log(await schema.isValid(nokString)); // false

Same with yup.array().required().min(0) won’t allow [].

Am I missing something?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
tnrichcommented, Sep 17, 2020

I don’t see how to do the old

string().required().min(0) 

case anymore. Do I have to now provide a custom yup method? I made a SO question asking for help https://stackoverflow.com/questions/63943689/how-to-get-yup-string-to-require-string-of-any-length-including-0

Thanks!

4reactions
blasterbugcommented, Mar 26, 2021

In deed, string().required().min(0) fail miserably for empty strings.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I check for an empty/undefined/null string in JavaScript?
It will check the length, returning undefined in case of a nullish value, without throwing an error. In the case of an empty...
Read more >
Null and undefined (Reference) - Prisma
How Prisma Client handles null and undefined, including a GraphQL use case. ... authorEmail is null , the query will fail - email...
Read more >
Documentation - TypeScript 2.0
Null - and undefined-aware types. TypeScript has two special types, Null and Undefined, that have the values null and undefined respectively.
Read more >
Checking for an Empty, Null, or Undefined String - YouTube
In this quick take, learn all about how you can to see whether your strings contain a valid value or not.
Read more >
JavaScript Check Empty String – Checking Null or Empty in JS
How to Check for Null in JavaScript. So far, we've seen how to check if a string is empty using the length and...
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