From ffa3bf6424e311eecbddcc416af22c357ba25184 Mon Sep 17 00:00:00 2001 From: Brandon Wilson Date: Mon, 5 Aug 2024 14:21:20 -0400 Subject: [PATCH] Validator displays name fields from error path (#72) * Validator displays name fields from error path * Validtor layer name extraction improvements * Move the name joining into md file * Add null names for layers without names * Styling fixes to make it more consistent * Fix variable naming to underscore case * Refactor path name extraction into error_clean function --- docs/static/js/validator.js | 25 +++++++++++++++++++++++-- docs/validator/index.md | 2 ++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/docs/static/js/validator.js b/docs/static/js/validator.js index 9729d67..97e03c7 100644 --- a/docs/static/js/validator.js +++ b/docs/static/js/validator.js @@ -462,7 +462,8 @@ class Validator let errors = []; if ( !this._validate_internal(data) ) - errors = this._validate_internal.errors.map(e => this._cleaned_error(e)); + errors = this._validate_internal.errors + .map(e => this._cleaned_error(e, data)); return errors.sort((a, b) => { if ( a.path < b.path ) @@ -473,8 +474,27 @@ class Validator }); } - _cleaned_error(error, prefix="") + _cleaned_error(error, data, prefix="") { + const path_parts = error.instancePath.split('/'); + + const path_names = []; + for (const path_part of path_parts) + { + if (path_part === '#' || path_part === '') + continue; + + data = data[path_part]; + + if (!data) + break; + + // Every layer with a type may be named + // Push a null value if it doesn't exist so display code can handle + if (data.ty) + path_names.push(data.nm); + } + return { type: error.type ?? "error", warning: error.warning, @@ -482,6 +502,7 @@ class Validator path: prefix + (error.instancePath ?? ""), name: error.parentSchema?._docs_name ?? "Value", docs: error.parentSchema?._docs, + path_names, }; } } diff --git a/docs/validator/index.md b/docs/validator/index.md index 110af2c..88f262f 100644 --- a/docs/validator/index.md +++ b/docs/validator/index.md @@ -131,6 +131,7 @@ textarea { Path + Named Path Severity Message Docs @@ -185,6 +186,7 @@ function show_errors(errors) if ( error.type == "warning" ) tr.classList.add("warning-" + error.warning); tr.appendChild(document.createElement("td")).appendChild(document.createTextNode(error.path ?? "")); + tr.appendChild(document.createElement("td")).appendChild(document.createTextNode(error.path_names ? error.path_names.map(n => n ?? "(unnamed)").join(' > ') : "")); tr.appendChild(document.createElement("td")).appendChild(document.createTextNode(error.type)); tr.appendChild(document.createElement("td")).appendChild(document.createTextNode(error.message)); let td = tr.appendChild(document.createElement("td"));