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

fix: errors from @NestedValidation are assigned to the child instead of the parent when property is not a class instance #679

Closed
NoNameProvided opened this issue Aug 7, 2020 · 1 comment · Fixed by #673
Labels
flag: BREAKING CHANGE Issues containing a breaking change, requiring a major release. status: done/released Issue has been completed, no further action is needed. type: fix Issues describing a broken feature.

Comments

@NoNameProvided
Copy link
Member

Description

When a property has a @ValidateNested() decorator and the validated data on the property is not an object or array the validation error incorrectly assigned to the children instead of the parent.

Minimal code-snippet showcasing the problem

import 'reflect-metadata';
import { validateSync, IsString, ValidateNested } from 'class-validator';

class SubClass {
  @IsString()
  name: string;
}

class ExampleClass {
  @ValidateNested()
  nestedWithClassValue: SubClass;

  @ValidateNested()
  nestedWithPrimitiveValue: SubClass;
}

const instance = new ExampleClass ();
instance.nestedWithClassValue = new SubClass();
instance.nestedWithPrimitiveValue = "invalid" as any;

const validationErrors = validateSync(instance);

Expected behavior

I have expected the error for the nestedWithPrimitiveValue property to appear on the root object instead of children.

[
  {
    "target": {
      "nestedWithClassValue": {},
      "nestedWithPrimitiveValue": "invalid"
    },
    "value": {},
    "property": "nestedWithClassValue",
    "children": [
      {
        "target": {},
        "property": "name",
        "children": [],
        "constraints": {
          "isString": "name must be a string"
        }
      }
    ]
  },
  {
    "target": {
      "nestedWithClassValue": {},
      "nestedWithPrimitiveValue": "invalid"
    },
    "value": "invalid",
    "property": "nestedWithPrimitiveValue",
    "constraints": {
      "nestedValidation": "nested property nestedWithPrimitiveValue must be either object or array"
    }
  }
]

Actual behavior

Currently the validationErrors will be the following structure:

[
  {
    "target": {
      "nestedWithClassValue": {},
      "nestedWithPrimitiveValue": "invalid"
    },
    "value": {},
    "property": "nestedWithClassValue",
    "children": [
      {
        "target": {},
        "property": "name",
        "children": [],
        "constraints": {
          "isString": "name must be a string"
        }
      }
    ]
  },
  {
    "target": {
      "nestedWithClassValue": {},
      "nestedWithPrimitiveValue": "invalid"
    },
    "value": "invalid",
    "property": "nestedWithPrimitiveValue",
    "children": [
      {
        "value": "invalid",
        "property": "nestedWithPrimitiveValue",
        "constraints": {
          "nestedValidation": "nested property nestedWithPrimitiveValue must be either object or array"
        }
      }
    ]
  }
]

Observe how the nestedValidation constraint is applied to the children instead of the root object.

@NoNameProvided NoNameProvided added type: fix Issues describing a broken feature. status: has PR Issues which has a related PR closing the issue. labels Aug 7, 2020
@NoNameProvided NoNameProvided added the flag: BREAKING CHANGE Issues containing a breaking change, requiring a major release. label Nov 20, 2022
@NoNameProvided NoNameProvided added status: fixed Issues with merged PRs, but not released yet. and removed status: has PR Issues which has a related PR closing the issue. labels Nov 20, 2022
@NoNameProvided NoNameProvided added status: done/released Issue has been completed, no further action is needed. and removed status: fixed Issues with merged PRs, but not released yet. labels Dec 9, 2022
@github-actions
Copy link

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

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
flag: BREAKING CHANGE Issues containing a breaking change, requiring a major release. status: done/released Issue has been completed, no further action is needed. type: fix Issues describing a broken feature.
1 participant