Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

castProperties now handles property tags

  • Loading branch information...
commit a9d9038c9808493c4dc676ed5398dd29fcd557f0 1 parent 9252f86
@serby authored
View
27 lib/entity-definition.js
@@ -70,14 +70,29 @@ EntityDefinition.prototype.cast = function(type, value) {
return value;
};
-EntityDefinition.prototype.castProperties = function(entityObject) {
- var schema = this.schema;
- var self = this;
- Object.keys(schema).forEach(function(key) {
- if ((self.schema[key].type) && (entityObject[key] !== undefined)) {
- entityObject[key] = self.cast(schema[key].type, entityObject[key]);
+EntityDefinition.prototype.castProperties = function(entityObject, tag) {
+ var
+ schema = this.schema,
+ self = this,
+ expected = [];
+
+ if (tag === undefined) {
+ expected = Object.keys(self.schema);
+ } else {
+ // Find which properties are required
+ Object.keys(self.schema).forEach(function(key) {
+ if (self.schema[key].tag && Array.isArray(self.schema[key].tag) && self.schema[key].tag.indexOf(tag) > -1) {
+ expected.push(key);
+ }
+ });
+ }
+ // Fix for the expected properties
+ expected.forEach(function(key) {
+ if (self.schema[key].type) {
+ entityObject[key] = self.cast(self.schema[key].type, entityObject[key]);
}
});
+
return entityObject;
};
View
44 package.json
@@ -1,24 +1,24 @@
{
- "name": "piton-entity",
- "description": "Tools for managing objects that represent business entities from the piton toolkit",
- "version": "0.0.7",
- "author": "Paul Serby <paul.serby@clock.co.uk>",
- "contributors": [
- { "name": "Paul Serby", "email": "paul.serby@clock.co.uk" }
- ],
- "dependencies": {
- "piton-validity": ">= 0.0.1 < 0.1.0",
- "piton-string-utils": ">= 0.0.1 < 0.1.0"
- },
- "devDependencies": {
- "expresso": "0.7.2"
- },
- "keywords": ["entity"],
- "repository": "git://github.com/PabloSerbo/piton-entity",
- "main": "index",
- "scripts": {
- "test": "make test",
- "prepublish" : "npm prune"
- },
- "engines": { "node": ">= 0.4.6 < 0.5.0" }
+ "name": "piton-entity",
+ "description": "Tools for managing objects that represent business entities from the piton toolkit",
+ "version": "0.0.8",
+ "author": "Paul Serby <paul.serby@clock.co.uk>",
+ "contributors": [
+ { "name": "Paul Serby", "email": "paul.serby@clock.co.uk" }
+ ],
+ "dependencies": {
+ "piton-validity": ">= 0.0.1 < 0.1.0",
+ "piton-string-utils": ">= 0.0.1 < 0.1.0"
+ },
+ "devDependencies": {
+ "expresso": "0.7.2"
+ },
+ "keywords": ["entity"],
+ "repository": "git://github.com/PabloSerbo/piton-entity",
+ "main": "index",
+ "scripts": {
+ "test": "make test",
+ "prepublish" : "npm prune"
+ },
+ "engines": { "node": ">= 0.4.6 < 0.5.0" }
}
View
10 test/entity-definition.test.js
@@ -124,8 +124,9 @@ module.exports = {
cast;
for(var i = 0; i < assertions[type].length; i += 2) {
assert.eql({
- age: assertions[type][i]
- },cast = entityDefinition.castProperties({ age: assertions[type][i + 1] }), 'Failed to cast \'' + type + '\' from \'' + assertions[type][i + 1] + '\' to \'' + assertions[type][i] + '\' instead got \'' + cast.name + '\'');
+ age: assertions[type][i],
+ active: null
+ },cast = entityDefinition.castProperties({ age: assertions[type][i + 1] }), 'Failed to cast \'' + type + '\' from \'' + assertions[type][i + 1] + '\' to \'' + assertions[type][i] + '\' instead got \'' + cast.age + '\'' + JSON.stringify(cast));
}
}
, 'castProperties converts boolean types of properties correctly': function() {
@@ -134,13 +135,16 @@ module.exports = {
// Even = expected, odd = supplied
for(var i = 0; i < assertions[type].length; i += 2) {
assert.eql({
+ age: null,
active: assertions[type][i]
- },cast = entityDefinition.castProperties({ active: assertions[type][i + 1] }), 'Failed to cast \'' + type + '\' from \'' + assertions[type][i + 1] + '\' to \'' + assertions[type][i] + '\' instead got \'' + cast.active + '\'');
+ },cast = entityDefinition.castProperties({ active: assertions[type][i + 1] }), 'Failed to cast \'' + type + '\' from \'' + assertions[type][i + 1] + '\' to \'' + assertions[type][i] + '\' instead got \'' + cast.active + '\'' + JSON.stringify(cast));
}
}
, 'castProperties does not effect untyped properties': function() {
var entityDefinition = createTestEntityDefinition();
assert.eql({
+ age: null,
+ active: null,
phoneNumber: '555-0923'
}, entityDefinition.castProperties({ phoneNumber: '555-0923' }));
}
Please sign in to comment.
Something went wrong with that request. Please try again.