Skip to content

Commit

Permalink
feat: add decorator to check if two properties match typestack#486
Browse files Browse the repository at this point in the history
  • Loading branch information
MamadTvl committed Apr 14, 2024
1 parent c43da04 commit 01099f8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
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

0 comments on commit 01099f8

Please sign in to comment.