Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-form-validator-component

Package Overview
Dependencies
Maintainers
3
Versions
58
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-form-validator-component - npm Package Compare versions

Comparing version 1.4.4 to 1.5.0

2

build/lib/index.js

@@ -1,1 +0,1 @@

!function(e,t){for(var r in t)e[r]=t[r]}(exports,function(e){var t={};function r(a){if(t[a])return t[a].exports;var n=t[a]={i:a,l:!1,exports:{}};return e[a].call(n.exports,n,n.exports,r),n.l=!0,n.exports}return r.m=e,r.c=t,r.d=function(e,t,a){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:a})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var a=Object.create(null);if(r.r(a),Object.defineProperty(a,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var n in e)r.d(a,n,function(t){return e[t]}.bind(null,n));return a},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=6)}([function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var a=/^(([^<>()[\]\\.,;:\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,}))$/,n=/^([a-zA-Z0-9_\-.]+)@([a-zA-Z0-9_\-.]+)\.([a-zA-Z]{2,5}){1,25}(,[ ]{0,1}([a-zA-Z0-9_\-.]+)@([a-zA-Z0-9_\-.]+)\.([a-zA-Z]{2,5}){1,25})*$/,o=/^[a-zA-Z]+(([',. -][a-zA-Z ])?[a-zA-Z]*)*$/,i=/^[+]?(\d{1,3})\s?(\d{10})$/,u=/^(([A-Za-z0-9]{2,4})\s?([A-Za-z0-9]{1,5})?)?$/,l=/^(\d{2}(\s|[-])\d{2}(\s|[-])\d{2}$)|\d{6}|^$/,s=/^((([A-Za-z]{6}[0-9A-Za-z]{2})|([A-Za-z]{4}(\s|-)?[A-Za-z]{2}(\s|-)?[0-9A-Za-z]{2}))[0-9A-Za-z]{3}?$)|^$/,d=/^([0-9A-Za-z]{4}\s?){4,6}([0-9A-Za-z]{1,4}|[0-9A-Za-z]{4}\s?[0-9A-Za-z]{1|4})?$/,c=/^\d{8,15}$/,f=/^[A-Z0-9a-z\-/\s]+$/i,p=/^-?\d*[.]?\d+$/,v=/^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([-.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/g,h={validator:function(e){return!!e.match(o)},error:"Please provide a full name"},m={validator:function(e){return!!e.match(a)},error:"Please provide a valid email address"},y={validator:function(e){return!!e.match(n)},error:"Please provide a valid email address, or several emails comma delimited"},b={validator:function(e){return!!e.match(i)},error:"Please provide a valid UK phone number"},O={validator:function(e){return!!e.match(u)},error:"Please provide a valid UK postcode"},g={validator:function(e){return!!e.match(l)},error:"Please provide a UK Sort Code"},A={validator:function(e){return!!e.match(s)},error:"Please provide a valid Swift Code"},P={validator:function(e){return!!e.match(d)},error:"Please provide a valid Swift Code"},_={validator:function(e){return!!e.match(c)},error:"Please provide a valid UK VAT Number"},j={validator:function(e){return!!e.match(f)},error:"Please provide an alphanumeric input"},S={validator:function(e){return"number"==typeof e||!!e.match(p)},error:"Please provide an numeric input"},z={validator:function(e){return!!e.match(v)},error:"Please provide a valid URL"},w={validator:function(e,t){return e.length===t},error:"Please provide an input of exactly "+length+" characters"},Z={validator:function(e,t){return e.length<=t},error:"The maximum length of this input is "+length+" characters"},E={validator:function(e,t){return e.length>=t},error:"The minimum length of this input is "+length+" characters"};t.isRequired={validator:function(e){return!!e},error:"Please provide a value"},t.isEmail=m,t.isEmailArray=y,t.isFullName=h,t.isPhoneNumber=b,t.isPostCode=O,t.isSortCode=g,t.isSwiftCode=A,t.isIban=P,t.isVatNumber=_,t.isAlphaNumeric=j,t.isNumeric=S,t.isUrl=z,t.isLength=w,t.maxLength=Z,t.minLength=E},function(e,t,r){"use strict";e.exports="SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED"},function(e,t,r){"use strict";var a=r(1);function n(){}e.exports=function(){function e(e,t,r,n,o,i){if(i!==a){var u=new Error("Calling PropTypes validators directly is not supported by the `prop-types` package. Use PropTypes.checkPropTypes() to call them. Read more at http://fb.me/use-check-prop-types");throw u.name="Invariant Violation",u}}function t(){return e}e.isRequired=e;var r={array:e,bool:e,func:e,number:e,object:e,string:e,symbol:e,any:e,arrayOf:t,element:e,instanceOf:t,node:e,objectOf:t,oneOf:t,oneOfType:t,shape:t,exact:t};return r.checkPropTypes=n,r.PropTypes=r,r}},function(e,t,r){e.exports=r(2)()},function(e,t){e.exports=require("react")},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var a=function(){return function(e,t){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return function(e,t){var r=[],a=!0,n=!1,o=void 0;try{for(var i,u=e[Symbol.iterator]();!(a=(i=u.next()).done)&&(r.push(i.value),!t||r.length!==t);a=!0);}catch(e){n=!0,o=e}finally{try{!a&&u.return&&u.return()}finally{if(n)throw o}}return r}(e,t);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),n=function(){function e(e,t){for(var r=0;r<t.length;r++){var a=t[r];a.enumerable=a.enumerable||!1,a.configurable=!0,"value"in a&&(a.writable=!0),Object.defineProperty(e,a.key,a)}}return function(t,r,a){return r&&e(t.prototype,r),a&&e(t,a),t}}(),o=l(r(4)),i=l(r(3)),u=function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t.default=e,t}(r(0));function l(e){return e&&e.__esModule?e:{default:e}}function s(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t<e.length;t++)r[t]=e[t];return r}return Array.from(e)}function d(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}var c=function(e){function t(e){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,t);var r=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));return r.onValidate=function(e,t){r.props.parent.setState(d({},e,t))},r.toArray=function(e){return Object.entries(e).reduce(function(e,t){var r=a(t,2),n=r[0],o=r[1];return e.concat({key:n,value:o})},[])},r.addToStateProperty=function(e,t){r.setState(d({},e,Object.assign(r.state[e],t)))},r.removeError=function(e,t){var a=r.state.errors[e],n=a.indexOf(t);n>-1&&a.splice(n,1),r.addToStateProperty("errors",d({},e,a))},r.removeAllErrors=function(e){r.setState({errors:Object.assign(r.state.errors,d({},e,[]))})},r.updateErrorsForField=function(e,t,a){e?r.removeError(t,a):r.addToStateProperty("errors",d({},t,[].concat(s(new Set([].concat(s(r.state.errors[t]||[]),[a]))))))},r.validateRules=function(e,t,a){return a.reduce(function(a,n){var o=u[n]||n,i=o.validator(t);return r.updateErrorsForField(i,e,o.error),a&&i},!0)},r.validateField=function(e,t){if(!r.props.fields[e].required&&0===t.length)return r.setState({validation:Object.assign(r.state.validation,d({},e,!0))}),!0;var a=r.state.fields[e].rules,n=r.validateRules(e,t,a);return r.setState({validation:Object.assign(r.state.validation,d({},e,n))}),n},r.checkGroupValid=function(e,t,a){return e.some(function(e){return!!e.rules.reduce(function(e,n){var o=u[n]||n,i=o.validator(a);return r.updateErrorsForField(i,t,o.error),e&&i},!0)})},r.validateGroup=function(e,t){var a=r.state.fields[e],n=Object.values(r.state.fields).filter(function(e){return e.required===a.required});r.setState({validation:Object.assign(r.state.validation,d({},a.required,!1))});var o=r.checkGroupValid(n,e,t);return o&&r.setState({validation:Object.assign(r.state.validation,d({},a.required,!0))}),o},r.validateFormAndUpdateState=function(){Object.values(r.props.fields).map(function(e){return e.name}).forEach(function(e){var t=document.getElementsByName(e)[0]&&document.getElementsByName(e)[0].value?document.getElementsByName(e)[0].value:"";r.validateFieldAndUpdateState(e,t)})},r.onChange=function(e){r.validateFieldAndUpdateState(e.target.name,e.target.value)},r.state={fields:e.fields,errors:Object.keys(e.fields).reduce(function(e,t){return e[t]=[],e},{}),validation:Object.keys(e.fields).reduce(function(t,r){var a=e.fields[r];return a.required&&"string"==typeof a.required?(t[a.required]=!1,t):(t[r]=!(a.rules&&a.rules.length>0),t)},{}),isFormValid:!1},r}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}(t,o.default.Component),n(t,[{key:"validateFieldAndUpdateState",value:function(e,t){var r=this.props.fields[e].onValidate||this.props.onValidate||this.onValidate;"string"==typeof this.props.fields[e].required?this.validateGroup(e,t)?r(e,t):r(e,null):this.validateField(e,t)?r(e,t):r(e,null),this.setState({isFormValid:Object.values(this.state.validation).every(function(e){return e})})}},{key:"componentDidMount",value:function(){var e=this;this.validateFormAndUpdateState(),this.props.validateOnLoad&&Object.values(this.props.fields).map(function(t){return e.removeAllErrors(t.name)})}},{key:"render",value:function(){var e=this.state,t=e.fields,r=e.errors,a=e.isFormValid,n=e.validation;return this.props.children({isFormValid:a,isFieldValid:n,fields:this.toArray(t),onChange:this.onChange,errors:r})}}]),t}();t.default=c,c.propTypes={parent:i.default.object,children:i.default.func,onValidate:i.default.func,fields:i.default.object,validateOnLoad:i.default.bool},c.defaultProps={validateOnLoad:!0}},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.Validator=void 0;var a=function(e){return e&&e.__esModule?e:{default:e}}(r(5));t.Validator=a.default}]));
!function(e,t){for(var r in t)e[r]=t[r]}(exports,function(e){var t={};function r(n){if(t[n])return t[n].exports;var a=t[n]={i:n,l:!1,exports:{}};return e[n].call(a.exports,a,a.exports,r),a.l=!0,a.exports}return r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var a in e)r.d(n,a,function(t){return e[t]}.bind(null,a));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=6)}([function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=/^(([^<>()[\]\\.,;:\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,}))$/,a=/^([a-zA-Z0-9_\-.]+)@([a-zA-Z0-9_\-.]+)\.([a-zA-Z]{2,5}){1,25}(,[ ]{0,1}([a-zA-Z0-9_\-.]+)@([a-zA-Z0-9_\-.]+)\.([a-zA-Z]{2,5}){1,25})*$/,o=/^[a-zA-Z]+(([',. -][a-zA-Z ])?[a-zA-Z]*)*$/,i=/^[+]?(\d{1,3})\s?(\d{10})$/,u=/^(([A-Za-z0-9]{2,4})\s?([A-Za-z0-9]{1,5})?)?$/,l=/^(\d{2}(\s|[-])\d{2}(\s|[-])\d{2}$)|\d{6}|^$/,s=/^((([A-Za-z]{6}[0-9A-Za-z]{2})|([A-Za-z]{4}(\s|-)?[A-Za-z]{2}(\s|-)?[0-9A-Za-z]{2}))[0-9A-Za-z]{3}?$)|^$/,d=/^([0-9A-Za-z]{4}\s?){4,6}([0-9A-Za-z]{1,4}|[0-9A-Za-z]{4}\s?[0-9A-Za-z]{1|4})?$/,c=/^\d{8,15}$/,f=/^[A-Z0-9a-z\-/\s]+$/i,p=/^-?\d*[.]?\d+$/,v=/^(http:\/\/www\.|https:\/\/www\.|http:\/\/|https:\/\/)?[a-z0-9]+([-.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/g,m={validator:function(e){return!!e.match(o)},error:"Please provide a full name"},y={validator:function(e){return!!e.match(n)},error:"Please provide a valid email address"},h={validator:function(e){return!!e.match(a)},error:"Please provide a valid email address, or several emails comma delimited"},b={validator:function(e){return!!e.match(i)},error:"Please provide a valid UK phone number"},g={validator:function(e){return!!e.match(u)},error:"Please provide a valid UK postcode"},O={validator:function(e){return!!e.match(l)},error:"Please provide a UK Sort Code"},A={validator:function(e){return!!e.match(s)},error:"Please provide a valid Swift Code"},j={validator:function(e){return!!e.match(d)},error:"Please provide a valid Swift Code"},P={validator:function(e){return!!e.match(c)},error:"Please provide a valid UK VAT Number"},S={validator:function(e){return!!e.match(f)},error:"Please provide an alphanumeric input"},_={validator:function(e){return"number"==typeof e||!!e.match(p)},error:"Please provide an numeric input"},z={validator:function(e){return!!e.match(v)},error:"Please provide a valid URL"},w={validator:function(e,t){return e.length===t},error:"Please provide an input of exactly "+length+" characters"},Z={validator:function(e,t){return e.length<=t},error:"The maximum length of this input is "+length+" characters"},V={validator:function(e,t){return e.length>=t},error:"The minimum length of this input is "+length+" characters"};t.isRequired={validator:function(e){return!!e},error:"Please provide a value"},t.isEmail=y,t.isEmailArray=h,t.isFullName=m,t.isPhoneNumber=b,t.isPostCode=g,t.isSortCode=O,t.isSwiftCode=A,t.isIban=j,t.isVatNumber=P,t.isAlphaNumeric=S,t.isNumeric=_,t.isUrl=z,t.isLength=w,t.maxLength=Z,t.minLength=V},function(e,t,r){"use strict";e.exports="SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED"},function(e,t,r){"use strict";var n=r(1);function a(){}e.exports=function(){function e(e,t,r,a,o,i){if(i!==n){var u=new Error("Calling PropTypes validators directly is not supported by the `prop-types` package. Use PropTypes.checkPropTypes() to call them. Read more at http://fb.me/use-check-prop-types");throw u.name="Invariant Violation",u}}function t(){return e}e.isRequired=e;var r={array:e,bool:e,func:e,number:e,object:e,string:e,symbol:e,any:e,arrayOf:t,element:e,instanceOf:t,node:e,objectOf:t,oneOf:t,oneOfType:t,shape:t,exact:t};return r.checkPropTypes=a,r.PropTypes=r,r}},function(e,t,r){e.exports=r(2)()},function(e,t){e.exports=require("react")},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},a=function(){return function(e,t){if(Array.isArray(e))return e;if(Symbol.iterator in Object(e))return function(e,t){var r=[],n=!0,a=!1,o=void 0;try{for(var i,u=e[Symbol.iterator]();!(n=(i=u.next()).done)&&(r.push(i.value),!t||r.length!==t);n=!0);}catch(e){a=!0,o=e}finally{try{!n&&u.return&&u.return()}finally{if(a)throw o}}return r}(e,t);throw new TypeError("Invalid attempt to destructure non-iterable instance")}}(),o=function(){function e(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,r,n){return r&&e(t.prototype,r),n&&e(t,n),t}}(),i=s(r(4)),u=s(r(3)),l=function(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t.default=e,t}(r(0));function s(e){return e&&e.__esModule?e:{default:e}}function d(e){if(Array.isArray(e)){for(var t=0,r=Array(e.length);t<e.length;t++)r[t]=e[t];return r}return Array.from(e)}function c(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}var f=function(e){function t(e){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,t);var r=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}(this,(t.__proto__||Object.getPrototypeOf(t)).call(this,e));return r.onValidate=function(e,t){r.props.parent.setState(c({},e,t))},r.toArray=function(e){return Object.entries(e).reduce(function(e,t){var r=a(t,2),n=r[0],o=r[1];return e.concat({key:n,value:o})},[])},r.addToStateProperty=function(e,t){r.setState(c({},e,Object.assign(r.state[e],t)))},r.removeError=function(e,t){var n=r.state.errors[e],a=n.indexOf(t);a>-1&&n.splice(a,1),r.addToStateProperty("errors",c({},e,n))},r.removeAllErrors=function(e){r.setState({errors:Object.assign(r.state.errors,c({},e,[]))})},r.updateErrorsForField=function(e,t,n){e?r.removeError(t,n):r.addToStateProperty("errors",c({},t,[].concat(d(new Set([].concat(d(r.state.errors[t]||[]),[n]))))))},r.validateRules=function(e,t,n){return n.reduce(function(n,a){var o=l[a]||a,i=o.validator(t);return r.updateErrorsForField(i,e,o.error),n&&i},!0)},r.validateGroup=function(e,t,n){if(Object.entries(r.state.groupValidation[n]).filter(function(t){return!t.includes(e)}).some(function(e){return e.includes(!0)})){var a=r.state.fields[e].rules,o=r.validateRules(e,t,a);return r.setState({groupValidation:Object.assign({},r.state.groupValidation[n],c({},e,o))}),!0}var i=r.state.fields[e].rules,u=r.validateRules(e,t,i),l=r.state.groupValidation;return l[n]=Object.assign({},l[n],c({},e,u)),r.setState({groupValidation:Object.assign({},r.state.groupValidation,l)},function(){return r.setState({validation:Object.assign(r.state.validation,c({},n,Object.values(r.state.groupValidation[n]).some(function(e){return!0===e})))})}),u},r.validateField=function(e,t){var a=r.props.fields[e].required&&n(r.props.fields[e].required)?r.props.fields[e].required:void 0;if(!a&&0===t.length)return r.setState({validation:Object.assign(r.state.validation,c({},e,!0))},function(){return r.removeAllErrors(e)}),!0;if(a)return r.validateGroup(e,t,a);var o=r.state.fields[e].rules,i=r.validateRules(e,t,o);return r.setState({validation:Object.assign(r.state.validation,c({},e,i))}),i},r.validateFormAndUpdateState=function(){Object.values(r.props.fields).map(function(e){return e.name}).forEach(function(e){var t=document.getElementsByName(e)[0]&&document.getElementsByName(e)[0].value?document.getElementsByName(e)[0].value:"";r.validateFieldAndUpdateState(e,t)})},r.onChange=function(e){r.validateFieldAndUpdateState(e.target.name,e.target.value)},r.state={fields:e.fields,errors:Object.keys(e.fields).reduce(function(e,t){return e[t]=[],e},{}),groupValidation:Object.keys(e.fields).reduce(function(t,r){var n=e.fields[r];return n.required&&"string"==typeof n.required?(t[n.required]=Object.assign({},t[n.required],c({},r,!(n.rules&&n.rules.length>0||n.required))),t):t},{}),validation:Object.keys(e.fields).reduce(function(t,r){var n=e.fields[r];return n.required&&"string"==typeof n.required?(t[n.required]=!1,t):(t[r]=!(n.rules&&n.rules.length>0||n.required),t)},{}),isFormValid:!1},r}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}(t,i.default.Component),o(t,[{key:"validateFieldAndUpdateState",value:function(e,t){var r=this.props.fields[e].onValidate||this.props.onValidate||this.onValidate;this.validateField(e,t)?r(e,t):r(e,null),this.setState({isFormValid:Object.values(this.state.validation).every(function(e){return e})})}},{key:"componentDidMount",value:function(){var e=this;this.validateFormAndUpdateState(),this.props.validateOnLoad&&Object.values(this.props.fields).map(function(t){return e.removeAllErrors(t.name)})}},{key:"render",value:function(){var e=this.state,t=e.fields,r=e.errors,n=e.isFormValid,a=e.validation;return this.props.children({isFormValid:n,isFieldValid:a,fields:this.toArray(t),onChange:this.onChange,errors:r})}}]),t}();t.default=f,f.propTypes={parent:u.default.object,children:u.default.func,onValidate:u.default.func,fields:u.default.object,validateOnLoad:u.default.bool},f.defaultProps={validateOnLoad:!0}},function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0}),t.Validator=void 0;var n=function(e){return e&&e.__esModule?e:{default:e}}(r(5));t.Validator=n.default}]));
{
"name": "react-form-validator-component",
"version": "1.4.4",
"version": "1.5.0",
"main": "build/lib/index.js",

@@ -5,0 +5,0 @@ "repository": "git@github.com:JDLT-Ltd/react-form-validator-component.git",

@@ -25,2 +25,4 @@ # React Form Validator

* [**2.4 - Arguments in RFVC**](#arguments)
* [**2.5 - Other options**](#other-options)
* [**2.5.1 - Group Validation**](#group-validation)

@@ -204,1 +206,27 @@

These can be displayed as a group or be mapped in order to produce individual error labels.
### Other Options
#### Group Validation
RFVC supports group validation, where only one member of a group needs to pass it's validation in order for the whole group to be validated.
In order to use group validation, simply replace the value of the required key on fields with the groupname.
i.e.
```javascript
fields = {
emailAddresses: {
name: 'emailAddresses',
rules: ['isEmailArray'],
required: 'test',
label: 'Email addresses'
},
something: {
name: 'something',
rules: ['isPhoneNumber'],
required: 'test',
label: 'Something'
}
}
```

@@ -27,2 +27,3 @@ import React from 'react'

rules: ['isEmailArray'],
required: 'test',
label: 'Email addresses'

@@ -33,4 +34,16 @@ },

rules: ['isPhoneNumber'],
required: true,
required: 'test',
label: 'Something'
},
number: {
name: 'number',
rules: ['isNumeric'],
required: 'group2',
label: 'number'
},
name: {
name: 'name',
rules: ['isFullName'],
required: 'group2',
label: 'name'
}

@@ -68,2 +81,12 @@ }

</Form.Field>
<Form.Field>
<label>Group 2 Number</label>
<Input name="number" onChange={onChange} />
{this.renderErrors(errors.number)}
</Form.Field>
<Form.Field>
<label>Group 2 Name</label>
<Input name="name" onChange={onChange} />
{this.renderErrors(errors.name)}
</Form.Field>
{<span>Form is {isFormValid ? 'valid' : 'not valid'}</span>}

@@ -76,3 +99,3 @@ <hr />

</Validator>
<Header as={'h2'}>Basic Validation using fields to map (and no semantic-ui)</Header>
{/* <Header as={'h2'}>Basic Validation using fields to map (and no semantic-ui)</Header>
<Validator fields={this.fields} parent={this}>

@@ -186,3 +209,3 @@ {({ isFormValid, fields, onChange, errors }) => {

}}
</Validator>
</Validator> */}
</Container>

@@ -189,0 +212,0 @@ )

@@ -15,5 +15,16 @@ import React from 'react'

}, {}),
groupValidation: Object.keys(props.fields).reduce((groupValidation, currentField) => {
const fieldValue = props.fields[currentField]
if (fieldValue.required && typeof fieldValue.required === 'string') {
groupValidation[fieldValue.required] = Object.assign({}, groupValidation[fieldValue.required], {
[currentField]: (fieldValue.rules && fieldValue.rules.length > 0) || fieldValue.required ? false : true
})
return groupValidation
}
return groupValidation
}, {}),
validation: Object.keys(props.fields).reduce((accumulator, currentValue) => {
//check for groups before adding fields to validation
const fieldValue = props.fields[currentValue]
//if field is a member of a group, add that group to validation and add the field to validation.groupValidation
if (fieldValue.required && typeof fieldValue.required === 'string') {

@@ -23,3 +34,4 @@ accumulator[fieldValue.required] = false

} else {
accumulator[currentValue] = fieldValue.rules && fieldValue.rules.length > 0 ? false : true
accumulator[currentValue] =
(fieldValue.rules && fieldValue.rules.length > 0) || fieldValue.required ? false : true
return accumulator

@@ -82,40 +94,67 @@ }

validateField = (fieldName, fieldValue) => {
if (!this.props.fields[fieldName].required && fieldValue.length === 0) {
validateGroup = (fieldName, fieldValue, groupName) => {
// check if any other member of the group is valid
if (
Object.entries(this.state.groupValidation[groupName])
.filter(field => !field.includes(fieldName))
.some(member => member.includes(true))
) {
const fieldRules = this.state.fields[fieldName].rules
const isFieldValid = this.validateRules(fieldName, fieldValue, fieldRules)
this.setState({
validation: Object.assign(this.state.validation, { [fieldName]: true })
groupValidation: Object.assign({}, this.state.groupValidation[groupName], { [fieldName]: isFieldValid })
})
return true
}
// check if this field is valid
const fieldRules = this.state.fields[fieldName].rules
const isFieldValid = this.validateRules(fieldName, fieldValue, fieldRules)
this.setState({
validation: Object.assign(this.state.validation, { [fieldName]: isFieldValid })
})
return isFieldValid
}
const newGroupValidation = this.state.groupValidation
checkGroupValid = (group, fieldName, fieldValue) =>
group.some(
field =>
!!field.rules.reduce((accumulator, fieldRule) => {
const rule = defaultRules[fieldRule] || fieldRule
const validation = rule.validator(fieldValue)
newGroupValidation[groupName] = Object.assign({}, newGroupValidation[groupName], { [fieldName]: isFieldValid })
this.updateErrorsForField(validation, fieldName, rule.error)
return accumulator && validation
}, true)
this.setState(
{
groupValidation: Object.assign({}, this.state.groupValidation, newGroupValidation)
},
() =>
this.setState({
validation: Object.assign(this.state.validation, {
[groupName]: Object.values(this.state.groupValidation[groupName]).some(member => member === true)
})
})
)
return isFieldValid
}
validateGroup = (fieldName, fieldValue) => {
const currentField = this.state.fields[fieldName]
const group = Object.values(this.state.fields).filter(field => field.required === currentField.required)
validateField = (fieldName, fieldValue) => {
// Check if field is in a group
const groupName =
this.props.fields[fieldName].required && typeof this.props.fields[fieldName].required
? this.props.fields[fieldName].required
: undefined
// ensure that empty non-required fields pass validation and don't throw errors
if (!groupName && fieldValue.length === 0) {
this.setState(
{
validation: Object.assign(this.state.validation, { [fieldName]: true })
},
() => this.removeAllErrors(fieldName)
)
return true
}
this.setState({ validation: Object.assign(this.state.validation, { [currentField.required]: false }) })
if (groupName) {
return this.validateGroup(fieldName, fieldValue, groupName)
}
const isGroupValid = this.checkGroupValid(group, fieldName, fieldValue)
if (isGroupValid)
this.setState({ validation: Object.assign(this.state.validation, { [currentField.required]: true }) })
return isGroupValid
// standard validation
const fieldRules = this.state.fields[fieldName].rules
const isFieldValid = this.validateRules(fieldName, fieldValue, fieldRules)
this.setState({
validation: Object.assign(this.state.validation, { [fieldName]: isFieldValid })
})
return isFieldValid
}

@@ -126,15 +165,6 @@

// if this field is a member of a group
if (typeof this.props.fields[fieldName].required === 'string') {
if (this.validateGroup(fieldName, fieldValue)) {
onValidate(fieldName, fieldValue)
} else {
onValidate(fieldName, null)
}
if (this.validateField(fieldName, fieldValue)) {
onValidate(fieldName, fieldValue)
} else {
if (this.validateField(fieldName, fieldValue)) {
onValidate(fieldName, fieldValue)
} else {
onValidate(fieldName, null)
}
onValidate(fieldName, null)
}

@@ -141,0 +171,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc