Class-validator: @ValidateNested() not working for me

Created on 14 Sep 2017  路  3Comments  路  Source: typestack/class-validator

Hi,

I have the following

export interface IQuantity {
  quantity: string
  unit: string

  isValid()
}

export class Quantity {
  @IsNumberString( { message: gbv_required_msg } )
  quantity: string

  @IsNotEmpty( { message: gbv_required_msg } )
  unit: string

  isValid(): boolean {
    if ( !_.isNil( this.quantity ) && !_.isNil( this.unit ) ) {
      return true
    }
    return false
  }
}

export interface IBleeding {
  frequency?: IQuantity
  onset?: IOnset
  relationToStool?: Array<string>
  volume?: IQuantity
}

export class Bleeding{

  @ValidateNested()
  frequency?: IQuantity = new Quantity()

  @ValidateNested()
  onset?: IOnset = new Onset()

  @ArrayNotEmpty({message: gbv_required_msg})
  relationToStool?: Array<string> = []

  @ValidateNested()
  volume?: IQuantity = new Quantity()
}

Validation is done below:

`

import { observe } from 'universal-observer'
this._observed =
        observe( classToPlain( this.bleeding ), change => {
          this.dirty = true
          this.errors = []
          let changeObject = change.object

          switch ( this.target ) {
            case 'bleedingCmp -> rosCmp': {
              transformAndValidate
              ( Bleeding, changeObject )
                  .then( ( bleeding: Bleeding ) => {

                    switch ( change.type ) {
                      case 'add':
                        this.bleedingData.emit( updatePayload1( bleeding, change.type, this.payload ) )

                        this._store$.dispatch(
                            {
                              type: BleedingAction.ADD_ROS_DATA,
                              payload: changeObject
                            } )
                        break

                      case 'update':
                        this.bleedingData.emit( updatePayload1( bleeding, change.type, this.payload ) )

                        this._store$.dispatch(
                            {
                              type: BleedingAction.UPDATE_ROS_DATA,
                              payload: changeObject
                            } )
                        break
                    }
                  } )
                  .catch( errors => {
                    this.errors = errors
                    console.log(errors)
            }
              break
          }
        } )

`
The errors printed to the console NEVER include errors of the propertes, nested (decorated with @ValidateNested() properties.

What am I doing wrong?

Cheers

Most helpful comment

@st-clair-clarke @NoNameProvided In that post I explain how I got it working

All 3 comments

Duplicate of https://github.com/pleerock/class-validator/issues/83 @ValidateNested is not working properly, it's not an issue with your code.

@st-clair-clarke @NoNameProvided In that post I explain how I got it working

Closing in favor of #83

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pietrobelluno picture pietrobelluno  路  3Comments

renehauck picture renehauck  路  5Comments

btd1337 picture btd1337  路  5Comments

Eldar-X picture Eldar-X  路  3Comments

twittwer picture twittwer  路  6Comments