UNPKG

mockingjar-lib

Version:

A TypeScript library for AI-powered JSON mock data generation using schema-based configuration

1 lines 5.93 kB
"use strict";function getFieldValue(e,r){if(!e||!r)return;const t=r.split(".");let a=e;for(const e of t){if(null==a)return;const r=e.match(/^(.+)\[(\d+)\]$/);if(r){const[,e,t]=r;if("object"!=typeof a||null===a||Array.isArray(a))return;if(a=a[e],!Array.isArray(a))return;{const e=parseInt(t);a=e<a.length?a[e]:void 0}}else{if("object"!=typeof a||null===a||Array.isArray(a))return;a=a[e]}}return a}function hasField(e,r){if(!e||!r)return!1;const t=r.split(".");let a=e;for(const e of t){if(null==a)return!1;const r=e.match(/^(.+)\[(\d+)\]$/);if(r){const[,e,t]=r;if("object"!=typeof a||null===a||Array.isArray(a))return!1;{if(!(e in a))return!1;if(a=a[e],!Array.isArray(a))return!1;const r=parseInt(t);if(r>=a.length)return!1;a=a[r]}}else{if("object"!=typeof a||null===a||Array.isArray(a))return!1;if(!(e in a))return!1;a=a[e]}}return!0}function jsonValidator(e,r){const t=[];for(const a of r.fields)validateField(e,a,"",t);return checkUnidentifiedFields(e,r.fields,"",t),t}function validateField(e,r,t,a){const n=t?`${t}.${r.name}`:r.name,i=getFieldValue(e,n),l=hasField(e,n);if(!(r.logic?.required||"object"===r.type&&r.children&&r.children.length>0||"array"===r.type&&r.arrayItemType)||l){if(l){if(validateFieldType(i,r,n,t,a),"object"===r.type&&r.children&&i&&"object"==typeof i)for(const t of r.children)validateField(e,t,n,a);"array"===r.type&&r.arrayItemType&&Array.isArray(i)&&validateArrayField(e,r,n,t,a)}}else a.push({parent:t||null,affectedField:n,reason:"missing required field",structure:r})}function validateFieldType(e,r,t,a,n){switch(r.type){case"text":if("string"!=typeof e)return void n.push({parent:a||null,affectedField:t,reason:"malformed type",structure:r});break;case"number":if("number"!=typeof e)return void n.push({parent:a||null,affectedField:t,reason:"malformed type",structure:r});break;case"boolean":if("boolean"!=typeof e)return void n.push({parent:a||null,affectedField:t,reason:"malformed type",structure:r});break;case"email":if("string"!=typeof e||!e.includes("@"))return void n.push({parent:a||null,affectedField:t,reason:"malformed type",structure:r});break;case"url":if("string"!=typeof e||!e.startsWith("http"))return void n.push({parent:a||null,affectedField:t,reason:"malformed type",structure:r});break;case"date":if("string"!=typeof e||isNaN(Date.parse(e)))return void n.push({parent:a||null,affectedField:t,reason:"malformed type",structure:r});break;case"array":if(!Array.isArray(e))return void n.push({parent:a||null,affectedField:t,reason:"malformed type",structure:r});break;case"object":if("object"!=typeof e||null===e||Array.isArray(e))return void n.push({parent:a||null,affectedField:t,reason:"malformed type",structure:r})}r.logic&&validateFieldConstraints(e,r,t,a,n)}function validateFieldConstraints(e,r,t,a,n){const i=r.logic;"text"!==r.type&&"email"!==r.type&&"url"!==r.type||"string"!=typeof e||(i.enum&&!i.enum.includes(e)&&n.push({parent:a||null,affectedField:t,reason:"value not allowed",structure:r}),i.pattern&&!new RegExp(i.pattern).test(e)&&n.push({parent:a||null,affectedField:t,reason:"pattern mismatch",structure:r}),i.minLength&&e.length<i.minLength&&n.push({parent:a||null,affectedField:t,reason:`string too short (minimum ${i.minLength} characters)`,structure:r}),i.maxLength&&e.length>i.maxLength&&n.push({parent:a||null,affectedField:t,reason:`string too long (maximum ${i.maxLength} characters)`,structure:r})),"number"===r.type&&"number"==typeof e&&(i.enum&&!i.enum.map(String).includes(String(e))&&n.push({parent:a||null,affectedField:t,reason:"value not allowed",structure:r}),void 0!==i.min&&e<i.min&&n.push({parent:a||null,affectedField:t,reason:`number below minimum (${i.min})`,structure:r}),void 0!==i.max&&e>i.max&&n.push({parent:a||null,affectedField:t,reason:`number above maximum (${i.max})`,structure:r})),"array"===r.type&&Array.isArray(e)&&(i.minItems&&e.length<i.minItems&&n.push({parent:a||null,affectedField:t,reason:`array too short: minimum ${i.minItems} items required`,structure:r}),i.maxItems&&e.length>i.maxItems&&n.push({parent:a||null,affectedField:t,reason:`array too long: maximum ${i.maxItems} items allowed`,structure:r}))}function validateArrayField(e,r,t,a,n){const i=getFieldValue(e,t);if(!Array.isArray(i)||!r.arrayItemType)return;const l=n.length;for(let a=0;a<i.length;a++){const l=`${t}[${a}]`,s=i[a];if(validateFieldType(s,r.arrayItemType,l,t,n),"object"===r.arrayItemType.type&&r.arrayItemType.children&&"object"==typeof s&&null!==s&&!Array.isArray(s)){const t=s;for(const a of r.arrayItemType.children){const r=`${l}.${a.name}`,i=t[a.name],s=a.name in t;if(!a.logic?.required&&"object"!==a.type&&"array"!==a.type||s){if(s){if(validateFieldType(i,a,r,l,n),"object"===a.type&&a.children&&"object"==typeof i&&null!==i&&!Array.isArray(i)){const e=i;for(const t of a.children){const a=`${r}.${t.name}`,i=e[t.name],l=t.name in e;!t.logic?.required&&"object"!==t.type&&"array"!==t.type||l?l&&validateFieldType(i,t,a,r,n):n.push({parent:r,affectedField:a,reason:"missing required field",structure:t})}}"array"===a.type&&a.arrayItemType&&Array.isArray(i)&&validateArrayField(e,a,r,l,n)}}else n.push({parent:l,affectedField:r,reason:"missing required field",structure:a})}}"array"===r.arrayItemType.type&&r.arrayItemType.arrayItemType&&Array.isArray(s)&&validateArrayField(e,r.arrayItemType,l,t,n)}n.length>l&&n.push({parent:a||null,affectedField:t,reason:"malformed type",structure:r})}function checkUnidentifiedFields(e,r,t,a){if(!e||"object"!=typeof e||Array.isArray(e))return;const n=r.map(e=>e.name);for(const i in e)if(e.hasOwnProperty(i)){const l=t?`${t}.${i}`:i;if(n.includes(i)){const t=r.find(e=>e.name===i),n=e[i];"object"===t?.type&&t.children&&"object"==typeof n&&null!==n&&!Array.isArray(n)&&checkUnidentifiedFields(n,t.children,l,a)}else a.push({parent:t||null,affectedField:l,reason:"Unidentified field",structure:null})}}Object.defineProperty(exports,"__esModule",{value:!0}),exports.jsonValidator=jsonValidator;