ido

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.4 kB
4
Indexable
//this is the class
export class Post {
  @MinLength(2)
  @MaxLength(15)
  @IsString()
  @IsNotEmpty()
  title?: string;

 
  @Min(1)
  // @Max(10)
  @IsInt()
  amount!: number;

  isValid!: boolean






 validate() {
    // if(Array.isArray(input)){
    //   [this.title, this.amount] = <[string,number]>input.gatherUserInput();
    // }
    const val = validate(this).then((errors) => {
      // errors is an array of validation errors
      if (errors.length > 0) {
        console.log("validation failed. errors: ", errors);
        console.log(typeof errors[0].constraints);
        alertAllErrors(errors);
        this.isValid = false;
      }
      else{
        console.log("validation succeed");
        this.isValid = true;
      }
    });
    

    //alert function for showing errors infront
    function alertAllErrors(errors: ValidationError[]) {
      // errors is an array of validation errors
      // has property object 'constraints' which has the error description messages
      errors.forEach((element) => {
        if (element.constraints) {
          for (const value of Object.values(element.constraints)) {
            alert(value);
          }
        }
      });
    }
    validateOrReject(this).catch((errors) => {
      console.log("Promise rejected (validation failed). Errors: ", errors);
    });
    return this.isValid;
  }