添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

NestJs along with class-validator is a good combination for validating API responses. Both are well documented but some needed use cases are not covered assuming developer to figure out. Below are some few cheat sheets which helped me in defining those cases and you can use these to scale to some extent.

} from ' class-validator ' const weekdays = [ ' monday ' , ' tuesday ' , ' wednesday ' , ' thursday ' , ' friday ' ]; export class SpecificString { @ IsArray () @ ArrayNotEmpty () @ IsIn ( weekdays ) day : string [] Enter fullscreen mode Exit fullscreen mode // check if every array item is string @ IsString ({ each : true }) // Pass `each: true` when you want this validation to run on every array item // Check if every array item has max length @ MaxLength ( 6 , { each : true }) // Check if every array item is only alphabets @ Matches ( ' ^[a-zA-Z \\ s]+$ ' , undefined , { each : true }) // CHeck if every array item contains a text @ Contains ( ' hello ' , { each : true }) stringArray : string [] Enter fullscreen mode Exit fullscreen mode } from ' class-validator ' @ ValidatorConstraint ({ name : ' arrayPrefixValidator ' }) export class ArrayPrefixValidator implements ValidatorConstraintInterface { validate ( values : string [] = []): boolean { if ( values . length ) { return values . every (( value ) => value . startsWith ( ' str- ' )) return false export class StringArray { @ IsArray () @ ArrayNotEmpty () // Check if every array item contains a prefix str- @ Validate ( ArrayPrefixValidator , { message : ' No str- prefix ' }) stringArray : string [] Enter fullscreen mode Exit fullscreen mode @ ValidatorConstraint ({ name : ' isDividedBy17 ' }) export class IsDividedBy17 implements ValidatorConstraintInterface { validate ( value : number ): boolean { if ( value { return value % 17 === 0 return false export class SpecificNumber { @ IsNumber () @ IsNotEmpty () // Check if number is divisible by 17 @ Validate ( IsDividedBy17 , { message : ' No divided by 17 ' }) specificNumber : number Enter fullscreen mode Exit fullscreen mode @ ValidatorConstraint ({ name : ' isEvenNumber ' }) export class IsEvenNumber implements ValidatorConstraintInterface { validate ( numbers : number ): boolean { if ( numbers { return numbers . every ( number => number % 2 === 0 ) return false export class SpecificNumber { @ IsNumber () @ IsNotEmpty () // Check if everynumber is even @ Validate ( IsEvenNumber , { message : ' No divided by 17 ' }) numbersList : number [] Enter fullscreen mode Exit fullscreen mode yearOfRelease: 2012 , languages: [ "en" , "fr" , "es" ] gener: [ "animation" , "kids" ] Enter fullscreen mode Exit fullscreen mode @ ValidatorConstraint ({ name : ' isEvenNumber ' }) export class IsEvenNumber implements ValidatorConstraintInterface { validate ( numbers : number ): boolean { if ( numbers { return numbers . every ( number => number % 2 === 0 ) return false const validLanguages = [ " en " , " es " , " fr " ] const validGeneres = [ " sci-fi " , " thriller " , " animation " , " horror " , " vintage " ] // 💡 Types here export class Movie { @ IsString () @ IsNotEmpty () @ ISRequired () name : string @ IsNotEmpty () @ IsNumber () @ ISRequired () yearOfRelease : number @ IsNotEmpty () @ IsString () @ IsIn ( validLanguages ) @ ISRequired () languages : string [] @ IsNotEmpty () @ IsString () @ IsIn ( validGeneres ) @ ISRequired () genre : string [] export class MoviesList { @ IsArray () @ ArrayNotEmpty () @ Type (() => Movie ) movies : Movie [] Enter fullscreen mode Exit fullscreen mode Once unpublished, all posts by sarathsantoshdamaraju will become hidden and only accessible to themselves. If sarathsantoshdamaraju is not suspended, they can still re-publish their posts from their dashboard.