Skip to content

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

Closed

Description

@NoNameProvided

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.

Activity

  1. added
    type: fix
    status: has PR
    on Aug 7, 2020
  2. added
    flag: BREAKING CHANGE
    on Nov 20, 2022
  3. added
    status: fixed
    and removed
    status: has PR
    on Nov 20, 2022
  4. added
    status: done/released
    and removed
    status: fixed
    on Dec 9, 2022
  5. github-actions commented on Jan 10, 2023

    @github-actions

    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.

  6. locked as resolved and limited conversation to collaborators on Jan 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    flag: BREAKING CHANGEstatus: done/releasedtype: fix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions