-
Notifications
You must be signed in to change notification settings - Fork 827
Open
Labels
flag: needs discussionIssues which needs discussion before implementation.Issues which needs discussion before implementation.type: featureIssues related to new features.Issues related to new features.
Description
It’d be great if class-validator could inject the original HTTP request (e.g., req.subdomains
) into custom validator constraints—without hacks like manipulating the DTO or using interceptors.
Motivation – Subdomain-based validation:
export class MyDto {
@IsValidForSubdomain()
someProp: string;
}
// Custom validator should access req.subdomains directly.
Current workaround (ugly):
- Interceptor attaches
req.subdomains
to DTO. - Validator reads it via
args.object['subdomains']
.
Messy, collision-prone, and framework-specific.
Proposed API:
export function IsValidForSubdomain(
validationOptions?: ValidationOptions
) {
return function (object: Object, propertyName: string) {
registerDecorator({
name: "isValidForSubdomain",
target: object.constructor,
propertyName: propertyName,
options: validationOptions,
validator: {
validate(value: any, args: ValidationArguments, req) {
return req.subdomains.includes('api') ? customCheck(value) : true;
}
}
});
};
}
Benefits:
- Clean and explicit
- No DTO pollution
- Framework-agnostic and safer DX
Metadata
Metadata
Assignees
Labels
flag: needs discussionIssues which needs discussion before implementation.Issues which needs discussion before implementation.type: featureIssues related to new features.Issues related to new features.