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`

53 lines (52 loc) 2.83 kB
"use strict"; 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _Length_message; Object.defineProperty(exports, "__esModule", { value: true }); exports.lengthMax = exports.length = exports.Length = void 0; /** * https://github.com/atmulyana/react-input-validator */ const javascript_common_1 = require("javascript-common"); const messages_1 = __importDefault(require("./messages")); const ValidationRule_1 = __importDefault(require("./ValidationRule")); class Length extends ValidationRule_1.default { 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, javascript_common_1.emptyString, "f"); var val = typeof (this.value) == 'number' ? this.value + javascript_common_1.emptyString : this.value; if (this.min !== undefined && val.length < this.min) __classPrivateFieldSet(this, _Length_message, this.lang(messages_1.default.lengthMin), "f"); if (this.max !== undefined && val.length > this.max) __classPrivateFieldSet(this, _Length_message, this.lang(messages_1.default.lengthMax), "f"); this.isValid = !__classPrivateFieldGet(this, _Length_message, "f"); return this; } } exports.Length = Length; _Length_message = new WeakMap(); const length = (min, max) => new Length(min, max); exports.length = length; const lengthMax = (max) => new Length(undefined, max); exports.lengthMax = lengthMax;