Skip to content

Commit

Permalink
template: Make validation errors from failed pattern matches shorter
Browse files Browse the repository at this point in the history
The pattern that was failed to match has been moved from "message" to
"details". The regex pattern can get very long, so to keep messages
short, it will now always be in "details".
  • Loading branch information
mstokes-f5 committed Oct 2, 2023
1 parent 8a3f299 commit d2ae90b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,7 @@ class Template {
const param = error.dataPath
.replace('.', ''); // strip leading dot
let message = (param !== '') ? `parameter ${param} ${error.message}` : error.message;
let details = null;

if (error.keyword === 'type') {
const typeStr = error.params.type;
Expand All @@ -1095,9 +1096,17 @@ class Template {
message = `${message}: ${allowedValues.join(', ')}`;
}

return {
message
};
if (error.keyword === 'pattern') {
message = `parameter ${param} should match pattern`;
details = `failed to match pattern: ${error.params.pattern}`;
}

const newError = { message };
if (details) {
newError.details = details;
}

return newError;
});
}

Expand Down
23 changes: 23 additions & 0 deletions test/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -1436,4 +1436,27 @@ describe('Template class tests', function () {
});
});
});
it('validate_pattern', function () {
const yamldata = `
definitions:
foo:
pattern: bar
parameters:
foo: foo
template: |-
{{foo}}
`;

return Template.loadYaml(yamldata)
.then((tmpl) => {
assert.throws(() => tmpl.validateParameters(), {
validationErrors: [
{
message: 'parameter foo should match pattern',
details: 'failed to match pattern: bar'
}
]
});
});
});
});

0 comments on commit d2ae90b

Please sign in to comment.