Skip to content

Feature request: @IsNullable #637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
andybarron opened this issue Jun 14, 2020 · 4 comments
Open

Feature request: @IsNullable #637

andybarron opened this issue Jun 14, 2020 · 4 comments
Labels
type: feature Issues related to new features.

Comments

@andybarron
Copy link

A version of @IsOptional that would allow null but not undefined would be a great add for a lot of use cases. Additionally, a version that allowed undefined but not null would come in handy for certain operations as well - I would think this is what @IsOptional should do, but the docs state that it allows null AND undefined, which I don't think is particularly intuitive.

@Kiliandeca
Copy link
Contributor

This seems to be related to #491

@rubiin
Copy link
Contributor

rubiin commented Jun 27, 2020

why not use a custom validator?

@NickKelly1
Copy link
Contributor

Right now you can accomplish this using a custom decorator.

Reposting my solution from here:

You can create "optionally null" and "optionally undefined" decorators by decorating the @ValidateIf decorator

/**
 * Skips validation if the target is null
 */
function IsNullable(options?: ValidationOptions): PropertyDecorator {
  return function IsNullableDecorator(prototype: Object, propertyKey: string | symbol) {
    ValidateIf((obj) => (obj)[propertyKey] !== null, options)(prototype, propertyKey);
  };
}

/**
 * Skips validation if the target is undefined
 */
function IsUndefinable(options?: ValidationOptions): PropertyDecorator {
  return function IsUndefinedDecorator(prototype: Object, propertyKey: string | symbol) {
    ValidateIf((obj) => (obj)[propertyKey] !== undefined, options)(prototype, propertyKey);
  };
}

@Kiliandeca Kiliandeca added the type: feature Issues related to new features. label Aug 27, 2020
@ynishimura
Copy link

I'm happy that @IsNullable is provided as a feature rather than using a custom validator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature Issues related to new features.
Development

No branches or pull requests

5 participants