MichalLytek / class-transformer-validator

A simple plugin for class-transformer and class-validator which combines them in a nice and programmer-friendly API.
MIT License
200 stars 19 forks source link

args.value.length not reflecting the true length of the input #2

Closed st-clair-clarke closed 7 years ago

st-clair-clarke commented 7 years ago

Hi, I have the following:

export class Onset {
  @IsNotEmpty( { message: 'Input required' } )
  @MinLength( 6, { message: minLength6Fn } )
  mode: string
}

export function minLength6Fn( args: ValidationArguments ) {
  switch ( args.value.length ) {
    case 0:
    case 1:
    case 2:
    case 3:
    case 4:
    case 5:
      return `Too short, minimum length is ${args.constraints[ 0 ]} characters, but actual is ${args.value.length}`
  }
}

Validation is done as shown below:

 onset = new Onset()
onset.model = 'Sudden'

          transformAndValidate( Onset, currentValue )
                  .then( ( onset: Onset ) => {

                  } )
                  .catch( errors => {
                    console.log( `onset errors | ${JSON.stringify( errors ) }` )
                    console.log( errors )

                    let propError = findPropertyValidationErrors( errors, 'mode' )
                    console.log( propError )
                  } )

The validation error printed to console is '["Too short, minimum length is 6 characters, but actual is 5"]

The ${args.value.length} in return Too short, minimum length is ${args.constraints[ 0 ]} characters, but actual is ${args.value.length} is NOT reflecting the true length of the input.

Is this a bug?

MichalLytek commented 7 years ago

Yeah, I've encountered the bug with @MinLength a year ago, I thought it was patched. Anyway - it's related to class-validator so as it's a peer dependency I'm closing the issue.