@re-flex/form-validator
Version:
Re-flex UTILS _> form-validator
3 lines (2 loc) • 5.01 kB
JavaScript
!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e||self).formValidator={})}(this,function(e){function n(e,n){e.prototype=Object.create(n.prototype),e.prototype.constructor=e,t(e,n)}function t(e,n){return t=Object.setPrototypeOf||function(e,n){return e.__proto__=n,e},t(e,n)}function r(e,n){(null==n||n>e.length)&&(n=e.length);for(var t=0,r=new Array(n);t<n;t++)r[t]=e[t];return r}var i="Not Valid",o=new Date(""),u=Array.isArray,a=function(e){return new Date(e).getTime()},c=/*#__PURE__*/function(){function e(){var e=this;this.options={nullable:!0,required:!1},this.matcher=[],this.nullable=function(n){return void 0===n&&(n=!0),e.options.nullable=!!n,{validate:e.validate}},this.required=function(n){return void 0===n&&(n=!0),e.options.required=!!n,{validate:e.validate}},this.defaultChain=function(){return{required:e.required,nullable:e.nullable,validate:e.validate}}}return e.prototype.validate=function(e){if(u(this.matcher))for(var n,t=function(e,n){var t="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(t)return(t=t.call(e)).next.bind(t);if(Array.isArray(e)||(t=function(e,n){if(e){if("string"==typeof e)return r(e,n);var t=Object.prototype.toString.call(e).slice(8,-1);return"Object"===t&&e.constructor&&(t=e.constructor.name),"Map"===t||"Set"===t?Array.from(e):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?r(e,n):void 0}}(e))){t&&(e=t);var i=0;return function(){return i>=e.length?{done:!0}:{done:!1,value:e[i++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}(this.matcher);!(n=t()).done;){var o=n.value,a=o.message,c=(0,o.checker)(e);if(console.log("isValid",c,a),console.log("this.options.required",this.options),this.options.required&&void 0===e&&null!==e)return a||i;if(!1===this.options.nullable&&null===e)return a||i;if(!c)return a||i}return null},e}(),s=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/,l=/^[A-Za-z]\w{7,14}$/,f=/^\d{10}$/,d=/^(?:3[47][0-9]{13})$/,h=/[(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/gim,p=/*#__PURE__*/function(e){function t(n){var t;return(t=e.call(this)||this).email=t.BindRegexpHook(s),t.password=t.BindRegexpHook(l),t.phone=t.BindRegexpHook(f),t.creditCard=t.BindRegexpHook(d),t.url=t.BindRegexpHook(h),t.match=t.BindRegexpHook(/.*/gim),t.customRegexp=n,t}return n(t,e),t.prototype.BindRegexpHook=function(e){var n=this;return function(t){return void 0===t&&(t=i),n.matcher.push({message:t,type:"string",checker:function(t){return(n.customRegexp||e).test(t)}}),n.defaultChain()}},t}(c),m=/*#__PURE__*/function(e){function t(n){var t;return(t=e.call(this)||this).BindNumberHook=function(e){return function(n){return void 0===n&&(n=i),t.matcher.push({message:n,type:"number",checker:function(n){return!Number.isNaN(n)&&e(n,t.maxMin)}}),t.defaultChain()}},t.positive=t.BindNumberHook(function(e){return e>0}),t.negative=t.BindNumberHook(function(e){return e<0}),t.max=t.BindNumberHook(function(e,n){return void 0===n&&(n=0),e<=n}),t.min=t.BindNumberHook(function(e,n){return void 0===n&&(n=0),e>=n}),t.float=t.BindNumberHook(function(e){return!String(e).includes(".")}),t.integer=t.BindNumberHook(function(e){return!Number.isInteger(e)}),t.safeInteger=t.BindNumberHook(function(e){return!Number.isSafeInteger(e)}),t.bigInteger=t.BindNumberHook(function(e){return!BigInt(e)===e}),t.maxMin=n,t}return n(t,e),t}(c),v=/*#__PURE__*/function(e){function t(n){var t;return(t=e.call(this)||this).BindDateHook=function(e){return function(n){return void 0===n&&(n=i),t.matcher.push({message:n,type:"date",checker:function(n){return new Date(n)!==o&&e(n,t.maxMin)}}),t.defaultChain()}},t.max=t.BindDateHook(function(e,n){return void 0===n&&(n=new Date),a(e)<=a(n)}),t.min=t.BindDateHook(function(e,n){return void 0===n&&(n=new Date),a(e)>=a(n)}),t.maxMin=n,t}return n(t,e),t}(c),b=/*#__PURE__*/function(e){function t(n){var t;return void 0===n&&(n=i),(t=e.call(this)||this).matcher.push({message:n,type:"boolean",checker:function(e){return["true","false"].includes(String(e))}}),t}return n(t,e),t}(c),g=/*#__PURE__*/function(e){function t(n,t,r){var o;return void 0===r&&(r=i),(o=e.call(this)||this).message=i,o.init=function(){return o.matcher.push({message:o.message,type:"object",checker:function(e){var n={};for(var t in o.rules)if(Object.prototype.hasOwnProperty.call(e,t)){var r=o.rules[t].validate(e[t]);n[t]=r}return n}}),function(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}(o)},o.rules=n,o.each=t,o.message=r,o}return n(t,e),t}(c);e.boolean=function(e){return new b(e)},e.date=function(){return new v},e.number=function(e){return new m(e)},e.object=function(e,n,t){return void 0===n&&(n=i),new g(e,t,n).init()},e.string=function(){return new p}});
//# sourceMappingURL=form-validator.umd.js.map