@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`
70 lines (69 loc) • 3.98 kB
JavaScript
"use strict";
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 __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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var _CustomRule_validate, _CustomRule_errorMessage, _CustomRule_message, _CustomRule_result;
Object.defineProperty(exports, "__esModule", { value: true });
exports.rule = exports.CustomRule = 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 CustomRule extends ValidationRule_1.default {
constructor(validateFunc, errorMessage) {
super();
_CustomRule_validate.set(this, void 0);
_CustomRule_errorMessage.set(this, void 0);
_CustomRule_message.set(this, void 0);
_CustomRule_result.set(this, {});
__classPrivateFieldSet(this, _CustomRule_validate, validateFunc, "f");
__classPrivateFieldSet(this, _CustomRule_errorMessage, typeof (errorMessage) == 'string' ? errorMessage : null, "f");
this.setPriority(1000);
}
get errorMessage() {
return __classPrivateFieldGet(this, _CustomRule_message, "f");
}
get resultValue() {
if ('value' in __classPrivateFieldGet(this, _CustomRule_result, "f"))
return __classPrivateFieldGet(this, _CustomRule_result, "f").value;
return super.resultValue;
}
validate() {
const param = {
inputValues: this.inputValues,
name: this.name,
};
const validationValue = __classPrivateFieldGet(this, _CustomRule_validate, "f").call(this, this.value, param); //It may return `true` if valid or an error message
this.isValid = validationValue === true;
delete __classPrivateFieldGet(this, _CustomRule_result, "f").value;
if (this.isValid) {
__classPrivateFieldSet(this, _CustomRule_message, null, "f");
if ('resultValue' in param)
__classPrivateFieldGet(this, _CustomRule_result, "f").value = param.resultValue;
}
else {
const msg = (typeof (validationValue) == 'string') ? validationValue.trim() :
__classPrivateFieldGet(this, _CustomRule_errorMessage, "f") ? __classPrivateFieldGet(this, _CustomRule_errorMessage, "f").trim() :
javascript_common_1.emptyString;
__classPrivateFieldSet(this, _CustomRule_message, msg || this.lang(messages_1.default.invalid), "f");
}
return this;
}
}
exports.CustomRule = CustomRule;
_CustomRule_validate = new WeakMap(), _CustomRule_errorMessage = new WeakMap(), _CustomRule_message = new WeakMap(), _CustomRule_result = new WeakMap();
const rule = (validateFunc, errorMessage) => new CustomRule(validateFunc, errorMessage);
exports.rule = rule;