Skip to content

Commit

Permalink
More DBRef tests and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
morphar committed Feb 13, 2015
1 parent ecdb713 commit 9fac922
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
20 changes: 18 additions & 2 deletions examples/comprehensive.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ var testAuthorFullName = function(field, data) {
return str;
};

var createDBRef = function(collectionName, idFieldName, field, data) {
return new DBRef(collectionName, data[idFieldName]);
};

var createDBRefs = function(collectionName, idFieldName, field, data) {
var tagIds = data[idFieldName];
var DBRefs = [];
for(var i in tagIds) {
DBRefs.push(new DBRef(collectionName, tagIds[i]));
}
return DBRefs;
};

// Just a helper to set strictness on all schemas at the same time
var setAllStrictnesses = function(strictness) {
templateSchema.setStrictness(strictness);
Expand All @@ -34,7 +47,7 @@ var testObject = {
"_id" : ObjectId('529327e2675916232d000004'),
"_version" : 3,
"status" : "published",
"author" : new DBRef(),
"author" : { _id: '519b983d78c2bde0dc000012', collection: 'authors' },
"authorBiography" : null,
"authorImage" : "/img/thumb-529327e2675916232d000004.png",
"authorFirstName": "Some",
Expand All @@ -48,6 +61,8 @@ var testObject = {
"likes" : [ '519b983d78c2bde0dc000112',
ObjectId('519b983d78c2bde0dc000113') ]
},
"tag_ids" : [ ObjectId('519b983d78c2bde0dc000112'),
ObjectId('519b983d78c2bde0dc000113') ],
"image" : "/img/img-529327b9675916232d000001.jpg",
"template" : [ { "title" : "Title", "id" : "title", "description" : "some some" },
{ "title" : "Title 2", "id" : "title2", "description" : "different" }],
Expand Down Expand Up @@ -96,12 +111,13 @@ testSchema = new ObjectSchema({
authorBiography: { ignored: true },
authorImage: { ignored: true },
author_id: { ignored: true },
author: { instanceOf: DBRef },
author: { required: true, filters: [createDBRef.bind(null, 'authors', 'author_id')], instanceOf: DBRef },
authorFullName: { required: true, filters: [testAuthorFullName] },
authorFirstName: { ignored: true },
authorLastName: { ignored: true },
nonExising: { optional: true }, // optional is only valid with strict
flags: { objectSchema: flagsSchema },
tags: { required: true, filters: [createDBRefs.bind(null, 'tags', 'tag_ids')] },
template: { objectSchema: templateSchema },
unsetField: { default: { my: { little: 'object' } } }
}, { });
Expand Down
14 changes: 13 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ var testObject = {
"likes" : [ ObjectId('519b983d78c2bde0dc000112'),
ObjectId('519b983d78c2bde0dc000113') ]
},
"tag_ids" : [ ObjectId('519b983d78c2bde0dc000112'),
ObjectId('519b983d78c2bde0dc000113') ],
"image" : "/img/img-529327b9675916232d000001.jpg",
"template" : [ { "title" : "Title", "id" : "title", "description" : "some some" },
{ "title" : "Title 2", "id" : "title2", "description" : "different" }],
Expand All @@ -45,6 +47,15 @@ var createDBRef = function(collectionName, idFieldName, field, data) {
return new DBRef(collectionName, data[idFieldName]);
};

var createDBRefs = function(collectionName, idFieldName, field, data) {
var tagIds = data[idFieldName];
var DBRefs = [];
for(var i in tagIds) {
DBRefs.push(new DBRef(collectionName, tagIds[i]));
}
return DBRefs;
};

// Helper to set strictness on all schemas at the same time
var setAllStrictnesses = function(strictness) {
templateSchema.setStrictness(strictness);
Expand Down Expand Up @@ -226,6 +237,7 @@ describe('ObjectSchema', function() {
authorBiography: { ignored: true },
authorImage: { ignored: true },
author: { required: true, filters: [createDBRef.bind(null, 'authors', 'author_id')], instanceOf: DBRef },
tags: { required: true, filters: [createDBRefs.bind(null, 'tags', 'tag_ids')] },
authorFullName: { required: true, filters: [testAuthorFullName] },
authorFirstName: { ignored: true },
authorLastName: { ignored: true },
Expand All @@ -244,7 +256,7 @@ describe('ObjectSchema', function() {
setAllStrictnesses('strict');
testSchema.validate(testObject, function(errors, result) {
assert.equal(result, false, 'testSchema result should be false');
assert.lengthOf(errors, 5, 'testSchema should have 27 errors');
assert.lengthOf(errors, 6, 'testSchema should have 27 errors');
done();
});
});
Expand Down

0 comments on commit 9fac922

Please sign in to comment.