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

Access Root node in custom validation #1431

Open
galwa opened this issue Nov 30, 2021 · 0 comments · May be fixed by #2327
Open

Access Root node in custom validation #1431

galwa opened this issue Nov 30, 2021 · 0 comments · May be fixed by #2327
Labels
flag: needs discussion Issues which needs discussion before implementation. type: feature Issues related to new features.

Comments

@galwa
Copy link

galwa commented Nov 30, 2021

Inline with similar requests such as https://github.com/typestack/class-validator/pull/36
In cases where child/nested object requires a parent value for it's validation

I suggest adding root Object to validation function :

export interface ValidatorConstraintInterface {
    validate(value: any, validationArguments?: ValidationArguments, root ?: any): Promise<boolean> | boolean;
    defaultMessage?(validationArguments?: ValidationArguments): string;
}

It is a relatively simple change on ValidationExecutor that I do not identify and issues with

export class ValidationExecutor {
...
private root:any = undefined;
...
execute(object: object, targetSchema: string, validationErrors: ValidationError[]): void {
...
    if(this.root === undefined){
      this.root = object;
    }

private customValidations(object: object, value: any, metadatas: ValidationMetadata[], error: ValidationError): void {
...
    if (!metadata.each || !(Array.isArray(value) || value instanceof Set || value instanceof Map)) {
          const validatedValue = customConstraintMetadata.instance.validate(value, validationArguments,this.root);...
...
    const validatedSubValues = arrayValue.map((subValue: any) =>
          customConstraintMetadata.instance.validate(subValue, validationArguments,this.root)

From root object one can find any direct parent, as root contains the validate function ValidationArguments object

@galwa galwa added flag: needs discussion Issues which needs discussion before implementation. type: feature Issues related to new features. labels Nov 30, 2021
@magrinj magrinj linked a pull request Dec 4, 2023 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flag: needs discussion Issues which needs discussion before implementation. type: feature Issues related to new features.
Development

Successfully merging a pull request may close this issue.

1 participant