Check for string to not be empty?
Not sure why I am struggling with this right now:
Is there a better way of checking if the data is not set, unfortunatly I get the ResponseError if I pass empty strings.
5 Replies
Hello, one way to improve your code is check for falsy values using the
!
operator
For example, instead of checking if theses values are null
or undefined
like this:
You can check if the value is falsy with the !
operator:
Have a quick look about checking for falsy values in Javascript on this article: https://www.freecodecamp.org/news/falsy-values-in-javascript/freeCodeCamp.org
Falsy Values in JavaScript
Description A falsy value is something which evaluates to FALSE, for instance when checking a variable. There are only six falsey values in JavaScript: undefined, null, NaN , 0, "" (empty string), and false of course. Checking for falsy values on variables It is possible to check for a falsy
If you want to allow empty strings to pass on this verification you can use a logic like that:
use a zod validator
GitHub
TypeScript-first schema validation with static type inference
TypeScript-first schema validation with static type inference
big +1 for zod. colocating your types and validation feels like magic