Related to: https://github.com/typestack/class-transformer/issues/226
I use class-transformer with class-validator to validate request body. I would like to validate class with nested object - I need to check whether the given object is correct but don't allow to provide an array of objects
// Class definition with validation rules
class A {
@IsDefined()
@IsString()
public x: string;
// ... other properties
}
class B {
@IsDefined()
@IsString()
public type!: string;
@IsDefined()
@ValidateNested()
@Type(() => A)
public nested!: A;
}
// invalid request object with array (B[]) instead of object (B)
var requestObjB = {
type: "test type"
nested: [{ x: "test x" }]
}
// try to validate request object
validate(plainToClass(B, requestObjB))
Validation failed and I get this error:
TypeError: newValue_1.push is not a function
at /Users/bartosz/dev/user-data-api/src/TransformOperationExecutor.ts:67:34
at Array.forEach (<anonymous>)
at TransformOperationExecutor.transform (/Users/bartosz/dev/user-data-api/src/TransformOperationExecutor.ts:41:30)
at _loop_1 (/Users/bartosz/dev/user-data-api/src/TransformOperationExecutor.ts:226:43)
at TransformOperationExecutor.transform (/Users/bartosz/dev/user-data-api/node_modules/class-transformer/TransformOperationExecutor.js:240:17)
at _loop_1 (/Users/bartosz/dev/user-data-api/src/TransformOperationExecutor.ts:226:43)
at TransformOperationExecutor.transform (/Users/bartosz/dev/user-data-api/node_modules/class-transformer/TransformOperationExecutor.js:240:17)
Is it possible to solve this problem or maybe there is better way to handle this kind of validation?
public nested!: A[]; not !: A;
@19majkel94 It doesn't satisfied me because I don't want to give a chance to send list of objects for nested property.
The rules are:
馃憤 object (B),
馃憥 array of objects (B[])
When someone send an array of object I would like to respond with validation error: nested property is in wrong format
The problem is with class-transformer - you declare an object type, you provide an array and it fails on transform.
yeah, you are right. I was hoping that there is other way to handle this problem than to validate the format of the request object before converting it with class-transformer, and then re-validate it with the class-validator
@19majkel94 So what's the recommended way forward here (since you're familiar with both xformer and validator)?
Obviously, we can't leave this loophole open :(
Any updates here?
@19majkel94 So what's the recommended way forward here (since you're familiar with both xformer and validator)?
I think the only way how to achieve this is https://github.com/typestack/class-validator#defining-validation-schema-without-decorators but I am not sure in what state is this feature.
It's pretty old but... adding IsNotEmptyObject or IsObject will not allow arrays, not perfect because will add different error, considering other solutions this was good enough for me.
@IsDefined()
@IsNotEmptyObject() / @IsObject()
@ValidateNested()
@Type(() => A)
public nested!: A;
Most helpful comment
It's pretty old but... adding IsNotEmptyObject or IsObject will not allow arrays, not perfect because will add different error, considering other solutions this was good enough for me.