@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 Max extends ValidationRule {
constructor(max) {
super();
this.max = max;
this.setPriority(2);
}
get errorMessage() {
return this.lang(messages.max);
}
validate() {
const max = typeof (this.max) == 'function' ? this.max() : this.max;
let valType = typeof this.value, limitType = typeof max;
if (valType == 'bigint')
valType = 'number';
if (limitType == 'bigint')
limitType = 'number';
if (limitType == valType && (valType == 'number' ||
valType == 'string' ||
(this.value instanceof Date && max instanceof Date)))
this.isValid = this.value <= max;
else
this.isValid = false;
return this;
}
}
export const max = (maxVal) => new Max(maxVal);