Skip to content

Commit

Permalink
feat: templated messages when errorMessage is an object with keyword …
Browse files Browse the repository at this point in the history
…messaged, #4
  • Loading branch information
epoberezkin committed May 8, 2017
1 parent 938e47f commit 1da841f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
21 changes: 19 additions & 2 deletions lib/dot/errorMessage.jst
Expand Up @@ -42,6 +42,7 @@
, $matches = '_em_matches' + $lvl
, $isArray = '_em_isArray' + $lvl
, $errors = '_em_errors' + $lvl
, $templates = '_em_templates' + $lvl
, $errSchemaPathString = it.util.toQuotedString(it.errSchemaPath);
}}

Expand Down Expand Up @@ -104,9 +105,19 @@
}
}}

{{? Object.keys($keywordErrors).length }}
{{ var $keywordErrorsArr = Object.keys($keywordErrors); }}
{{? $keywordErrorsArr.length }}
{{=$i}} = 0;
{{=$errors}} = {{= JSON.stringify($keywordErrors) }};
var {{=$templates}} = {
{{ var $comma = false; }}
{{~ $keywordErrorsArr:$k }}
{{? INTERPOLATION.test($schema[$k]) }}
{{?$comma}},{{?}}{{= it.util.toQuotedString($k) }}: {{= templateFunc($schema[$k]) }}
{{ $comma = true; }}
{{?}}
{{~}}
};
while ({{=$i}} < errors) {
{{=$err}} = vErrors[{{=$i}}];
if ({{# def.em_keywordErrorMatch}}) {
Expand All @@ -124,7 +135,9 @@
, dataPath: {{=$dataPath}}
, schemaPath: {{=$errSchemaPathString}} + '/{{=$keyword}}'
, params: { errors: {{=$errors}}[{{=$key}}] }
, message: validate.schema{{=$schemaPath}}[{{=$key}}]
, message: {{=$templates}}[{{=$key}}]
? {{=$templates}}[{{=$key}}]()
: validate.schema{{=$schemaPath}}[{{=$key}}]
{{? it.opts.verbose }}
, schema: validate.schema{{=$schemaPath}}
, parentSchema: validate.schema{{=it.schemaPath}}
Expand Down Expand Up @@ -210,4 +223,8 @@
}) + "'";
return expr.replace(EMPTY_STR, '');
}

function templateFunc(str) {
return 'function() { return ' + templateExpr(str) + '; }';
}
}}
55 changes: 53 additions & 2 deletions spec/object.spec.js
Expand Up @@ -10,8 +10,8 @@ describe('errorMessage value is an object', function() {

beforeEach(function() {
ajvs = [
ajvErrors(new Ajv({allErrors: true, jsonPointers: true})),
ajvErrors(new Ajv({allErrors: true, jsonPointers: true, verbose: true}))
ajvErrors(new Ajv({allErrors: true, jsonPointers: true/*, sourceCode: true, processCode: require('js-beautify').js_beautify*/ })),
ajvErrors(new Ajv({allErrors: true, jsonPointers: true, verbose: true/*, sourceCode: true, processCode: require('js-beautify').js_beautify*/ }))
];
});

Expand Down Expand Up @@ -62,6 +62,57 @@ describe('errorMessage value is an object', function() {
}
});
});

it('should replace keyword errors with interpolated error messages', function() {
var schema = {
type: 'object',
properties: {
foo: {
type: 'number',
minimum: 5,
maximum: 10,
errorMessage: {
type: 'property ${0#} should be number, it is ${0}',
minimum: 'property ${0#} should be >= 5, it is ${0}',
maximum: 'property foo should be <= 10'
}
}
}
};

ajvs.forEach(function (ajv) {
var validate = ajv.compile(schema);
assert.strictEqual(validate({foo: 7}), true);
testInvalid({foo: 'a'}, [['type']]);
testInvalid({foo: ['a']}, [['type']]);
testInvalid({foo: 4.5}, [['minimum']]);
testInvalid({foo: 10.5}, [['maximum']]);

function testInvalid(data, expectedErrors) {
assert.strictEqual(validate(data), false);
assert.strictEqual(validate.errors.length, expectedErrors.length);
validate.errors.forEach(function (err, i) {
var expectedErr = expectedErrors[i];
if (Array.isArray(expectedErr)) { // errorMessage error
assert.strictEqual(err.keyword, 'errorMessage');
var expectedMessage = schema.properties.foo
.errorMessage[err.params.errors[0].keyword]
.replace('${0#}', '"foo"')
.replace('${0}', JSON.stringify(data.foo));
assert.strictEqual(err.message, expectedMessage);
assert.strictEqual(err.dataPath, '/foo');
assert.strictEqual(err.schemaPath, '#/properties/foo/errorMessage');
var replacedKeywords = err.params.errors.map(function (e) {
return e.keyword;
});
assert.deepEqual(replacedKeywords.sort(), expectedErr.sort());
} else { // original error
assert.strictEqual(err.keyword, expectedErr);
}
});
}
});
});
});

describe('properties and items', function() {
Expand Down

0 comments on commit 1da841f

Please sign in to comment.