UNPKG

valedictorian

Version:

Another React validation library

249 lines (193 loc) 6.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; exports.required = required; exports.equal = equal; exports.notEqual = notEqual; exports.evaluate = evaluate; exports.format = format; exports.length = length; exports.min = min; exports.max = max; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var defaultOptions = { if: function _if() { return true; }, unless: function unless() { return false; } }; function validate(body) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; return function (obj) { options = Object.assign({}, defaultOptions, options); if (!options.if() || options.unless()) { return obj; } // Clean up the value, as pretty much every validator expects a trimmed input obj = Object.assign({}, obj, { value: (obj.value + "").trim() }); return body(obj); }; } function required() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; options = Object.assign({}, { message: "is required" }, options); return validate(function (obj) { if (obj.value === "") { obj.valid = false; obj.errors.push(options.message); } return obj; }, options); } function equal(value) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; options = Object.assign({}, { message: "is not equal" }, options); return validate(function (obj) { if (obj.value !== value.trim()) { obj.valid = false; obj.errors.push(options.message); } return obj; }, options); } function notEqual(value) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; options = Object.assign({}, { message: "is equal" }, options); return validate(function (obj) { if (obj.value === value.trim()) { obj.valid = false; obj.errors.push(options.message); } return obj; }, options); } function evaluate(func) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; options = Object.assign({}, { message: "is not valid" }, options); return validate(function (obj) { if (!func(obj.value)) { obj.valid = false; obj.errors.push(options.message); } return obj; }, options); } function format(regexp) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; options = Object.assign({}, { allowEmpty: false, message: "is not valid" }, options); return validate(function (obj) { if (options.allowEmpty === true && obj.value === "") { return obj; } if (!obj.value.match(regexp)) { obj.valid = false; obj.errors.push(options.message); } return obj; }, options); } function length(len) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var min = null; var max = null; if ((typeof len === "undefined" ? "undefined" : _typeof(len)) === "object") { min = len.min; max = len.max; } else { max = len; } options = Object.assign({}, { minMessage: "is too short (Min is ${min})", maxMessage: "is too long (Max is ${max})" }, options); return validate(function (obj) { if (max && obj.value.length > max) { obj.valid = false; obj.errors.push(options.maxMessage.replace(/\${len}|\${max}/, max)); } else if (min && obj.value.length < min) { obj.valid = false; obj.errors.push(options.minMessage.replace(/\${min}/, min)); } return obj; }, options); } function min(minVal) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; options = Object.assign({}, { allowEmpty: false, message: "Minimum ${min}" }, options); return validate(function (obj) { if (options.allowEmpty === true && obj.value === "") { return obj; } var float = parseFloat(obj.value); if (isNaN(float) || float < minVal) { obj.valid = false; obj.errors.push(options.message.replace(/\${min}/, minVal)); } return obj; }, options); } function max(maxVal) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; options = Object.assign({}, { allowEmpty: false, message: "Maximum ${max}" }, options); return validate(function (obj) { if (options.allowEmpty === true && obj.value === "") { return obj; } var float = parseFloat(obj.value); if (isNaN(float) || float > maxVal) { obj.valid = false; obj.errors.push(options.message.replace(/\${max}/, maxVal)); } return obj; }, options); } var Validator = function () { function Validator(validators) { _classCallCheck(this, Validator); this.validators = validators; } _createClass(Validator, [{ key: "validate", value: function validate(obj) { obj = Object.assign({}, { beenValid: false }, obj, { valid: true, errors: [] }); var result = this.validators.reduce(function (obj, validator) { return validator(obj); }, obj); if (result.valid) { result.beenValid = true; } return result; } }]); return Validator; }(); exports.default = Validator;