Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Init.

  • Loading branch information...
commit e4baadc298275c45cf888777d066eaf5239b6bf6 0 parents
Jonas Grimfelt authored
0  .env
No changes.
9 .gitignore
@@ -0,0 +1,9 @@
+.env-local
+.DS_Store
+.coverage_data
+.*~
+*~
+tmp/*
+log/*
+node_modules
+npm-debug.log
7 .travis.yml
@@ -0,0 +1,7 @@
+language: node_js
+node_js:
+ - 0.8
+script:
+ - "make test-ci"
+notifications:
+ email: false
20 MIT-LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2013 Jonas Grimfelt. https://github.com/grimen
+
+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.
10 Makefile
@@ -0,0 +1,10 @@
+test:
+ make test-local
+
+test-local:
+ (. ./.env && ./node_modules/.bin/mocha ./test/index.js)
+
+test-ci:
+ (. ./.env && ./node_modules/.bin/mocha ./test/index.js --reporter dot)
+
+.PHONY: test
56 README.md
@@ -0,0 +1,56 @@
+# NODE-DOCUMENT-VALIDATOR [![Build Status](https://secure.travis-ci.org/grimen/node-document-validator.png)](http://travis-ci.org/grimen/node-document-validator)
+
+**Validator** adapter interface for [node-document](https://github.com/grimen/node-document) ODM for Node.js.
+
+## About
+
+Unified interface for validating data based on a custom JSON Schema.
+
+
+## Adapters
+
+* [Amanda](https://github.com/grimen/node-document-validator-amanda)
+* [Schema.js](https://github.com/grimen/node-document-validator-schema)
+* [JSONSchema](https://github.com/grimen/node-document-validator-jsonschema)
+* [JSV](https://github.com/grimen/node-document-validator-jsv)
+
+
+## API
+
+### `#validate`
+
+* `(data, schema, [callback(err, res)])`
+
+ ```javascript
+ validator.validate({foo: 'bar'}, {foo: {type: 'string', required: true}}, function(err, res) {
+ // console.log(arguments);
+ });
+ ```
+
+
+## Installation
+
+```shell
+ $ npm install node-document-validator
+```
+
+
+## Usage
+
+For details; see [node-document](https://github.com/grimen/node-document).
+
+
+## Test
+
+**Local tests:**
+
+```shell
+ $ make test
+```
+
+
+## License
+
+Released under the MIT license.
+
+Copyright (c) [Jonas Grimfelt](http://github.com/grimen)
6 index.js
@@ -0,0 +1,6 @@
+
+// -----------------------
+// Export
+// --------------------
+
+module.exports = require('./lib');
110 lib/index.js
@@ -0,0 +1,110 @@
+require('sugar');
+var fun = require('funargs');
+
+// -----------------------
+// Constructor
+// --------------------
+
+// new Validator ()
+// new Validator (options)
+function Validator () {
+ var self = this, args = fun(arguments);
+
+ self.klass = self.klass || Validator;
+
+ self.options = Object.merge(self.klass.defaults.options, args.objects()[0] || {}, true);
+ self.engine = null;
+}
+
+// -----------------------
+// Class
+// --------------------
+
+// .name
+Validator.__defineGetter__('name', function() {
+ return this.name;
+});
+
+Validator.defaults = {
+ options: {}
+};
+
+Validator.options = null;
+
+Validator.reset = function() {
+ var self = this;
+
+ if (self.defaults) {
+ self.options = self.defaults.options;
+ }
+};
+
+// -----------------------
+// Instance
+// --------------------
+
+// #name
+Validator.prototype.__defineGetter__('name', function() {
+ return this.constructor.name;
+});
+
+// #_validate ()
+Validator.prototype._validate = function(args, execute) {
+ var self = this, attributes, schema, options, callback;
+
+ args = fun(args);
+ attributes = args.objects()[0];
+ schema = args.objects()[1] || {};
+ options = Object.merge(self.options, args.objects()[2] || {}, true);
+ callback = args.functions()[0];
+
+ if (!Object.isObject(attributes)) {
+ throw new Error("ArgumentError: Expected `attributes` <object>, got <" + typeof attributes + ">");
+ }
+
+ if (!Object.isObject(schema)) {
+ throw new Error("ArgumentError: Expected `schema` <object>, got <" + typeof schema + ">");
+ }
+
+ schema = {
+ type: 'object',
+ properties: schema
+ };
+
+ var done = function(errors, valid) {
+ if (callback) {
+ callback(null, errors, valid);
+ }
+ };
+
+ try {
+ execute(attributes, schema, options, done);
+
+ } catch (err) {
+ if (callback) {
+ callback(err);
+ } else {
+ throw err;
+ }
+ }
+
+ // if (!callback) {
+ // var result;
+ // // TODO: Sync:ed call
+ // return result;
+ // }
+}
+
+// #validate (attributes)
+// #validate (attributes, options)
+// #validate (attributes, callback)
+// #validate (attributes, options, callback)
+Validator.prototype.validate = function() {
+ throw new Error("Not implemented");
+}
+
+// -----------------------
+// Export
+// --------------------
+
+module.exports = Validator;
51 package.json
@@ -0,0 +1,51 @@
+{
+ "name": "node-document-validator",
+ "description": "Validator adapter interface for `node-document` ODM for Node.js.",
+ "version": "0.1.0",
+ "homepage": "https://github.com/grimen/node-document-validator",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/grimen/node-document-validator.git"
+ },
+ "bugs": {
+ "web": "https://github.com/grimen/node-document-validator/issues"
+ },
+ "licenses": [
+ {
+ "type": "MIT",
+ "url": "https://github.com/grimen/node-document-validator/blob/MIT-LICENSE"
+ }
+ ],
+ "maintainers": [
+ {
+ "name": "Jonas Grimfelt",
+ "email": "grimen@gmail.com",
+ "web": "https://github.com/grimen"
+ }
+ ],
+ "contributors": [],
+ "directories": {
+ "lib": "./lib"
+ },
+ "main": "./index.js",
+ "scripts": {
+ "test": "make test",
+ "test-ci": "make test-ci"
+ },
+ "bin": {},
+ "engines": {
+ "node": ">= 0.8.0"
+ },
+ "dependencies": {
+ "funargs": "latest",
+ "sugar": "latest"
+ },
+ "devDependencies": {
+ "mocha": "latest",
+ "chai": "latest",
+ "cover": "latest",
+ "node-inspector": "latest",
+ "longjohn": "latest",
+ "colors": "latest"
+ }
+}
24 test/helper.js
@@ -0,0 +1,24 @@
+process.env.NODE_ENV = 'test';
+
+require('sugar');
+require('colors');
+
+var chai = require('chai'),
+ longjohn = require('longjohn');
+
+longjohn.async_trace_limit = 3;
+
+// REVIEW: http://chaijs.com/plugins
+chai.Assertion.includeStack = true;
+
+module.exports.flag = function(value, default_value) {
+ if (typeof value === 'undefined') {
+ return (default_value === undefined) ? false : default_value;
+ } else {
+ return (/^1|true$/i).test('' + value); // ...to avoid the boolean/truthy ghetto.
+ }
+};
+
+module.exports.assert = chai.assert;
+
+module.exports.debug = console.log;
93 test/index.js
@@ -0,0 +1,93 @@
+var helper = require('./helper'),
+ assert = helper.assert,
+ debug = helper.debug;
+
+var Validator = require('../lib/'),
+ validator = new Validator();
+
+module.exports = {
+
+ 'Validator': {
+ 'new': {
+ '()': function() {
+ assert.instanceOf ( validator, require('../lib/') );
+
+ Validator.reset();
+
+ var validator2 = new Validator();
+
+ assert.equal ( validator2.url, null );
+ assert.typeOf ( validator2.options, 'object' );
+ assert.deepEqual ( validator2.options.custom, undefined );
+ },
+
+ '(options)': function() {
+ Validator.reset();
+
+ var validator2 = new Validator({custom: {foo: 'bar'}});
+
+ assert.equal ( validator2.url, null );
+ assert.typeOf ( validator2.options, 'object' );
+ assert.deepEqual ( validator2.options.custom, {foo: 'bar'} );
+ }
+ },
+
+ '.klass': function() {
+ assert.property ( validator, 'klass' );
+ assert.equal ( validator.klass, Validator );
+ },
+
+ '.name': function() {
+ assert.property ( Validator, 'name' );
+ assert.equal ( Validator.name, 'Validator' );
+ },
+
+ '.defaults': function() {
+ assert.property ( Validator, 'defaults' );
+
+ assert.typeOf ( Validator.defaults.options, 'object' );
+ },
+
+ '.options': function() {
+ assert.property ( Validator, 'options' );
+ assert.typeOf ( Validator.options, 'null' );
+ },
+
+ '.reset()': function() {
+ assert.property ( Validator, 'reset' );
+ assert.typeOf ( Validator.reset, 'function' );
+
+ Validator.options = {foo: "bar"};
+ assert.deepEqual ( Validator.options, {foo: "bar"} );
+ assert.deepEqual ( Validator.defaults.options, {} );
+
+ Validator.reset();
+
+ assert.equal ( Validator.options, Validator.defaults.options );
+ }
+ },
+
+ 'Validator.prototype': {
+ '#name': function() {
+ assert.property ( validator, 'name' );
+ assert.equal ( validator.name, 'Validator' );
+ },
+
+ '#options': function() {
+ assert.property ( validator, 'options' );
+ assert.typeOf ( validator.options, 'object' );
+ },
+
+ '#engine': function() {
+ assert.property ( validator, 'engine' );
+ assert.typeOf ( validator.engine, 'null' );
+ },
+
+ '#validate': function() {
+ assert.property ( validator, 'validate' );
+ assert.typeOf ( validator.validate, 'function' );
+ assert.throws ( validator.validate, Error );
+ }
+ }
+
+};
3  test/mocha.opts
@@ -0,0 +1,3 @@
+--ui exports
+--reporter spec
+--bail
Please sign in to comment.
Something went wrong with that request. Please try again.