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

Passing context with custom validator #292

Closed
adamalfredsson opened this issue Nov 29, 2018 · 2 comments
Closed

Passing context with custom validator #292

adamalfredsson opened this issue Nov 29, 2018 · 2 comments
Labels
type: fix Issues describing a broken feature.

Comments

@adamalfredsson
Copy link

I'm trying to pass a context object on my custom validator but I can't access it from the ValidationError object. Am I missing something?

class IsLongerThanValidator implements ValidatorConstraintInterface {
    validate(value: any, args: ValidationArguments) {
        const [relatedPropertyName] = args.constraints;
        const relatedValue = (args.object as any)[relatedPropertyName];
        return  typeof value === "string" &&
            typeof relatedValue === "string" &&
            value.length > relatedValue.length; // you can return a Promise<boolean> here as well, if you want to make async validation
    }
}

export function IsLongerThan(property: string, validationOptions?: ValidationOptions) {
   return function (object: Object, propertyName: string) {
        registerDecorator({
            name: "isLongerThan",
            target: object.constructor,
            propertyName: propertyName,
            constraints: [property],
            options: validationOptions,
            validator: IsLongerThanValidator
        });
   };
}
class MyForm {
    title = 'Default title';

    @IsLongerThan('title', {
        context: {
            i18n: 'validations.isLongerThan',
            value: 'title',
        },
    })
    subtitle?: string;
}
const [error] = await validate(myForm)
console.log(error.property) // subtitle
console.log(error.contexts) // undefined
@alex-adamant
Copy link

Yes, passing context doesn't work for custom validations and for IsDefined decorator either. mapContext function in ValidationExecutor runs only once for default metadatas and not for definedMetadatas or customValidationMetadatas. I guess fix should be rather simple.

chiangf added a commit to chiangf/class-validator that referenced this issue Dec 15, 2018
Context wasn't being passed in for isDefined and custom validators.

typestack#292
chiangf added a commit to chiangf/class-validator that referenced this issue Dec 20, 2018
Context wasn't being passed in for isDefined and custom validators.

typestack#292
dolsem pushed a commit to dolsem/class-validator that referenced this issue Aug 7, 2019
Context wasn't being passed in for isDefined and custom validators.

typestack#292
@vlapo vlapo added the type: fix Issues describing a broken feature. label Oct 21, 2019
chiangf added a commit to chiangf/class-validator that referenced this issue Oct 23, 2019
Context wasn't being passed in for isDefined and custom validators.

typestack#292
@vlapo vlapo closed this as completed in 0ef898e Oct 28, 2019
@lock
Copy link

lock bot commented Nov 4, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: fix Issues describing a broken feature.
Development

No branches or pull requests

3 participants