Permalink
Browse files

First commit

  • Loading branch information...
0 parents commit a872d7a5a556d364d780267d74e4fc2d1a883c63 @chriso chriso committed Oct 6, 2010
Showing with 218 additions and 0 deletions.
  1. +20 −0 LICENSE
  2. +37 −0 README.md
  3. +1 −0 index.js
  4. +18 −0 lib/filter.js
  5. +2 −0 lib/index.js
  6. +117 −0 lib/validator.js
  7. +23 −0 package.json
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2010 Chris O'Hara <cohara87@gmail.com>
+
+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.
@@ -0,0 +1,37 @@
+A library of methods for input validation, sanitization and filtering.
+
+To install pillar, use [npm](http://github.com/isaacs/npm):
+
+ $ npm install validator
+
+## Example
+
+ var Validator = require('validator').Validator;
+
+ Validator.check('test@email.com').length(3, 40).isEmail();
+
+## Error handling
+
+By default, the validation methods throw an exception when a check fails
+
+ try {
+ Validator.check('abc').notNull().isInt()
+ } catch (e) {
+ console.log(e); //Invalid integer
+ }
+
+To set a custom error message, set the second param of check()
+
+ try {
+ Validator.check('abc', 'Please enter a valid integer').notNull().isInt()
+ } catch (e) {
+ console.log(e); //Please enter a valid integer
+ }
+
+To attach a custom error handler
+
+ Validator.error = function(msg) {
+ //Do smoething
+ }
+
+To see the library in use, check out [pillar](http://github.com/chriso/pillar)
@@ -0,0 +1 @@
+exports = module.exports = require('./lib');
@@ -0,0 +1,18 @@
+Filter = function() {};
+exports.Filter = new Filter();
+
+Filter.prototype.modify = function(str) {
+ //Override
+}
+
+Filter.prototype.filter = function(str) {
+ this.str = str;
+ return this;
+}
+
+Filter.prototype.ifNull = function(replace) {
+ if (!this.str || this.str === '') {
+ this.modify(replace);
+ }
+ return this.str;
+}
@@ -0,0 +1,2 @@
+exports.Validator = require('./validator');
+exports.Filter = require('./filter');
@@ -0,0 +1,117 @@
+Validator = function() {}
+exports.Validator = new Validator();
+
+Validator.prototype.check = function(str, fail_msg) {
+ this.str = String(str);
+ this.msg = fail_msg;
+ return this;
+}
+
+Validator.prototype.error = function(msg) {
+ throw msg;
+ return this;
+}
+
+Validator.prototype.isEmail = function() {
+ if (!this.str || !this.str.match(/^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/)) {
+ this.error(this.msg || 'Invalid email');
+ }
+ return this;
+}
+
+Validator.prototype.isUrl = function() {
+ if (!this.str || !this.str.match(/^(?:(?:ht|f)tp(?:s?)\:\/\/|~\/|\/)?(?:\w+:\w+@)?((?:(?:[-\w\d{1-3}]+\.)+(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|edu|co\.uk|ac\.uk|it|fr|tv|museum|asia|local|travel|[a-z]{2})?)|((\b25[0-5]\b|\b[2][0-4][0-9]\b|\b[0-1]?[0-9]?[0-9]\b)(\.(\b25[0-5]\b|\b[2][0-4][0-9]\b|\b[0-1]?[0-9]?[0-9]\b)){3}))(?::[\d]{1,5})?(?:(?:(?:\/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|\/)+|\?|#)?(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?:#(?:[-\w~!$ |/.,*:;=]|%[a-f\d]{2})*)?$/)) {
+ this.error(this.msg || 'Invalid URL');
+ }
+ return this;
+}
+
+Validator.prototype.isAlpha = function() {
+ if (!this.str || !this.str.match(/^[a-zA-Z]+$/)) {
+ this.error(this.msg || 'Invalid characters');
+ }
+ return this;
+}
+
+Validator.prototype.isAlphanumeric = function() {
+ if (!this.str || !this.str.match(/^[a-zA-Z0-9]+$/)) {
+ this.error(this.msg || 'Invalid characters');
+ }
+ return this;
+}
+
+Validator.prototype.isNumeric = function() {
+ if (!this.str || !this.str.match(/^[0-9]+$/)) {
+ this.error(this.msg || 'Invalid number');
+ }
+ return this;
+}
+
+Validator.prototype.isLowercase = function() {
+ if (!this.str || !this.str.match(/^[a-z0-9]+$/)) {
+ this.error(this.msg || 'Invalid characters');
+ }
+ return this;
+}
+
+Validator.prototype.isUppercase = function() {
+ if (!this.str || !this.str.match(/^[A-Z0-9]+$/)) {
+ this.error(this.msg || 'Invalid characters');
+ }
+ return this;
+}
+
+Validator.prototype.isInt = function() {
+ if (!this.str || !this.str.match(/^(?:0|[1-9][0-9]*)$/)) {
+ this.error(this.msg || 'Invalid integer');
+ }
+ return this;
+}
+
+Validator.prototype.isDecimal = function() {
+ if (!this.str || !this.str.match(/^(?:0|[1-9][0-9]*)(?:\.[0-9]*)?$/)) {
+ this.error(this.msg || 'Invalid decimal');
+ }
+ return this;
+}
+
+Validator.prototype.notNull = function() {
+ if (!this.str || !this.str !== '') {
+ this.error(this.msg || 'Invalid characters');
+ }
+ return this;
+}
+
+Validator.prototype.isNull = function() {
+ if (this.str !== '' && this.str) {
+ this.error(this.msg || 'Invalid characters');
+ }
+ return this;
+}
+
+Validator.prototype.equals = function(equals) {
+ if (this.str != equals) {
+ this.error(this.msg || 'Not equal');
+ }
+ return this;
+}
+
+Validator.prototype.regex = function(pattern, modifiers) {
+ if (typeof pattern !== 'function') {
+ pattern = new RegExp(pattern, modifiers || '');
+ }
+ if (! (this.str || '').match(pattern)) {
+ this.error(this.msg || 'Invalid characters');
+ }
+ return this;
+}
+
+Validator.prototype.notRegex = function(pattern, modifiers) {
+ if (typeof pattern !== 'function') {
+ pattern = new RegExp(pattern, modifiers || '');
+ }
+ if ((this.str || '').match(pattern)) {
+ this.error(this.msg || 'Invalid characters');
+ }
+ return this;
+}
@@ -0,0 +1,23 @@
+{ "name" : "validator",
+ "description" : "Validation, sanitization and filtering for node.js",
+ "version" : "0.0.1",
+ "homepage" : "http://github.com/chriso/node-validator",
+ "keywords" : ["validator", "validation", "filter", "filtering", "xss", "sanitization", "sanitize"],
+ "author" : "Chris O'Hara <cohara87@gmail.com>",
+ "main" : "./lib",
+ "directories" : { "lib" : "./lib" },
+ "bugs" :
+ { "mail" : "",
+ "web" : "http://github.com/chriso/node-validator/issues"
+ },
+ "repository": {
+ "type": "git",
+ "url": "http://github.com/chriso/node-validator.git"
+ },
+ "engines" : { "node" : ">=0.2.2" },
+ "licenses" :
+ [ { "type" : "MIT"
+ , "url" : "http://github.com/chriso/node-validator/raw/master/LICENSE"
+ }
+ ]
+}

0 comments on commit a872d7a

Please sign in to comment.