Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 11, 2015
0 parents commit 8509dbb
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
92 changes: 92 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Created by https://www.gitignore.io

### Node ###
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules


### OSX ###
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk


### SublimeText ###
# cache files for sublime text
*.tmlanguage.cache
*.tmPreferences.cache
*.stTheme.cache

# workspace files are user-specific
*.sublime-workspace

# project files should be checked into the repository, unless a significant
# proportion of contributors will probably not be using SublimeText
# *.sublime-project

# sftp configuration file
sftp-config.json


### VirtualEnv ###
# Virtualenv
# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
.Python
[Bb]in
[Ii]nclude
[Ll]ib
[Ss]cripts
pyvenv.cfg
pip-selfcheck.json


## nightwatch
reports
reports/*
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "adonis-validation-provider",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"indicative": "^1.2.4",
"lodash": "^3.10.1"
},
"devDependencies": {
"chai": "^3.2.0",
"co": "^4.6.0",
"mocha": "^2.3.2"
}
}
109 changes: 109 additions & 0 deletions src/Validator/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
"use strict";

/**
* adonis-validation-provider
* Copyright(c) 2015-2015 Harminder Virk
* MIT Licensed
*/

const Indicative = new(require("indicative"))
const _ = require("lodash")

class Validator {

constructor() {
this.errors = [];
}


/**
* @function validate
* @description validate schema using indicative validate
* and returns on first error
* @param {Object} rules
* @param {Object} data
* @param {Object} messages
* @return {Promise}
* @public
*/
* validate(rules, data, messages) {
let self = this;
return new Promise(function(resolve, reject) {
Indicative
.validate(rules, data, messages)
.then(function(success) {
self.errors = [];
resolve();
})
.catch(function(error) {
self.errors = error;
resolve();
});
});
}


/**
* @function validateAll
* @description validate schema using indicative validate
* @param {Object} rules
* @param {Object} data
* @param {Object} messages
* @return {Promise}
* @public
*/
* validateAll(rules, data, messages) {
let self = this;
return new Promise(function(resolve, reject) {
Indicative
.validateAll(rules, data, messages)
.then(function(success) {
self.errors = [];
resolve();
})
.catch(function(error) {
self.errors = error;
resolve();
});
});
}


/**
* @function fails
* @description tells whether there was an error using validate method
* or not
* @return {Boolean}
* @public
*/
fails() {
return _.size(this.errors) ? true : false;
}

/**
* @function messages
* @description returns error messages
* @return {Array}
* @public
*/
messages() {
return this.errors;
}


/**
* @function extend
* @description adds new methods to indicative lib
* @param {String} rule
* @param {Function} method
* @param {String} message
* @public
*/
extend(rule, method, message) {
Indicative.extend(rule, message, method);
}

}


module.exports = Validator;
55 changes: 55 additions & 0 deletions test/validator.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"use strict";

/**
* adonis-validation-provider
* Copyright(c) 2015-2015 Harminder Virk
* MIT Licensed
*/

const Validator = new(require("../src/Validator"))
const co = require("co")
const chai = require("chai")
const expect = chai.expect

describe("Validator", function() {

it("should validate data schema and return appropriate errors", function(done) {

co(function*() {

let schema = {
username: 'required'
}

let data = {};

yield Validator.validate(schema, data);

expect(Validator.fails()).to.equal(true);
expect(Validator.messages()[0].rule).to.equal("required");

}).then(done).catch(done)

});

it("should not return previous errors when validation is passed next time", function(done) {

co(function*() {

let schema = {
username: 'required'
}

let data = {
username: 'boom'
};

yield Validator.validate(schema, data);
expect(Validator.fails()).to.equal(false);

}).then(done).catch(done)


});

});

0 comments on commit 8509dbb

Please sign in to comment.