Class-validator: ValidateNested, unhandled exception when provided array contains null

Created on 2 Feb 2019  路  5Comments  路  Source: typestack/class-validator

Hi!

I'm getting TypeError when validating array of objects that contains nulls among another objects.
Let's consider the next example:

class MySubClass {
    @MinLength(5)
    name: string;
}

class MyClass {
    @ValidateNested()
    mySubClasses: MySubClass[];
}

const model = new MyClass();
model.mySubClasses = [null, new MySubClass()];

Here you can see null in mySubClasses array. Looks legit, right?

But invocation of validator.validate(model) causes the next error (I've succeeded reproducing this in tests):

TypeError: Cannot read property 'constructor' of null
    at ValidationExecutor.execute (build/compiled/src/validation/ValidationExecutor.js:43:88)
    at /Users/user/Develop/class-validator/build/compiled/src/validation/ValidationExecutor.js:218:27
    at Array.forEach (<anonymous>)
    at /Users/user/Develop/class-validator/build/compiled/src/validation/ValidationExecutor.js:215:23
    at Array.forEach (<anonymous>)
    at ValidationExecutor.nestedValidations (build/compiled/src/validation/ValidationExecutor.js:209:19)
    at /Users/user/Develop/class-validator/build/compiled/src/validation/ValidationExecutor.js:82:19
    at Array.forEach (<anonymous>)
    at ValidationExecutor.execute (build/compiled/src/validation/ValidationExecutor.js:62:39)
    at Validator.coreValidate (build/compiled/src/validation/Validator.js:63:18)
    at Validator.validate (build/compiled/src/validation/Validator.js:72:21)
    at Context.<anonymous> (build/compiled/test/functional/nested-validation.spec.js:177:26)

Desired behavior: don't fail on null, just skip it and continue validating the rest elements of array.

fix

Most helpful comment

But I can see the same error with @ValidateNested({ each: true })

All 5 comments

ValidateNested is not for arrays, { each: true } is

But I can see the same error with @ValidateNested({ each: true })

Also I'm getting the next error with whitelist: true

TypeError: Cannot delete property '0' of [object String]
    at /Users/user/Develop/class-validator/build/compiled/src/validation/ValidationExecutor.js:106:89
    at Array.forEach (<anonymous>)
    at ValidationExecutor.whitelist (build/compiled/src/validation/ValidationExecutor.js:106:38)
    at ValidationExecutor.execute (build/compiled/src/validation/ValidationExecutor.js:60:18)
    at /Users/user/Develop/class-validator/build/compiled/src/validation/ValidationExecutor.js:218:27
    at Array.forEach (<anonymous>)
    at /Users/user/Develop/class-validator/build/compiled/src/validation/ValidationExecutor.js:215:23
    at Array.forEach (<anonymous>)
    at ValidationExecutor.nestedValidations (build/compiled/src/validation/ValidationExecutor.js:209:19)
    at /Users/user/Develop/class-validator/build/compiled/src/validation/ValidationExecutor.js:82:19
    at Array.forEach (<anonymous>)
    at ValidationExecutor.execute (build/compiled/src/validation/ValidationExecutor.js:62:39)
    at Validator.coreValidate (build/compiled/src/validation/Validator.js:63:18)
    at Validator.validate (build/compiled/src/validation/Validator.js:72:21)
    at Context.<anonymous> (build/compiled/test/functional/nested-validation.spec.js:178:26)

in this test:

    class MySubClass {
        @MinLength(5)
        name: string;
    }

    class MyClass {
        @ValidateNested({ each: true })
        mySubClasses: MySubClass[];
    }

    const model = new MyClass();
    model.mySubClasses = <any> ["test", new MySubClass()];
    model.mySubClasses[1].name = "my";

    return validator.validate(model, { whitelist: true })

It seems that ValidationExecutor.execute() can't handle primitive types properly.
Maybe I'm using @ValidateNested incorrectly here?

Worth to update this example in source code after fix.

It looks like there's a PR fixing this issue and it's been tagged as part of the 0.10.1 milestone. Anyone know when that milestone is targeting release?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dicgf8 picture dicgf8  路  6Comments

TomMarius picture TomMarius  路  3Comments

btd1337 picture btd1337  路  5Comments

NoNameProvided picture NoNameProvided  路  3Comments

jerradpatch picture jerradpatch  路  4Comments