UNPKG

react-proforma

Version:

React Proforma helps you build simple to complex web forms with ease in React. -- Simplicity where you want it. Flexibility where you need it.

132 lines (131 loc) 5.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var FieldValidator = /** @class */ (function () { function FieldValidator(_value) { this._value = _value; this.errors = []; } FieldValidator.prototype._push = function (msg) { this.errors.push(msg); }; FieldValidator.prototype.min = function (length, msg) { if (!length || isNaN(length)) throw new Error('".min()" method: Missing or invalid arguments.'); if (length >= 1) { var _msg = msg || "This field must be at least " + length + " " + (length <= 1 ? 'character' : 'characters') + " long."; if (this._value.length < length) this._push(_msg); } return this; }; FieldValidator.prototype.max = function (length, msg) { if (!length || isNaN(length)) throw new Error('".max()" method: Missing or invalid arguments.'); if (length >= 1) { var _msg = msg || "This field must not be more than " + length + " " + (length <= 1 ? 'character' : 'characters') + " long."; if (this._value.length > length) this._push(_msg); } return this; }; FieldValidator.prototype.required = function (msg) { var _msg = msg || 'This field is required.'; if (this._value.length === 0) this._push(_msg); return this; }; FieldValidator.prototype.integer = function (msg) { var _msg = msg || 'The value entered must be an integer.'; var intTest = parseInt(this._value); if (isNaN(intTest)) this._push(_msg); return this; }; FieldValidator.prototype.float = function (msg) { var _msg = msg || 'The value entered must be a floating point number.'; var floatTest = parseFloat(this._value); if (isNaN(floatTest)) this._push(_msg); return this; }; FieldValidator.prototype.email = function (msg, rgx) { var _msg = msg || 'Please enter a valid email address.'; // const emailRegex = // rgx || /^[A-Z0-9._%+-]{1,64}@(?:[A-Z0-9-]{1,63}\.){1,125}[A-Z]{2,63}$/i; var emailRegex = rgx || /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; if (!emailRegex.test(this._value)) this._push(_msg); return this; }; FieldValidator.prototype.regex = function (rgx, msg) { if (!rgx || !rgx.test) throw new Error('".regex()" method: Missing or invalid arguments.'); var _msg = msg || 'The value entered does not match the required regular expression pattern.'; if (!rgx.test(this._value)) this._push(_msg); return this; }; FieldValidator.prototype.equals = function (comparedString, msg) { if (!comparedString || !(typeof comparedString === 'string')) throw new Error('".equals()" method: Missing or invalid arguments.'); var _msg = msg || "The value in this field does not equal \"" + comparedString + "\"."; if (this._value !== comparedString) this._push(_msg); return this; }; FieldValidator.prototype.custom = function (fn) { if (!fn || !(typeof fn === 'function')) throw new Error('".custom()" method: Missing or invalid arguments.'); var returnedError = fn(); if (typeof returnedError === 'string') this._push(returnedError); return this; }; FieldValidator.prototype.end = function () { if (this.errors.length > 0) return this.errors; return null; }; return FieldValidator; }()); /** * @function * Exposes the FieldValidator class which allows you to chain validation methods together and * produce an array of error messages that are attached to the "errors" object inside the * "ProformaBag" of properties and methods. To be used inside the "validationObject" prop * passed to the Proforma component. See README for more detailed examples. * @example * <Proforma * config={{ * {...} * validationObject: { * field_one: (values) => { * return fieldValidator(values.field_one) * .required() * .min(5) * .max(25) * .end(); * }, * email: (values) => { * return fieldValidator(values.email) * .required() * .email('Please enter a valid email address!') * .end(); * } * } * }} * > * {...} * </Proforma> * * @param {string} value - the value to be validatated (e.g. values.name, values.email, values.password, etc.) * @returns {FieldValidator} FieldValidator class instance */ function fieldValidator(value) { return new FieldValidator(value); } exports.fieldValidator = fieldValidator;