-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5207f54
Showing
8 changed files
with
857 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2013 Nosco ApS | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[](http://badge.fury.io/js/object-schema) | ||
|
||
# ObjectSchema | ||
|
||
ObjectSchema is meant as a filter/schema for filtering, checking and validating javascript objects. | ||
|
||
It can be used for more than just creating MongoDB schemas in node.js. | ||
|
||
The idea is to use the same code to validate client side form data (soon). | ||
|
||
### This is an early BETA version | ||
|
||
As soon as the module has shown it's worth and stability on a live system, it will be marked as version >= 1.0.0. | ||
|
||
Until then: Feel free to play around with it, learn from it. | ||
|
||
### To install | ||
|
||
npm install object-schema |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/****************************************************\ | ||
******************** WARNING!!!! ******************* | ||
* This is meant as a thorough example! * | ||
* It does more or less the same as the test script * | ||
\****************************************************/ | ||
|
||
var ObjectId = require('mongodb').ObjectID; | ||
var DBRef = require('mongodb').DBRef; | ||
var ObjectSchema = require('../index.js'); | ||
|
||
// A couple of filter functions to test with | ||
var testLC = function(field, data) { return data[field].toLowerCase(); }; | ||
var testAuthorFullName = function(field, data) { | ||
var str = data['authorFirstName']; | ||
str += (data['authorFirstName'] && data['authorLastName']) ? ' ' : ''; | ||
str += data['authorLastName']; | ||
return str; | ||
}; | ||
|
||
// Just a helper to set strictness on all schemas at the same time | ||
var setAllStrictnesses = function(strictness) { | ||
templateSchema.setStrictness(strictness); | ||
templateItemsSchema.setStrictness(strictness); | ||
flagsSchema.setStrictness(strictness); | ||
testSchema.setStrictness(strictness); | ||
}; | ||
|
||
/**************************************\ | ||
** Here comes the interesting part! ** | ||
\**************************************/ | ||
|
||
// Create an object to test against | ||
var testObject = { | ||
"_id" : ObjectId('529327e2675916232d000004'), | ||
"_version" : 3, | ||
"status" : "published", | ||
"author" : new DBRef(), | ||
"authorBiography" : null, | ||
"authorImage" : "/img/thumb-529327e2675916232d000004.png", | ||
"authorFirstName": "Some", | ||
"authorLastName": "Name", | ||
"author_id" : ObjectId('519b983d78c2bde0dc000012'), | ||
"counters" : { | ||
"books" : 5 | ||
}, | ||
"description" : "Some description ", | ||
"flags" : { "votes" : [ '519b983d78c2bde0dc000012' ], | ||
"likes" : [ ObjectId('519b983d78c2bde0dc000112'), | ||
ObjectId('519b983d78c2bde0dc000113') ] | ||
}, | ||
"image" : "/img/img-529327b9675916232d000001.jpg", | ||
"template" : [ { "title" : "Title", "id" : "title", "description" : "some some" }, | ||
{ "title" : "Title 2", "id" : "title2", "description" : "different" }], | ||
"title" : "Title 2", | ||
"notDefined": "ignore or throw out" | ||
}; | ||
|
||
|
||
// This is the schema for each item of the template | ||
templateItemsSchema = new ObjectSchema({ | ||
'title': { type: 'string', required: true }, | ||
'id': { type: 'string', required: true }, | ||
'description': { type: 'string', regex: /^.+/i } | ||
}); | ||
|
||
// Each field in the template schema should be a valid templateItemsSchema | ||
templateSchema = new ObjectSchema({ | ||
'*': { objectSchema: templateItemsSchema } | ||
}); | ||
|
||
|
||
// All fields in a flags schema consists of an array of ObjectIds | ||
// This is a new schema to match each of the arrays values | ||
// The template above, could also have been constructed like this | ||
// Flags looks like this: | ||
// "flags" : { "votes" : [ '519b983d78c2bde0dc000012' ], | ||
// "likes" : [ '519b983d78c2bde0dc000112' ] } | ||
// Do you see this pattern?: | ||
// * * object, ObjectId, 'objectId' | ||
flagsSchema = new ObjectSchema({ | ||
'*': { objectSchema: { '*': { type: 'object', | ||
instanceOf: ObjectId, | ||
filters: ['objectId'] } } } | ||
}); | ||
|
||
|
||
// Create the "parent" ObjectSchema schema | ||
testSchema = new ObjectSchema({ | ||
_id: { required: true, instanceOf: ObjectId }, | ||
_version: { required: true, type: 'number' }, | ||
description: { required: true, filters: [ 'trim', testLC ], | ||
regex: /some description/ }, | ||
counters: { ignored: true }, | ||
authorBiography: { ignored: true }, | ||
authorImage: { ignored: true }, | ||
author_id: { ignored: true }, | ||
author: { instanceOf: DBRef }, | ||
authorFullName: { required: true, filters: [testAuthorFullName] }, | ||
authorFirstName: { ignored: true }, | ||
authorLastName: { ignored: true }, | ||
flags: { objectSchema: flagsSchema }, | ||
template: { objectSchema: templateSchema } | ||
}, { }); | ||
|
||
|
||
setAllStrictnesses('strict'); | ||
testSchema.validate(testObject, function(errors, result) { | ||
console.log('----------'); | ||
console.log(' strict'); | ||
console.log('----------'); | ||
console.log('errors:'); | ||
console.log(errors); | ||
console.log('----------'); | ||
console.log('result:'); | ||
console.log(result); | ||
console.log(' '); | ||
}); | ||
|
||
setAllStrictnesses('relaxed'); | ||
testSchema.validate(testObject, function(errors, result) { | ||
console.log('----------'); | ||
console.log(' relaxed'); | ||
console.log('----------'); | ||
console.log('errors:'); | ||
console.log(errors); | ||
console.log('----------'); | ||
console.log('result:'); | ||
console.log(result); | ||
console.log(' '); | ||
}); | ||
|
||
setAllStrictnesses('loose'); | ||
testSchema.validate(testObject, function(errors, result) { | ||
console.log('----------'); | ||
console.log(' loose'); | ||
console.log('----------'); | ||
console.log('errors:'); | ||
console.log(errors); | ||
console.log('----------'); | ||
console.log('result:'); | ||
console.log(result); | ||
console.log(' '); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
var ObjectId = require('mongodb').ObjectID; | ||
var ObjectSchema = require('../index.js'); | ||
|
||
// Create an object to test against | ||
var testObject = { | ||
"_id" : ObjectId('529327e2675916232d000004'), | ||
"_version" : 3, | ||
"description" : " Some description ", | ||
"nonsense": "ignore", | ||
"notDefined": "ignore or throw out" | ||
}; | ||
|
||
// Create the ObjectSchema schema | ||
testSchema = new ObjectSchema({ | ||
_id: { required: true, instanceOf: ObjectId }, | ||
_version: { required: true, type: 'number' }, | ||
description: { required: true, filters: [ 'trim' ] }, | ||
nonsense: { ignored: true } | ||
}, { }); | ||
|
||
|
||
testSchema.setStrictness('strict'); | ||
testSchema.validate(testObject, function(errors, result) { | ||
console.log('----------'); | ||
console.log(' strict'); | ||
console.log('----------'); | ||
console.log('errors:'); | ||
console.log(errors); | ||
console.log('----------'); | ||
console.log('result:'); | ||
console.log(result); | ||
console.log(' '); | ||
}); | ||
|
||
testSchema.setStrictness('relaxed'); | ||
testSchema.validate(testObject, function(errors, result) { | ||
console.log('----------'); | ||
console.log(' relaxed'); | ||
console.log('----------'); | ||
console.log('errors:'); | ||
console.log(errors); | ||
console.log('----------'); | ||
console.log('result:'); | ||
console.log(result); | ||
console.log(' '); | ||
}); | ||
|
||
testSchema.setStrictness('loose'); | ||
testSchema.validate(testObject, function(errors, result) { | ||
console.log('----------'); | ||
console.log(' loose'); | ||
console.log('----------'); | ||
console.log('errors:'); | ||
console.log(errors); | ||
console.log('----------'); | ||
console.log('result:'); | ||
console.log(result); | ||
console.log(' '); | ||
}); |
Oops, something went wrong.