UNPKG

json-deep-compare

Version:

A powerful library for comparing JSON objects with support for deep comparison, regex validation, and customizable options

1 lines 1.28 kB
var PathUtils=require("./PathUtils");class RegexValidator{constructor(e,t){this.options=e,this.result=t}validateValue(e,t){if("string"==typeof e)for(var[a,s]of Object.entries(this.options.regexChecks)){var r=!1;if(a===t)r=!0;else if(this.options.matchKeysByName){PathUtils.getKeyNameFromPath(t)===a&&(r=!0)}r&&this._checkRegex(e,t,s)}}_checkRegex(e,t,a){a.test(e)?this.result.addPassedRegexCheck({path:t,value:e,pattern:a.toString()}):this.result.addFailedRegexCheck({path:t,value:e,pattern:a.toString(),message:"Value does not match regex pattern"})}validateAllMatchingKeys(e){var t=this;if(this.options.matchKeysByName){var a=PathUtils.getAllPaths(e),s=function(s){var r=a.filter((e=>PathUtils.getKeyNameFromPath(e)===s));for(var i of r){var o=PathUtils.getValueAtPath(e,i);if("string"==typeof o)t._isPathChecked(i)||(h.test(o)?t.result.addPassedRegexCheck({path:i,value:o,pattern:h.toString(),matchedByName:!0}):t.result.addFailedRegexCheck({path:i,value:o,pattern:h.toString(),message:"Value does not match regex pattern",matchedByName:!0}))}};for(var[r,h]of Object.entries(this.options.regexChecks))s(r)}}_isPathChecked(e){var t=this.result.getResult();return t.regexChecks.passed.some((t=>t.path===e))||t.regexChecks.failed.some((t=>t.path===e))}}module.exports=RegexValidator;