All files / validator/yupTypes mixed.ts

0% Statements 0/0
0% Branches 0/0
0% Functions 0/0
0% Lines 0/0

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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203                                                                                                                                                                                                                                                                                                                                                                                                                     
import { ArraySchema } from './array'
import { BooleanSchema } from './boolean'
import { DateSchema } from './date'
import { LocaleValue, MixedLocale } from './Locale'
import { NumberSchema } from './number'
import { ObjectSchema } from './object'
import { Refable } from './ref'
import { StringSchema } from './string'
import { ValidationError } from './ValidationError'
 
export interface SchemaDescription {
  type: string
  label: string
  meta: {}
  tests: Array<{
    name: string
    params: {}
  }>
}
 
export interface SchemaValidateOptions {
  strict?: boolean
  abortEarly?: boolean
  stripUnknown?: boolean
  recursive?: boolean
  context?: {}
}
 
export interface SchemaTestOptions<TSchema, TValue, TParams extends {} = {}> {
  name: string
  message: LocaleValue<TParams>
  test: (
    this: {
      path: string
      schema: TSchema
      options: SchemaValidateOptions
      parent: any
      createError: (options: {
        path: string
        message: LocaleValue
        params?: TParams
      }) => ValidationError
    },
    value: TValue,
  ) => boolean | Promise<boolean>
  params?: TParams
  exclusive?: boolean
}
 
export interface MixedSchema<T = any> {
  __isYupSchema__: true
 
  type: 'mixed' | 'string' | 'number' | 'boolean' | 'object' | 'date' | 'array'
 
  clone(): this
 
  label(label: string): this
 
  meta(meta: {}): this
 
  describe(): SchemaDescription
 
  /** @类型不友好 */
  concat(schema: MixedSchema<any>): this
 
  validate(value: T, options?: SchemaValidateOptions): Promise<T>
 
  validateSync(value: T, options?: SchemaValidateOptions): T
 
  /**
   * 验证增强,包括:对象顺序验证、返回结果包含错误信息。
   */
  validatePlus(
    value: T,
    options?: SchemaValidateOptions,
  ): Promise<{
    error?: ValidationError
    data: T
  }>
 
  /**
   * 验证增强,包括:对象顺序验证、返回结果包含错误信息。
   */
  validatePlusSync(
    value: T,
    options?: SchemaValidateOptions,
  ): {
    error?: ValidationError
    data: T
  }
 
  /** @类型不友好 */
  validateAt(
    path: string,
    value: any,
    options?: SchemaValidateOptions,
  ): Promise<any>
 
  /** @类型不友好 */
  validateSyncAt(path: string, value: any, options?: SchemaValidateOptions): any
 
  isValid(value: T, options?: SchemaValidateOptions): Promise<boolean>
 
  isValidSync(value: T, options?: SchemaValidateOptions): boolean
 
  cast(value: T, options?: SchemaValidateOptions): any
 
  isType(value: T): boolean
 
  strict(isStrict?: boolean): this
 
  strip(stripField?: boolean): this
 
  withMutation<X>(fn: (schema: this) => X): X
 
  default(value: T | (() => T)): this
 
  default(): T | undefined
 
  nullable(isNullable?: boolean): this
 
  allowEmptyString(): this
 
  required(message?: MixedLocale['required']): this
 
  notRequired(): this
 
  defined(): this
 
  typeError(message: LocaleValue): this
 
  oneOf(
    arrayOfValues: Record<any, T> | Refable<T>[],
    message?: MixedLocale['oneOf'],
  ): this
 
  /** oneOf 的别名 */
  enum(
    arrayOfValues: Record<any, T> | Refable<T>[],
    message?: MixedLocale['oneOf'],
  ): this
 
  /** oneOf 的别名 */
  equals(
    arrayOfValues: Record<any, T> | Refable<T>[],
    message?: MixedLocale['oneOf'],
  ): this
 
  notOneOf(arrayOfValues: Refable<T>[], message?: MixedLocale['notOneOf']): this
 
  when(builder: (value: T, schema: this) => this): this
 
  when<V>(
    key: string,
    builder: {
      is: boolean | ((value: V) => boolean)
      then: GetSchema<T>
      otherwise: GetSchema<T>
    },
  ): this
 
  when<V>(key: string, builder: (value: V, schema: this) => this): this
 
  test(
    test: SchemaTestOptions<this, T>['test'] | RegExp,
    message?: SchemaTestOptions<this, T>['message'],
  ): this
 
  test<TParams extends {} = {}>(
    options: SchemaTestOptions<this, T, TParams>,
  ): this
 
  transform(
    transformer: (this: this, currentValue: T, originalValue: T) => T,
  ): this
}
 
export type GetSchema<T> =
  | (// 为何要加 []
    // https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#distributive-conditional-types
    [T] extends [string]
      ? StringSchema<T>
      : [T] extends [number]
      ? NumberSchema<T>
      : [T] extends [boolean]
      ? BooleanSchema<T>
      : [T] extends [Date]
      ? DateSchema<T>
      : T extends Array<infer X>
      ? ArraySchema<X>
      : T extends {}
      ? ObjectSchema<T>
      : MixedSchema<T>)
  | MixedSchema<T>
 
export type GetObjectSchema<T extends {}> = {
  [K in keyof T]: GetSchema<T[K]>
}
 
export declare function mixed<T = any>(
  payload?: (schema: MixedSchema<T>) => MixedSchema<T>,
): MixedSchema<T>