All files validationTypes.ts

100% Statements 21/21
100% Branches 4/4
100% Functions 11/11
100% Lines 11/11

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43                                                              31015x 14778x 458x 276x 307x 318x 7984x 3x 15492x 31103x 13x  
type NonEmptyArray<T> = [T, ...T[]]
export type TypeMeta = {name?: string, description?: string}
type CustomValueType = string // Maybe use nominal typing to prevent arbitrary strings
export type ValueTypes = ValueType | NonEmptyArray<ValueType>
export type TypeDef = {$types: {[key:string]:ValueTypes}} & ObjectType
export type Validation = ValueTypes | TypeDef
export type SimpleTypes = 'string' | 'boolean' | 'number' | 'integer' | 'null' | '?' | 'any' | CustomValueType
export type ObjectType = { [key: string]: ValueTypes }
export type EnumType = TypeMeta & { $enum: string[]}
export type ArrayType = TypeMeta & { $array: ValueTypes, minLength?: number, maxLength?:number}
export type MapType = TypeMeta & { $map: ValueTypes, regex?: string, minLength?: number, maxLength?:number }
export type AndType = TypeMeta & { $and: (ObjectType | CustomValueType)[] }
export type StringType = TypeMeta & { select?: string, $string: {
  minLength?: number, maxLength?: number, regex?: string}}
export declare type NumberType = TypeMeta & {
  postfix?: string
  $number: { min?: number, max?: number, step?: number }
}
export declare type MetaType = TypeMeta & { $type: ValueTypes }
 
export type ValueType =
  | SimpleTypes
  | EnumType
  | ObjectType
  | ArrayType
  | StringType
  | NumberType
  | MetaType
  | MapType
  | AndType
 
export const isSimpleType = (tbd: any): tbd is SimpleTypes => typeof tbd === 'string'
export const isArray = (tbd: any): tbd is ArrayType => tbd.$array
export const isMap = (tbd: any): tbd is MapType => tbd.$map
export const isString = (tbd: any): tbd is StringType => tbd.$string
export const isNumber = (tbd: any): tbd is NumberType => tbd.$number
export const isMeta = (tbd: any): tbd is MetaType => tbd && tbd.$type
export const isEnum = (tbd: any): tbd is EnumType => tbd.$enum
export const isObj = (tbd: any): tbd is ObjectType =>
  tbd instanceof Object && !Object.keys(tbd).some(x => x.startsWith('$'))
export const isTypeDefValidation = (tbd: any): tbd is TypeDef => tbd.$types
export const isAnd = (tbd: any): tbd is TypeDef => tbd.$and