Permalink
Browse files

Initial commit

  • Loading branch information...
nathanoehlman committed Nov 8, 2012
1 parent 42bbad3 commit b0f863ec891835c97f86b0c9df40aa074dcc1157
Showing with 118 additions and 0 deletions.
  1. +1 −0 index.js
  2. +55 −0 lib/validator.js
  3. +28 −0 package.json
  4. +34 −0 test/validator.js
View
@@ -0,0 +1 @@
+module.exports = require('./lib/validator');
View
@@ -0,0 +1,55 @@
+var _ = require('lodash'),
+ revalidator = require('revalidator'),
+ formats = {
+ url: { type: 'url' },
+ email: { type: 'email' },
+ ip: { type: 'ip-address' },
+ ipv6: { type: 'ipv6' },
+ datetime: { type: 'date-time' },
+ date: { type: 'date' },
+ time: { type: 'time' },
+ color: { type: 'color' },
+ hostname: { type: 'host-name' },
+ utc: { type: 'utc-millisec' },
+ regex: { type: 'regex' }
+ };
+
+function EasyValidator(fields) {
+
+ var validator = this;
+ validator.properties = {};
+ _.each(fields, validator.add.bind(validator));
+}
+
+EasyValidator.prototype.validate = function(obj, opts) {
+ return revalidator.validate(obj, {properties: this.properties}, opts);
+}
+
+EasyValidator.prototype.add = function(field) {
+ this.properties[field] = {
+ type: 'string',
+ required: true
+ };
+ return this;
+}
+
+EasyValidator.prototype.format = function(field, format, opts) {
+ if (!this.properties[field]) this.add(field);
+ this.properties[field]['format'] = format;
+ if (opts) {
+ this.properties[field] = _.extend(this.properties[field], opts);
+ }
+ return this;
+}
+
+// Bind convenience methods
+_.each(formats, function(value, key) {
+ EasyValidator.prototype[key] = function(field, format, opts) {
+ return this.format(field, value.type, opts);
+ };
+});
+
+
+module.exports = function(fields) {
+ return new EasyValidator(fields);
+}
View
@@ -0,0 +1,28 @@
+{
+ "name": "easyvalidator",
+ "version": "0.0.0",
+ "description": "Super simple validation based on reValidator",
+ "main": "index.js",
+ "dependencies": {
+ "revalidator": "~0.1.3",
+ "lodash": "~0.9.1"
+ },
+ "devDependencies": {
+ "mocha": "~1.7.0",
+ "chai": "~1.3.0"
+ },
+ "scripts": {
+ "test": "mocha --reporter spec"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/nathanoehlman/easyvalidator.git"
+ },
+ "keywords": [
+ "validation",
+ "simple",
+ "revalidator"
+ ],
+ "author": "Nathan Oehlman",
+ "license": "BSD"
+}
View
@@ -0,0 +1,34 @@
+var should = require('chai').should(),
+ easyvalidator = require('..');
+
+describe('Easy validator', function() {
+
+ it('should be able to create a simple validator', function(done) {
+
+ var validator = easyvalidator(['name', 'description']),
+ obj = {name: 'test'},
+ result = validator.validate(obj);
+
+ result.valid.should.equal(false);
+ result.errors[0].should.have.property('attribute', 'required');
+ result.errors[0].should.have.property('property', 'description');
+ result.errors.length.should.equal(1);
+ done();
+
+ });
+
+
+ it('should be able to chain formats', function(done) {
+
+ var validator = easyvalidator(['name']).email('email').add('description'),
+ obj = { name: 'test', email: '123123'},
+ result = validator.validate(obj);
+
+ result.valid.should.equal(false);
+ result.errors[0].should.have.property('attribute', 'format');
+ result.errors[0].should.have.property('property', 'email');
+ result.errors.length.should.equal(2);
+
+ done();
+ });
+});

0 comments on commit b0f863e

Please sign in to comment.