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() + @NotEquals(null) not working

See original GitHub issue

I want to validate displayName with optional and not equal null but it not working. it does not validate @NotEquals(null).

export class UpdateDto implements Partial<UserEntity> {
  @ApiProperty()
  @IsOptional()
  @NotEquals(null)
  displayName?: string;
}

this case is pass input:

{
 displayName: null
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
tmtroncommented, Apr 18, 2020

Recommended I suggest, that you remove @NotEquals(null) and set skipMissingProperties to true in the validation options
see this Stackblitz example

Alternative

When you want to use the default validation options, you can use ValidateIf to make it work:

export class UpdateDto {
  @IsString()
  @NotEquals(null)
  @ValidateIf((object, value) => value !== undefined)
  displayName?: string;
}

This will pass the validation when displayName is undefined or a string, but fail for null.

see this Stackblitz example

0reactions
lock[bot]commented, May 5, 2020

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Class Validate Not Null - StackBlitz
IsOptional,. NotEquals. } from "class-validator";. import "reflect-metadata";. /**. * test for https://github.com/typestack/. class-validator/issues/579.
Read more >
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 >
class-validator - npm
Decorator-based property validation for classes.. Latest version: 0.14.0, last published: 16 days ago. Start using class-validator in your ...
Read more >
Validation Chain API - express-validator
.bail(). Returns: the current validation chain instance. Stops running validations if any of the previous ones have failed.
Read more >
How to Check If Two Variant Values Are Equal (or Not) in VBA
When are two identical values not equal? ... And that means we spend a lot of time working with Nulls. ... The Notes...
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