@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`
93 lines (92 loc) • 3.78 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 _Required_if, _Required_trimmed;
/**
* https://github.com/atmulyana/react-input-validator
*/
import messages from './messages';
import { isFilled } from './Rule';
import ValidationRule from './ValidationRule';
export class Required extends ValidationRule {
constructor(_if) {
super();
_Required_if.set(this, void 0);
_Required_trimmed.set(this, true);
__classPrivateFieldSet(this, _Required_if, _if, "f");
}
static If(_if) {
return new Required(_if);
}
get priority() { return -Number.MAX_VALUE; }
get errorMessage() {
return this.lang(messages.required);
}
get resultValue() {
return typeof (this.value) == 'string' && __classPrivateFieldGet(this, _Required_trimmed, "f") ? this.value.trim() : this.value;
}
notTrimmed() {
__classPrivateFieldSet(this, _Required_trimmed, false, "f");
return this;
}
validate() {
if (typeof __classPrivateFieldGet(this, _Required_if, "f") == 'function') {
const param = { inputValues: this.inputValues, name: this.name };
if (__classPrivateFieldGet(this, _Required_if, "f").call(this, this.value, param))
this.isValid = isFilled(this.value);
else
this.isValid = true;
}
else {
this.isValid = isFilled(this.value);
}
return this;
}
}
_Required_if = new WeakMap(), _Required_trimmed = new WeakMap();
class RequiredIf extends Required {
constructor() {
super(...arguments);
this.if = Required.If.bind(null);
}
}
export const required = new RequiredIf();
required.arrayAsSingle = function () {
throw new Error("`required` rule object is shared among inputs. If you want to invoke `arrayAsSingle`, use `new Required()` instead.");
};
required.setMessageFunc = function () {
throw new Error("`required` rule object is shared among inputs. If you want to set message, use `new Required()` instead.");
};
required.notTrimmed = function () {
throw new Error("`required` rule object is shared among inputs. If you want to call `notTrimmed`, use `new Required()` instead.");
};
const isFalse = () => false;
export const alwaysValid = Required.If(isFalse);
Object.defineProperty(alwaysValid, 'isValid', {
configurable: false,
enumerable: true,
get() {
return true;
},
set() { },
});
alwaysValid.arrayAsSingle = function () {
//always valid, has no effect
return this;
};
alwaysValid.setMessageFunc = function () {
//always valid, has no effect
return this;
};
alwaysValid.notTrimmed = function () {
//always valid, has no effect
return this;
};