@apptane/react-ui-form
Version:
Form layout component in Apptane React UI framework
124 lines (96 loc) • 3.38 kB
JavaScript
export default class Validator {
constructor(guard) {
this.__guard = void 0;
this.__guard = guard;
}
static require(value) {
return value != null && (typeof value === "string" ? value.length > 0 : true);
}
static get optional() {
return this.__optional;
}
static get required() {
return this.__required;
}
/**
* Returns `true` or the specified error message if the value is not
* a valid email address.
*/
email(value, message) {
if (this.__guard && !this.__guard(value)) {
return message !== null && message !== void 0 ? message : true;
}
return Validator.email(value) ? false : message !== null && message !== void 0 ? message : true;
}
/**
* Returns `true` or the specified error message if the value is not
* a string within the specified length.
*/
string(value, length, message) {
if (this.__guard && !this.__guard(value)) {
return message !== null && message !== void 0 ? message : true;
}
return Validator.string(value, length) ? false : message !== null && message !== void 0 ? message : true;
}
/**
* Returns `true` or the specified error message if the value is not
* a number within the specified range.
*/
number(value, min, max, message) {
if (this.__guard && !this.__guard(value)) {
return message !== null && message !== void 0 ? message : true;
}
return Validator.number(value, min, max) ? false : message !== null && message !== void 0 ? message : true;
}
/**
* Returns `true` or the specified error message if the value is not
* an integer within the specified range.
*/
integer(value, min, max, message) {
if (this.__guard && !this.__guard(value)) {
return message !== null && message !== void 0 ? message : true;
}
return Validator.integer(value, min, max) ? false : message !== null && message !== void 0 ? message : true;
}
/**
* Validates email address.
*/
static email(value) {
if (value == null) {
return false;
}
return /^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*$/.test(value);
}
/**
* Validates string.
*/
static string(value, length) {
if (value == null) {
return false;
}
return !length || value.length <= length;
}
/**
* Validates real number.
*/
static number(value, min, max) {
if (value == null) {
return false;
}
const number = parseFloat(value);
return !isNaN(number) && number >= min && number <= max;
}
/**
* Validates integer number.
*/
static integer(value, min, max) {
if (value == null) {
return false;
}
const number = parseInt(value, 10);
return !isNaN(number) && number >= min && number <= max;
}
}
Validator.__required = new Validator(Validator.require);
Validator.__optional = new Validator(undefined);
//# sourceMappingURL=Validator.js.map