@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`
29 lines (28 loc) • 918 B
JavaScript
import messages from './messages';
import ValidationRule from './ValidationRule';
export class Min extends ValidationRule {
constructor(min) {
super();
this.min = min;
this.setPriority(2);
}
get errorMessage() {
return this.lang(messages.min);
}
validate() {
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) => new Min(minVal);