Skip to content

Commit

Permalink
Validator displays name fields from error path (#72)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
b-wils authored Aug 5, 2024
1 parent 0466452 commit ffa3bf6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
25 changes: 23 additions & 2 deletions docs/static/js/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand All @@ -473,15 +474,35 @@ 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,
message: (error.parentSchema?._name ?? "Value") + " " + error.message,
path: prefix + (error.instancePath ?? ""),
name: error.parentSchema?._docs_name ?? "Value",
docs: error.parentSchema?._docs,
path_names,
};
}
}
Expand Down
2 changes: 2 additions & 0 deletions docs/validator/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ textarea {
<thead>
<tr>
<th>Path</th>
<th>Named Path</th>
<th>Severity</th>
<th>Message</th>
<th>Docs</th>
Expand Down Expand Up @@ -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"));
Expand Down

0 comments on commit ffa3bf6

Please sign in to comment.