@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`
62 lines (61 loc) • 3.58 kB
JavaScript
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 _CustomRule_validate, _CustomRule_errorMessage, _CustomRule_message, _CustomRule_result;
/**
* https://github.com/atmulyana/react-input-validator
*/
import { emptyString } from 'javascript-common';
import messages from './messages';
import ValidationRule from './ValidationRule';
export class CustomRule extends ValidationRule {
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() :
emptyString;
__classPrivateFieldSet(this, _CustomRule_message, msg || this.lang(messages.invalid), "f");
}
return this;
}
}
_CustomRule_validate = new WeakMap(), _CustomRule_errorMessage = new WeakMap(), _CustomRule_message = new WeakMap(), _CustomRule_result = new WeakMap();
export const rule = (validateFunc, errorMessage) => new CustomRule(validateFunc, errorMessage);