express-form
Advanced tools
Comparing version 0.5.3 to 0.6.0
249
lib/form.js
@@ -1,4 +0,1 @@ | ||
var validator = require("validator"), | ||
object = require("object-additions").object; | ||
/*! | ||
@@ -10,152 +7,114 @@ * Express - Form | ||
/** | ||
* camelize(str): -> String | ||
* - str (String): The string to make camel-case. | ||
* | ||
* Converts dash-separated words into camelCase words. Cribbed from Prototype.js. | ||
* | ||
* field-name -> fieldName | ||
* -field-name -> FieldName | ||
**/ | ||
function camelize(str) { | ||
return (str || "").replace(/-+(.)?/g, function(match, chr) { | ||
return chr ? chr.toUpperCase() : ''; | ||
}); | ||
} | ||
var utils = require("./utils") | ||
, Field = require("./field"); | ||
var Filter = require("./filter"); | ||
var Validator = require("./validator"); | ||
var dataSources = ["body", "query", "params"], | ||
autoLocals = true, | ||
flashErrors = true, | ||
SUPPORTS_REQUEST_FLASH = undefined; // Might not if used outside Express. | ||
function form() { | ||
var routines = Array.prototype.slice.call(arguments); | ||
return function(req, res, next) { | ||
if (undefined === SUPPORTS_REQUEST_FLASH) { | ||
SUPPORTS_REQUEST_FLASH = "flash" in req && object.isFunction(req.flash); | ||
} | ||
if (!req.form) { | ||
req.form = {}; | ||
} | ||
dataSources.forEach(function(source) { | ||
if (req[source] && !object.isString(req[source])) { | ||
Object.keys(req[source]).forEach(function(name) { | ||
if (autoLocals && source == "body") { | ||
if (!res.locals) res.locals = {}; | ||
// Make all params locals, so they're accessible from the view. | ||
// If they have dashes in their names, camelize them so they don't | ||
// break ejs: <%= field-name %> becomes <%= fieldName %> in your views. | ||
if (typeof res.locals == 'function') { // Express 2.0 Support | ||
res.local(camelize(name),req[source][name]); | ||
} else { | ||
res.locals[camelize(name)] = req[source][name]; | ||
} | ||
} | ||
// Copy data as is from source to the form object. | ||
req.form[name] = req[source][name]; | ||
}); | ||
} | ||
}); | ||
var map = {}; | ||
var flashed = {}; | ||
Object.defineProperties(req.form, { | ||
"errors": { | ||
value: [], | ||
enumerable: false | ||
}, | ||
"getErrors": { | ||
value: function(name) { | ||
return map[name] || []; | ||
}, | ||
enumerable: false | ||
}, | ||
"isValid": { | ||
get: function() { | ||
return this.errors.length == 0; | ||
}, | ||
enumerable: false | ||
}, | ||
"flashErrors": { | ||
value: function() { | ||
if (!SUPPORTS_REQUEST_FLASH) return; | ||
this.errors.forEach(function(error) { | ||
if (!flashed[error]) { | ||
flashed[error] = true; | ||
req.flash("error", error); | ||
} | ||
}); | ||
}, | ||
enumerable: false | ||
} | ||
}); | ||
routines.forEach(function(routine) { | ||
var result = routine.run(req.form); | ||
if (Array.isArray(result) && result.length) { | ||
var errors = req.form.errors = req.form.errors || [], | ||
name = routine.name; | ||
map[name] = map[name] || []; | ||
result.forEach(function(error) { | ||
errors.push(error); | ||
map[name].push(error); | ||
}); | ||
} | ||
}); | ||
if (flashErrors) req.form.flashErrors(); | ||
if (next) next(); | ||
}; | ||
var routines = Array.prototype.slice.call(arguments) | ||
, options = form._options; | ||
return function (req, res, next) { | ||
var map = {} | ||
, flashed = {} | ||
, mergedSource = {}; | ||
if (!req.form) req.form = {}; | ||
options.dataSources.forEach(function (source) { | ||
utils.merge(mergedSource, req[source]); | ||
}); | ||
if (options.passThrough) req.form = utils.clone(mergedSource); | ||
if (options.autoLocals) { | ||
for (var prop in req.body) { | ||
if (!req.body.hasOwnProperty(prop)) continue; | ||
if (typeof res.locals === "function") { // Express 2.0 Support | ||
res.local(utils.camelize(prop), req.body[prop]); | ||
} else { | ||
if (!res.locals) res.locals = {}; | ||
res.locals[utils.camelize(prop)] = req.body[prop]; | ||
} | ||
} | ||
} | ||
Object.defineProperties(req.form, { | ||
"errors": { | ||
value: [], | ||
enumerable: false | ||
}, | ||
"getErrors": { | ||
value: function (name) { | ||
if(!name) return map; | ||
return map[name] || []; | ||
}, | ||
enumerable: false | ||
}, | ||
"isValid": { | ||
get: function () { | ||
return this.errors.length === 0; | ||
}, | ||
enumerable: false | ||
}, | ||
"flashErrors": { | ||
value: function () { | ||
if (typeof req.flash !== "function") return; | ||
this.errors.forEach(function (error) { | ||
if (flashed[error]) return; | ||
flashed[error] = true; | ||
req.flash("error", error); | ||
}); | ||
}, | ||
enumerable: false | ||
} | ||
}); | ||
routines.forEach(function (routine) { | ||
var result = routine.run(mergedSource, req.form, options); | ||
if (!Array.isArray(result) || !result.length) return; | ||
var errors = req.form.errors = req.form.errors || [] | ||
, name = routine.name; | ||
map[name] = map[name] || []; | ||
result.forEach(function (error) { | ||
errors.push(error); | ||
map[name].push(error); | ||
}); | ||
}); | ||
if (options.flashErrors) req.form.flashErrors(); | ||
if (next) next(); | ||
} | ||
} | ||
form.filter = function(fieldname) { | ||
return new Filter(fieldname); | ||
form.field = function (property, label) { | ||
return new Field(property, label); | ||
}; | ||
form.validate = function(fieldname, label) { | ||
return new Validator(fieldname, label); | ||
form.filter = form.validate = form.field; | ||
form._options = { | ||
dataSources: ["body", "query", "params"], | ||
autoTrim: false, | ||
autoLocals: true, | ||
passThrough: false, | ||
flashErrors: true | ||
}; | ||
Object.defineProperty(form, "validator", { | ||
get: function() { | ||
console.log("WARNING: `validator()` is deprecated and will be removed soon. Use `validate()` instead."); | ||
return form.validate; | ||
} | ||
}); | ||
form.configure = function (options) { | ||
for (var p in options) { | ||
if (!Array.isArray(options[p]) && p === "dataSources") { | ||
options[p] = [options[p]]; | ||
} | ||
this._options[p] = options[p]; | ||
} | ||
return this; | ||
} | ||
form.configure = function(options) { | ||
if (options) { | ||
if ("dataSources" in options) { | ||
dataSources = []; | ||
var sources = options.dataSources; | ||
if (object.isString(sources)) { | ||
dataSources.push(sources); | ||
} else if (Array.isArray(sources)) { | ||
sources.forEach(function(source) { dataSources.push(source); }); | ||
} | ||
} | ||
if ("autoLocals" in options) { | ||
autoLocals = !!options.autoLocals; | ||
} | ||
if ("flashErrors" in options) { | ||
flashErrors = !!options.flashErrors; | ||
} | ||
} | ||
return this; | ||
}; | ||
module.exports = form; | ||
module.exports = form; |
{ | ||
"author": "Dan Dean <@dandean> (http://dandean.com)", | ||
"name": "express-form", | ||
"description": "Form validation and data filtering for Express", | ||
"version": "0.5.3", | ||
"version": "0.6.0", | ||
"homepage": "http://dandean.github.com/express-form", | ||
"repository": { | ||
"type": "git", | ||
"url": "http://github.com/dandean/express-form.git" | ||
"url": "git://github.com/dandean/express-form.git" | ||
}, | ||
"author": "Dan Dean <me@dandean.com> (http://dandean.com)", | ||
"contributors": [ | ||
{ "name": "Dan Dean", "email": "me@dandean.com" }, | ||
{ "name": "Marc Harter", "email": "wavded@gmail.com" } | ||
"Marc Harter <wavded@gmail.com>", | ||
"Sugarstack <@sugarstack>" | ||
], | ||
@@ -20,4 +20,3 @@ "keywords" : ["form", "validator", "validation", "express"], | ||
}, | ||
"main" : "./index", | ||
"directories": { "lib": "./lib" }, | ||
"main" : "index", | ||
"bugs" : { "web" : "http://github.com/dandean/express-form/issues" }, | ||
@@ -24,0 +23,0 @@ "scripts": { "test": "expresso" }, |
@@ -9,3 +9,3 @@ var assert = require("assert"), | ||
app.configure(function() { | ||
app.use(express.bodyDecoder()); | ||
app.use(express.bodyParser()); | ||
app.use(app.router); | ||
@@ -12,0 +12,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
61266
1438
13