defined() allows null to validate
See original GitHub issueDescribe the bug
Calling defined
on any schema will allow null
to pass validation, unless required
is also included or nullable(false)
is called after calling defined
.
Discovered while discussing in https://github.com/DefinitelyTyped/DefinitelyTyped/pull/45244.
number().isValidSync(null); // false
number().defined().isValidSync(null); // true (!!)
number().nullable(false).defined().isValidSync(null); // true (!!)
number().required().defined().isValidSync(null); // false
To Reproduce
https://codesandbox.io/s/hopeful-diffie-tp9l7
Expected behavior
defined
should only disallow undefined
– it should never affect the allowance or disallowance of null
.
Platform (please complete the following information):
- Browser: Edge
- Version: 83
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:13 (9 by maintainers)
Top Results From Across the Web
class validator - How to allow null, but forbid undefined?
It turns out that this is possible by using conditional validation ValidateIf : class DbRow { @IsNumber() id!: number; @IsNumber() ...
Read more >What does it mean to do a "null check" in C or C++?
That means returning a (pointer - or even better, reference to an) empty array or list instead of null, or returning an empty...
Read more >Null safety | Kotlin Documentation
To allow nulls, you can declare a variable as a nullable string ... For example, the toString() function is defined on a nullable...
Read more >Validations & Constraints - Sequelize
The allowNull check is the only check in Sequelize that is a mix of a validation and a constraint in the senses described...
Read more >21.2 Validating Null and Empty Strings
Suppose, on the other hand, that you have a @NotNull constraint on an element, meaning that input is required. In this case, an...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I also just stumbled over this. In TypeScript, the types don’t match the actual allowed values after validation.
Correct:
Also correct:
This is the problem case:
To make sure the inferred types are correct, you have to call
nullable
anyway:The documentation for this is correct, but the types are incorrect and led me to a lot of head scratching.
.defined().nullable(false)
and.nullable().present()
(or notNullOrUndefined) wouldn’t be equivalent and solve slightly different use cases. The former strictly bansnull
, whereas the latter allows it as a value but fails validation.e.g.
but
The reason you might want the second but not the first is when parsing server responses, e.g. you fetch from an API, then cast the JSON, manipulate it and then validate after, at which point you want required values to be filled and throw if they aren’t. Mentioning this mostly to clarify my own thinking for when invariably read this months from now contemplating changing something again 😛