Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial commit

  • Loading branch information...
commit d7c459fd06d3bd8e2688f164dd51528f38c9c833 0 parents
Cameron Bytheway camshaft authored

Showing 4 changed files with 108 additions and 0 deletions. Show diff stats Hide diff stats

  1. +2 0  .gitignore
  2. +40 0 README.md
  3. +40 0 index.js
  4. +26 0 package.json
2  .gitignore
... ... @@ -0,0 +1,2 @@
  1 +node_modules
  2 +npm-debug.log
40 README.md
Source Rendered
... ... @@ -0,0 +1,40 @@
  1 +# architect-validator
  2 +
  3 +[validator](https://github.com/chriso/node-validator) plugin for
  4 +[architect](https://github.com/c9/architect)
  5 +
  6 +## Config Format
  7 +
  8 +```json
  9 +{
  10 + "packagePath": "./node_modules/architect-validator",
  11 + "sanitize": true
  12 +}
  13 +```
  14 +
  15 +## Usage
  16 +
  17 +Validate that ssssshtuff
  18 +
  19 +```js
  20 +module.exports = function (options, imports, register) {
  21 +
  22 + var validator = imports.validator;
  23 +
  24 + var obj = {
  25 + email: "this isn't an email",
  26 + name: "My name can be whatever"
  27 + }
  28 +
  29 + validator(obj, function(key, value){
  30 + // throw an exception or return false
  31 + if (key == 'email') {
  32 + return isEmail(value);
  33 + }
  34 + }, function(errors){
  35 + // Errors come back as a list of kv's
  36 + // errors == [{key: 'email', "My name can be whatever"}]
  37 + });
  38 +
  39 +};
  40 +```
40 index.js
... ... @@ -0,0 +1,40 @@
  1 +
  2 +module.exports = function(options, imports, register) {
  3 +
  4 + var _ = require('underscore');
  5 + var sanitizer = require('validator').sanitize;
  6 +
  7 + options = _.defaults(options, {sanitize: true});
  8 +
  9 + var validate = function(data, reduce, done, sanitize) {
  10 +
  11 + if (_.isUndefined(sanitize)) {
  12 + sanitize = options.sanitize;
  13 + }
  14 +
  15 + var errors =
  16 + _.chain(data)
  17 + .map(function(value, key) {
  18 + if(sanitize) {
  19 + value = sanitizer(value).xss();
  20 + }
  21 + return {key: key, value: value};
  22 + })
  23 + .reduce(function(memo, input) {
  24 + try {
  25 + var result = reduce(input.key, input.value);
  26 + if (result == false) memo.push(input);
  27 + }
  28 + catch(e) {
  29 + if (!memo) memo=[];
  30 + memo.push(input);
  31 + }
  32 + return memo;
  33 + }, false)
  34 + .value();
  35 +
  36 + done(errors);
  37 + }
  38 +
  39 + register(null, {validator: validate});
  40 +}
26 package.json
... ... @@ -0,0 +1,26 @@
  1 +{
  2 + "name": "architect-validator",
  3 + "description": "Validator for Architect",
  4 + "version": "0.0.1",
  5 + "main": "index.js",
  6 + "author": {
  7 + "name": "Nujii Labs",
  8 + "email": "developer@nujii.com"
  9 + },
  10 + "contributors": [
  11 + { "name": "Cameron Bytheway", "email": "cameron@nujii.com" }
  12 + ],
  13 + "repository": {
  14 + "type": "git",
  15 + "url": "git://github.com/nujii/architect-validator.git"
  16 + },
  17 + "license": "MIT",
  18 + "dependencies": {
  19 + "validator": "~0.4.11",
  20 + "underscore": "~1.3.3"
  21 + },
  22 + "plugin": {
  23 + "provides": ["validator"],
  24 + "consumes": []
  25 + }
  26 +}

0 comments on commit d7c459f

Please sign in to comment.
Something went wrong with that request. Please try again.