[V5] How to validate empty string but don't allow undefined and null? #4112
Replies: 1 comment 2 replies
-
Check this: create custom rule and register it. In the rule you want to check for nulls or undefined and throw a validation error, everything else pass. e.g. validator.rule('notNullOrUndefined', (value, [field], options) => {
if (value === null || value === undefined) {
options.errorReporter.report(
options.pointer,
'notNullOrUndefined',
'notNullOrUndefined validation failed',
options.arrayExpressionPointer,
{ field}
)
}
}) |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Validator schema:
Now I send { name: null } or { name: "" } or { name: undefined } , everything i requrest will pass, then
Now { name: null } or { name: "" } or { name: undefined } all throw Exception
I want name is required, but empty string is OK. like...
{name: ""} OK
{name: "hello"} OK
{name: null} whatever
{name: undefined} Exception
{} Exception
Beta Was this translation helpful? Give feedback.
All reactions