koatty_validation
TypeScript icon, indicating that this package has built-in type declarations

1.3.2 • Public • Published

koatty_validation

Validation Util for Koatty. Based on class-validator, extended parameter type checking and restricted attribute functions.

User Decorators

  • @IsDefined

  • @IsCnName

  • @IsIdNumber

  • @IsZipCode

  • @IsMobile

  • @IsPlateNumber

  • @IsEmail

  • @IsIP

  • @IsPhoneNumber

  • @IsUrl

  • @IsHash

  • @IsNotEmpty

  • @Equals

  • @NotEquals

  • @Contains

  • @IsIn

  • @IsNotIn

  • @IsDate

  • @Gt

  • @Gte

  • @Lt

  • @Lte

  • @Valid

  • @Validated

export class Controller {

    Test(@Valid("IsNotEmpty", "can not be empty!!") id: number){
        //todo
    }

    @Validated() // use dto validation
    TestDto(user: UserDTO) {

    }
}

export class UserDTO {
    @IsNotEmpty({ message: "can not be empty!!" })
    phoneNum: string;

    @IsCnName({ message: "must be cn name"})
    userName: string;
}

Validator for manual

FunctionValidator

  • IsCnName
  • IsIdNumber
  • IsZipCode
  • IsMobile
  • IsPlateNumber
  • IsEmail
  • IsIP
  • IsPhoneNumber
  • IsUrl
  • IsHash
  • IsNotEmpty
  • Equals
  • NotEquals
  • Contains
  • IsIn
  • IsNotIn
  • IsDate
  • Gt
  • Gte
  • Lt
  • Lte

exp:

const str = "";
// throw Error
FunctionValidator.IsNotEmpty(str, "cannot be empty");
FunctionValidator.Contains(str, {message: "must contain s", value: "s"});
// 
if (!ValidFuncs.IsNotEmpty(str)) {
    console.log("empty");
}

ClassValidator

exp:

class SchemaClass {
    @IsDefined
    id: number;
    
    @IsNotEmpty
    name: string;
}


ClassValidator.valid(SchemaClass, {name: ''}).catch(err => {
    console.log(err);
})

Readme

Keywords

none

Package Sidebar

Install

npm i koatty_validation

Weekly Downloads

15

Version

1.3.2

License

BSD-3-Clause

Unpacked Size

121 kB

Total Files

12

Last publish

Collaborators

  • richenlin