IsOptional support for null
See original GitHub issueI’m not sure whether this is part of as conditional decorator limitations in the readme. In any case…
The @IsOptional
decorator should be adding anyOf: [{type: someType}, {type: 'null'}]
as well as removing the property from the required
array. It doesn’t seem to be doing the former.
I note that internally, class validator uses conditionalValidation
for the IsOptional
decorator. Am I correct that the limitation is that this doesn’t work when there are multiple decorators for one field? For example:
@IsOptional()
@IsNumber()
thing: number
@IsOptional()
@IsSting()
otherThing: string
The functionality of IsOptional
depends on the other decorators.
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (3 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; ...
Read more >Optional (Java Platform SE 8 ) - Oracle Help Center
A container object which may or may not contain a non-null value. If a value is present, isPresent() will return true and get()...
Read more >Null in Parse JSON when Upload in Forms is Optional
Solved: I have a flow that is triggered when a user submits a response to a Microsoft Form. A user has the option...
Read more >Documentation - TypeScript 3.7
Note that if bar is null or undefined , our code will still hit an error accessing ... libraries in JSDoc annotated JavaScript...
Read more >MySQL NULL Values - IS NULL and IS NOT NULL - W3Schools
A field with a NULL value is a field with no value. If a field in a table is optional, it is possible...
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
Personally, I don’t think
@IsOptional
should automatically allownull
on a property that perhaps has other validations. Null is a very specific value that can have meaning. I always understood@IsOptional
to mean if the property exists, then ensure it follows the other validations, and if it doesn’t exist, that’s ok.This is especially relevant when I’m implementing a
PATCH
API call where all properties in the body are optional, but each must pass some validation if included, andnull
is a valid value for a property.Hello,
Any news on this? How can i validate or null or enum?
This doesn’t work.