UNPKG

@react-input-validator/rules

Version:

The validation rule objects used by the packages: `@react-input-validator/core`, `@react-input-validator/native` and `@react-input-validator/web`

40 lines (34 loc) 1.22 kB
/** * https://github.com/atmulyana/react-input-validator */ import type {ComparableType, IRule} from './Rule'; import messages from './messages'; import ValidationRule from './ValidationRule'; export class Min extends ValidationRule<ComparableType> { constructor(min: ComparableType | (() => ComparableType)) { super(); this.min = min; this.setPriority(2); } min: ComparableType | (() => ComparableType); get errorMessage() { return this.lang(messages.min); } validate(): IRule<ComparableType> { const min = typeof(this.min) == 'function' ? this.min() : this.min; let valType = typeof this.value, limitType = typeof min; if (valType == 'bigint') valType = 'number'; if (limitType == 'bigint') limitType = 'number'; if (limitType == valType && ( valType == 'number' || valType == 'string' || (this.value instanceof Date && min instanceof Date) )) this.isValid = this.value >= min; else this.isValid = false; return this; } } export const min = (minVal: ComparableType | (() => ComparableType)): Min => new Min(minVal);