Skip to content

Commit

Permalink
Implement initial validation support (required properties, max number…
Browse files Browse the repository at this point in the history
… of items in collection), refs #21
  • Loading branch information
bergie committed Sep 17, 2012
1 parent d345356 commit 45efbe5
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
91 changes: 91 additions & 0 deletions src/Entity.js
Expand Up @@ -200,6 +200,97 @@ VIE.prototype.Entity = function(attrs, opts) {
return Backbone.Model.prototype.unset.call(this, attr, opts);
},

validate: function (attrs, opts) {
if (opts && opts.validate === false) {
return;
}

var types = this.get('@type');
if (_.isArray(types)) {
var results = [];
_.each(types, function (type) {
var res = this.validateByType(type, attrs, opts);
if (res) {
results.push(res);
}
}, this);
if (_.isEmpty(results)) {
return;
}
return _.flatten(results);
}

return this.validateByType(types, attrs, opts);
},

validateByType: function (type, attrs, opts) {
var messages = {
max: '<%= property %> cannot contain more than <%= num %> items',
min: '<%= property %> must contain at least <%= num %> items',
required: '<%= property %> is required'
};

if (!type.attributes) {
return;
}

var toError = function (definition, constraint, messageValues) {
return {
property: definition.id,
constraint: constraint,
message: _.template(messages[constraint], _.extend({
property: definition.id
}, messageValues))
};
};

var checkMin = function (definition, attrs) {
if (!attrs[definition.id]) {
return toError(definition, 'required', {});
}
};

// Check the number of items in attr against max
var checkMax = function (definition, attrs) {
if (!attrs[definition.id]) {
return;
}

if (!attrs[definition.id].isCollection || !_.isArray(attrs[definition.id])) {
return;
}

if (attrs[definition.id].length > definition.max) {
return toError(definition, 'max', {
num: definition.max
});
}
};

var results = [];
_.each(type.attributes.list(), function (definition) {
var res;
if (definition.max && definition.max != -1) {
res = checkMax(definition, attrs);
if (res) {
results.push(res);
}
}

if (definition.min && definition.min > 0) {
res = checkMin(definition, attrs);
if (res) {
results.push(res);
}
}
});

if (_.isEmpty(results)) {
return;
}
return results;
},

isNew: function() {
if (this.getSubjectUri().substr(0, 7) === '_:bnode') {
return true;
Expand Down
28 changes: 28 additions & 0 deletions test/core/type.js
Expand Up @@ -271,3 +271,31 @@ test("VIE - Type form schema generation", function () {
var author = new schema['author']['model'];
ok(author.isEntity);
});

test("VIE - Type based validation", function () {
var v = new VIE();
v.namespaces.add("xsd", "http://www.w3.org/2001/XMLSchema#");

// Define an 'article' type
var article = new v.Type("Article");
var person = new v.Type("Person");

// Define some properties
var title = new v.Attribute("title", ["xsd:string"], article, 1, 1);
var content = new v.Attribute("content", ["xsd:string"], article, 0, 1);
var published = new v.Attribute("published", ["xsd:dateTime"], article, 0, 1);
var author = new v.Attribute("author", [person], article, 1, 1);
article.attributes.add([title, content, published, author]);

// Tell VIE about the types
v.types.add([person, article]);

// Create an entity with the type
var entity = new v.Entity({'@type': 'Article'});
ok(entity);
equal(entity.isValid(), false);

var results = entity.validate(entity.attributes);
ok(_.isArray(results));
equal(results.length, 2);
});

0 comments on commit 45efbe5

Please sign in to comment.