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

Add decorator to check if two properties match or property follows a custom condition. #2462

Open
wants to merge 2 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ isBoolean(value);
| `@IsNotEmpty()` | Checks if given value is not empty (!== '', !== null, !== undefined). |
| `@IsIn(values: any[])` | Checks if value is in an array of allowed values. |
| `@IsNotIn(values: any[])` | Checks if value is not in an array of disallowed values. |
| `@DoesMatch(condition: (obj, value) => boolean)` | Checks if a given value follows a custom condition. |
| **Type validation decorators** | |
| `@IsBoolean()` | Checks if a value is a boolean. |
| `@IsDate()` | Checks if the value is a date. |
Expand Down
20 changes: 20 additions & 0 deletions src/decorator/common/DoesMatch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ValidationOptions } from '../ValidationOptions';
import { ValidateBy, buildMessage } from './ValidateBy';

export const DOES_MATCH = 'doesMatch';

/**
* Checks if a given value follows a custom condition.
*/
export function DoesMatch(
condition: (object: any, value: any) => boolean,
validationOptions?: ValidationOptions
): PropertyDecorator {
return ValidateBy({
name: DOES_MATCH,
validator: {
validate: (value, args): boolean => condition(args?.object, value),
defaultMessage: buildMessage(eachPrefix => eachPrefix + '$property does not match', validationOptions),
},
});
}
1 change: 1 addition & 0 deletions src/decorator/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export * from './common/IsEmpty';
export * from './common/IsNotEmpty';
export * from './common/IsIn';
export * from './common/IsNotIn';
export * from './common/DoesMatch';

// -------------------------------------------------------------------------
// Number checkers
Expand Down
33 changes: 33 additions & 0 deletions test/functional/validation-functions-and-decorators.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ import {
isTaxId,
IsTaxId,
IsISO4217CurrencyCode,
DoesMatch,
} from '../../src/decorator/decorators';
import { Validator } from '../../src/validation/Validator';
import { ValidatorOptions } from '../../src/validation/ValidatorOptions';
Expand Down Expand Up @@ -519,6 +520,38 @@ describe('IsNotIn', () => {
});
});

describe('DoesMatch', () => {
const validValues = ['foobar'];
const invalidValues = ['bar'];
class MyClass {
@DoesMatch((obj, value) => obj.propertyToMatch === value)
someProperty: string;

@IsString()
propertyToMatch: string;
}

it('should not fail if validator.validate said that its valid', () => {
const obj = new MyClass();
obj.propertyToMatch = 'foobar';
return checkValidValues(obj, validValues);
});

it('should fail if validator.validate said that its invalid', () => {
const obj = new MyClass();
obj.propertyToMatch = 'foobar';
return checkInvalidValues(obj, invalidValues);
});

it('should return error object with proper data', () => {
const obj = new MyClass();
obj.propertyToMatch = 'foobar';
const validationType = 'doesMatch';
const message = 'someProperty does not match';
return checkReturnedError(obj, invalidValues, validationType, message);
});
});

// -------------------------------------------------------------------------
// Specifications: type check
// -------------------------------------------------------------------------
Expand Down