Options
All
  • Public
  • Public/Protected
  • All
Menu

Module validate/use

Index

Functions

Validate

0.0.46 provide
  • Validate(): (target: any, propertyKey: DecoratorKey, descriptor: PropertyDescriptor) => void
  • 类方法检验

    example

    基础实例

      class TO {}
    
     @Rule(TO)
      class UserDTO extends TO {
        @Rule(RuleType.number().max(10))
        age: number;
      }
    
     @Rule(UserDTO)
      class HelloDTO extends UserDTO {
      }
    
     class Hello {
        @Validate()
        school(a, data: HelloDTO) {
          return data;
        }
      }
      const user = {
        age: 8,
      };
      const result = new Hello().school(1, user);
      expect(result).toEqual(user);
    

    Returns (target: any, propertyKey: DecoratorKey, descriptor: PropertyDescriptor) => void

      • (target: any, propertyKey: DecoratorKey, descriptor: PropertyDescriptor): void
      • Parameters

        • target: any
        • propertyKey: DecoratorKey
        • descriptor: PropertyDescriptor

        Returns void

ValidateAndTransformComponentProps

0.0.46 provide
  • ValidateAndTransformComponentProps<T>(dto: {}): <P>(constructor: P) => { prototype: any } & P
  • 组件的装饰器使用方式

    example

    基础使用方式

      export class DataDto {
        @Rule(RuleType.string().required())
        @addPre('test') // 添加前缀
        bizCode: string;
    
       @Rule(RuleType.string().required())
        @addPre('test') // 添加前缀
        bizName: string;
    
       @Rule(RuleType.number())
        @addAlarm // 报警装饰
        note?: string;
      }
    
     @ValidateComponentProps(DataDto)
      class Node extends Component<DataDto> {
        IRender() {
          console.log(this.props.bizCode, this.props.bizName, this.props.note);
        }
      }
     new Node({
        bizCode: '你好',
        bizName: '世界',
      }).IRender();
      // output:
      // Debugger attached.
      // DataDto.note: 参数告警
      // test-你好 test-世界 undefined
    

    Type parameters

    • T

    Parameters

    • dto: {}

    Returns <P>(constructor: P) => { prototype: any } & P

      • <P>(constructor: P): { prototype: any } & P
      • Type parameters

        • P: new (...args: any[]) => {}

        Parameters

        • constructor: P

        Returns { prototype: any } & P

validateComponentPropsHoc

0.0.46 provide
  • validateComponentPropsHoc<T>(dto: T): <F>(FcComponent: F) => F
  • 通过HOC的方式检验组件服务端数据

    example

    基础使用方式

       export class DataDto {
         @Rule(RuleType.string().required())
         @addPre('test') // 添加前缀
         bizCode: string;
    
         @Rule(RuleType.string().required())
         @addPre('test') // 添加前缀
         bizName: string;
    
         @Rule(RuleType.number())
         @addAlarm // 报警装饰
         note?: string;
       }
    
       class Node extends Component<DataDto> {
         IRender() {
           console.log(this.props.bizCode, this.props.bizName, this.props.note);
         }
       }
    
       const INode = ValidateComponentPropsHoc(DataDto)(Node)
    
       new INode({
         bizCode: '你好',
         bizName: '世界',
       }).IRender();
    
       // output:
       // Debugger attached.
       // DataDto.note: 参数告警
       // test-你好 test-世界 undefined
    

    Type parameters

    • T: new () => any

    Parameters

    • dto: T

    Returns <F>(FcComponent: F) => F

      • <F>(FcComponent: F): F
      • Type parameters

        • F: (...args: any) => any

        Parameters

        • FcComponent: F

        Returns F

validateInterfaceData

0.0.46 provide
  • validateInterfaceData<T>(dto: {}): (value: Pick<FilterDtoFunction<T>, {[ K in string | number | symbol]: FilterDtoFunction<T>[K] extends never ? never : K }[keyof T]>) => T
  • 校验服务端数据的使用方式

    example

    基础使用方式

      export class DataDto {
        @Rule(RuleType.string().required())
        @addPre('test') // 添加前缀
        bizCode: string;
    
       @Rule(RuleType.string().required())
        @addPre('test') // 添加前缀
        bizName: string;
    
       @Rule(RuleType.number())
        @addAlarm // 报警装饰
        note?: string;
      }
    
     const result = validateInterfaceData(DataDto)({
        bizCode: '你好',
        bizName: '世界',
        note: '213',
      });
    
     console.log(result.bizCode, result.bizCode, result.note);
     // console.log test-你好 test-你好 213
    

    Type parameters

    • T

    Parameters

    • dto: {}

    Returns (value: Pick<FilterDtoFunction<T>, {[ K in string | number | symbol]: FilterDtoFunction<T>[K] extends never ? never : K }[keyof T]>) => T

      • (value: Pick<FilterDtoFunction<T>, {[ K in string | number | symbol]: FilterDtoFunction<T>[K] extends never ? never : K }[keyof T]>): T
      • Parameters

        • value: Pick<FilterDtoFunction<T>, {[ K in string | number | symbol]: FilterDtoFunction<T>[K] extends never ? never : K }[keyof T]>

        Returns T