Skip to content
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

feat: add IsNullable decorator #2443

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -795,10 +795,11 @@ isBoolean(value);
## Validation decorators

| Decorator | Description |
| ------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|--------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| **Common validation decorators** | |
| `@IsDefined(value: any)` | Checks if value is defined (!== undefined, !== null). This is the only decorator that ignores skipMissingProperties option. |
| `@IsOptional()` | Checks if given value is empty (=== null, === undefined) and if so, ignores all the validators on the property. |
| `@IsNullable()` | Checks if given value is null and if so, ignores all the validators on the property. |
| `@Equals(comparison: any)` | Checks if value equals ("===") comparison. |
| `@NotEquals(comparison: any)` | Checks if value not equal ("!==") comparison. |
| `@IsEmpty()` | Checks if given value is empty (=== '', === null, === undefined). |
Expand Down
28 changes: 28 additions & 0 deletions src/decorator/common/IsNullable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ValidationOptions } from '../ValidationOptions';
import { ValidationMetadataArgs } from '../../metadata/ValidationMetadataArgs';
import { ValidationTypes } from '../../validation/ValidationTypes';
import { ValidationMetadata } from '../../metadata/ValidationMetadata';
import { getMetadataStorage } from '../../metadata/MetadataStorage';

export const IS_NULLABLE = 'isNullable';

/**
* Checks if value is null and if so, ignores all validators.
*/
export function IsNullable(validationOptions?: ValidationOptions): PropertyDecorator {
return function (object: object, propertyName: string): void {
const args: ValidationMetadataArgs = {
type: ValidationTypes.CONDITIONAL_VALIDATION,
name: IS_NULLABLE,
target: object.constructor,
propertyName: propertyName,
constraints: [
(object: any, value: any): boolean => {
return object[propertyName] !== null;
},
],
validationOptions: validationOptions,
};
getMetadataStorage().addValidationMetadata(new ValidationMetadata(args));
};
}
48 changes: 48 additions & 0 deletions test/functional/conditional-validation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IsNotEmpty, ValidateIf, IsOptional, Equals } from '../../src/decorator/decorators';
import { Validator } from '../../src/validation/Validator';
import {IsNullable} from "../../src/decorator/common/IsNullable";

const validator = new Validator();

Expand Down Expand Up @@ -92,4 +93,51 @@ describe('conditional validation', () => {
expect(errors[0].value).toEqual('bad_value');
});
});

it('should validate a property when value is supplied', () => {
class MyClass {
@IsNullable()
@Equals('test')
title: string = 'bad_value';
}

const model = new MyClass();
return validator.validate(model).then(errors => {
expect(errors.length).toEqual(1);
expect(errors[0].target).toEqual(model);
expect(errors[0].property).toEqual('title');
expect(errors[0].constraints).toEqual({ equals: 'title must be equal to test' });
expect(errors[0].value).toEqual('bad_value');
});
});

it('should validate a property when value is undefined', () => {
class MyClass {
@IsNullable()
@Equals('test')
title: string = undefined;
}

const model = new MyClass();
return validator.validate(model).then(errors => {
expect(errors.length).toEqual(1);
expect(errors[0].target).toEqual(model);
expect(errors[0].property).toEqual('title');
expect(errors[0].constraints).toEqual({ equals: 'title must be equal to test' });
expect(errors[0].value).toEqual(undefined);
});
});

it("shouldn't validate a property when value is null", () => {
class MyClass {
@IsNullable()
@Equals('test')
title: string = null;
}

const model = new MyClass();
return validator.validate(model).then(errors => {
expect(errors.length).toEqual(0);
});
});
});