I am validating that a string is either a MongoId or a UUID. I had to implement my own validator for this:
import {
ValidatorConstraint,
ValidatorConstraintInterface,
ValidationOptions,
registerDecorator,
isUUID,
isMongoId,
} from 'class-validator';
@ValidatorConstraint()
export class IsUUIDOrMongoIdConstraint implements ValidatorConstraintInterface {
validate(text: string): boolean {
return isUUID(text) || isMongoId(text);
}
defaultMessage(): string {
return 'Text ($value) must be a UUID or a MongoId';
}
}
export function IsUUIDOrMongoId(validationOptions?: ValidationOptions) {
return function (object: Object, propertyName: string) {
registerDecorator({
target: object.constructor,
propertyName: propertyName,
options: validationOptions,
constraints: [],
validator: IsUUIDOrMongoIdConstraint,
});
};
}
A workaround is also a somewhat complex RegExp, but that doesn't work in all cases where you want to compose different validators.
Proposed solution
Dream scenario would be to to be able to compose different validators, similar to how applyDecorators(...) works. I am thinking of something like this:
Background
I am validating that a string is either a MongoId or a UUID. I had to implement my own validator for this:
A workaround is also a somewhat complex RegExp, but that doesn't work in all cases where you want to compose different validators.
Proposed solution
Dream scenario would be to to be able to compose different validators, similar to how applyDecorators(...) works. I am thinking of something like this:
Or something like this: