@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`
44 lines (43 loc) • 2.36 kB
JavaScript
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _Length_message;
/**
* https://github.com/atmulyana/react-input-validator
*/
import { emptyString } from "javascript-common";
import messages from './messages';
import ValidationRule from './ValidationRule';
export class Length extends ValidationRule {
constructor(min, max) {
super();
_Length_message.set(this, void 0);
this.min = min;
this.max = max;
this.arrayAsSingle(true);
}
get errorMessage() {
return __classPrivateFieldGet(this, _Length_message, "f");
}
validate() {
__classPrivateFieldSet(this, _Length_message, emptyString, "f");
var val = typeof (this.value) == 'number' ? this.value + emptyString : this.value;
if (this.min !== undefined && val.length < this.min)
__classPrivateFieldSet(this, _Length_message, this.lang(messages.lengthMin), "f");
if (this.max !== undefined && val.length > this.max)
__classPrivateFieldSet(this, _Length_message, this.lang(messages.lengthMax), "f");
this.isValid = !__classPrivateFieldGet(this, _Length_message, "f");
return this;
}
}
_Length_message = new WeakMap();
export const length = (min, max) => new Length(min, max);
export const lengthMax = (max) => new Length(undefined, max);