Let's say one has a class, Post, that one wants to validate.
export class Post {
@IsArray()
tags: string[];
}
Let's now say that he wants to ensure that the word 'mistake' never appears in the tags field. He writes a custom validation decorator to do this:
export function NoMistakes (validationOptions?: ValidationOptions) {
return function (object: Object, propertyName: string) {
registerDecorator({
name: 'noMistakes',
target: object.constructor,
propertyName: propertyName,
options: validationOptions,
validator: {
validate (tags: any, args: ValidationArguments) {
tags.forEach(tag => {
if (tag == 'mistake') {
return false;
}
});
return true;
}
}
});
}
}
Now, he rewrites Post as so:
export class Post {
@IsArray()
@NoMistakes()
tags: string[];
}
The problem occurs, for example, when tags in not of type int. In this case, an error occurs in the custom validator when trying to iterate over something that is not an array.
How can one have the validator @IsArray be executed before the custom validator? In general, is there a way one can choose the order of the validations?