Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve uniqueItemProperties error messaging #123

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions keywords/uniqueItemProperties.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ var SCALAR_TYPES = ['number', 'integer', 'string', 'boolean', 'null'];
module.exports = function defFunc(ajv) {
defFunc.definition = {
type: 'array',
errors: true,
compile: function(keys, parentSchema, it) {
var equal = it.util.equal;
var scalar = getScalarKeys(keys, parentSchema);

return function(data) {
return function validate(data) {
if (data.length > 1) {
for (var k=0; k < keys.length; k++) {
var i, key = keys[k];
Expand All @@ -20,15 +21,20 @@ module.exports = function defFunc(ajv) {
var prop = data[i][key];
if (prop && typeof prop == 'object') continue;
if (typeof prop == 'string') prop = '"' + prop;
if (hash[prop]) return false;
if (hash[prop]) {
validate.errors = constructError(key);
return false;
}
hash[prop] = true;
}
} else {
for (i = data.length; i--;) {
if (!data[i] || typeof data[i] != 'object') continue;
for (var j = i; j--;) {
if (data[j] && typeof data[j] == 'object' && equal(data[i][key], data[j][key]))
if (data[j] && typeof data[j] == 'object' && equal(data[i][key], data[j][key])){
validate.errors = constructError(key);
return false;
}
}
}
}
Expand Down Expand Up @@ -57,3 +63,12 @@ function getScalarKeys(keys, schema) {
: SCALAR_TYPES.indexOf(propType) >= 0;
});
}

function constructError(key) {
const keyword = 'uniqueItemProperties';
return [{
keyword,
params: { keyword },
message: 'should have unique ' + key,
}];
}
53 changes: 53 additions & 0 deletions spec/uniqueItemProperties.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict';

var Ajv = require('ajv');
var defFunc = require('../keywords/uniqueItemProperties');
var defineKeywords = require('..');

describe('keyword "uniqueItemProperties"', function () {
var ajvs = [
defFunc(new Ajv({ $data: true })),
defineKeywords(new Ajv({ $data: true }), 'uniqueItemProperties'),
defineKeywords(new Ajv({ $data: true, allErrors: true }))
];

describe('with scalar propery', function () {
ajvs.forEach(function (ajv, i) {
it('should return error message with property name #' + i, function () {
var invalidData = [
{ 'id': 1 },
{ 'id': 1 },
{ 'id': 3 }
];
var validate = ajv.compile({ uniqueItemProperties: ['id'] });

validate(invalidData).should.equal(false);
var error = validate.errors[0];

error.keyword.should.equal('uniqueItemProperties');
error.params.keyword.should.equal('uniqueItemProperties');
error.message.should.equal('should have unique id');
});
});
});

describe('with non scalar propery', function () {
ajvs.forEach(function (ajv, i) {
it('should return error message with property name #' + i, function () {
var invalidData = [
{ 'name': {'firstName': 'John', 'lastName': 'Doe'}},
{ 'name': {'firstName': 'John', 'lastName': 'Doe'}},
{ 'name': {'firstName': 'Lorem', 'lastName': 'Ipsum'}}
];
var validate = ajv.compile({ uniqueItemProperties: ['name'] });

validate(invalidData).should.equal(false);
var error = validate.errors[0];

error.keyword.should.equal('uniqueItemProperties');
error.params.keyword.should.equal('uniqueItemProperties');
error.message.should.equal('should have unique name');
});
});
});
});