All files / vae VaeDateSchema.ts

100% Statements 12/12
60% Branches 3/5
100% Functions 7/7
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 44 45 46 47 48 49 50 51 52 53 54 55 56 57                  9x     9x 7x                       2x 2x 2x 2x                               2x 2x 2x 2x                  
import { anyToDate, isDate, isValid } from '../date'
import { Nullable } from '../types'
import { VaeLocale, VaeLocaleMessage } from './VaeLocale'
import { VaeSchema } from './VaeSchema'
 
export class VaeDateSchema<
  T0 extends Nullable<Date> = Date,
> extends VaeSchema<T0> {
  constructor(message: VaeLocaleMessage = VaeLocale.date.type) {
    super({
      type: 'date',
    })
    this.transform(v => anyToDate(v) as any).check({
      fn: v => isDate(v) && isValid(v),
      message: message,
    })
  }
 
  /**
   * 最小日期
   */
  min(
    value: Date | string | number,
    message: VaeLocaleMessage = VaeLocale.date.min,
  ) {
    const minDate = anyToDate(value)
    const minTime = minDate.getTime()
    return this.check({
      fn: v => minTime <= v.getTime(),
      message: message,
      messageParams: {
        min: minDate,
      },
      tag: 'min',
    })
  }
 
  /**
   * 最大日期
   */
  max(
    value: Date | string | number,
    message: VaeLocaleMessage = VaeLocale.date.max,
  ) {
    const maxDate = anyToDate(value)
    const maxTime = maxDate.getTime()
    return this.check({
      fn: v => maxTime >= v.getTime(),
      message: message,
      messageParams: {
        max: maxDate,
      },
      tag: 'max',
    })
  }
}