Skip to content

Commit 3bb4804

Browse files
committed
template: Make validation errors from failed pattern matches shorter
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".
1 parent 8a3f299 commit 3bb4804

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

lib/template.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,7 @@ class Template {
10841084
const param = error.dataPath
10851085
.replace('.', ''); // strip leading dot
10861086
let message = (param !== '') ? `parameter ${param} ${error.message}` : error.message;
1087+
let details = null;
10871088

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

1098-
return {
1099-
message
1100-
};
1099+
if (error.keyword === 'pattern') {
1100+
message = `parameter ${param} should match pattern`;
1101+
details = `failed to match pattern: ${error.params.pattern}`;
1102+
}
1103+
1104+
const newError = { message };
1105+
if (details) {
1106+
newError.details = details;
1107+
}
1108+
1109+
return newError;
11011110
});
11021111
}
11031112

test/template.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,4 +1436,27 @@ describe('Template class tests', function () {
14361436
});
14371437
});
14381438
});
1439+
it('validate_pattern', function () {
1440+
const yamldata = `
1441+
definitions:
1442+
foo:
1443+
pattern: bar
1444+
parameters:
1445+
foo: foo
1446+
template: |-
1447+
{{foo}}
1448+
`;
1449+
1450+
return Template.loadYaml(yamldata)
1451+
.then((tmpl) => {
1452+
assert.throws(() => tmpl.validateParameters(), {
1453+
validationErrors: [
1454+
{
1455+
message: 'parameter foo should match pattern',
1456+
details: 'failed to match pattern: bar'
1457+
}
1458+
]
1459+
});
1460+
});
1461+
});
14391462
});

0 commit comments

Comments
 (0)