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.

IsOptional support for null

See original GitHub issue

I’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:open
  • Created 5 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

25reactions
lobancommented, Oct 9, 2019

Personally, I don’t think @IsOptional should automatically allow null 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, and null is a valid value for a property.

4reactions
incompletudecommented, Jun 2, 2020

Hello,

Any news on this? How can i validate or null or enum?

This doesn’t work.

  @IsOptional()
  @IsEnum(["c", "a", "r"], { each: true })
  @Transform((value: string) => value.split(","))
  status: string[]
Read more comments on GitHub >

github_iconTop 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 >

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