Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

express-form

Package Overview
Dependencies
Maintainers
0
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-form - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

test/._express.test.js

2

._index.js

@@ -1,4 +0,4 @@

Mac OS X  2��ATTR�����!�!com.macromates.caret{
Mac OS X  2��ATTRΪ;��!�!com.macromates.caret{
column = 0;
line = 1;
}

@@ -1,4 +0,4 @@

Mac OS X  2��ATTR�����"�"com.macromates.caret{
Mac OS X  2��ATTRΪ@��"�"com.macromates.caret{
column = 17;
line = 3;
}

@@ -1,4 +0,4 @@

Mac OS X  2��ATTR�����$�$com.macromates.caret{
column = 41;
line = 239;
Mac OS X  2��ATTRΪB��#�#com.macromates.caret{
column = 4;
line = 292;
}

@@ -1,4 +0,4 @@

Mac OS X  2��ATTR�����#�#com.macromates.caret{
Mac OS X  2��ATTRΪE��#�#com.macromates.caret{
column = 79;
line = 23;
}

@@ -1,4 +0,4 @@

Mac OS X  2��ATTR�����"�"com.macromates.caret{
Mac OS X  2��ATTRΪN��"�"com.macromates.caret{
column = 0;
line = 13;
}

@@ -1,4 +0,4 @@

Mac OS X  2��ATTR�����#�#com.macromates.caret{
column = 43;
line = 42;
Mac OS X  2��ATTRΪP��#�#com.macromates.caret{
column = 16;
line = 37;
}

@@ -1,4 +0,4 @@

Mac OS X  2#UATTR���U���#com.macromates.caret�vcom.macromates.foldings{
column = 30;
line = 71;
}x���R��j8+�(?W�V���.�����W4@����f&�Z�Ya��9a�,�1Ȓ� �0��ؐ����1�̘��L�1ʜW��*sK"�� *���?C Kb��� �&U��x
Mac OS X  2��ATTRΪR��#�#com.macromates.caret{
column = 24;
line = 10;
}

@@ -31,2 +31,23 @@ var validator = require("validator"),

var map = {};
Object.defineProperties(req.form, {
"errors": {
value: [],
enumerable: form
},
"getErrors": {
value: function(name) {
return map[name] || [];
},
enumerable: false
},
"isValid": {
get: function() {
return this.errors.length == 0;
},
enumerable: false
}
});
routines.forEach(function(routine) {

@@ -36,14 +57,14 @@ var result = routine.run(req.form);

if (Array.isArray(result) && result.length) {
var errors = req.form.errors = req.form.errors || [];
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);
});
}
});
Object.defineProperty(req.form, "isValid", {
value: req.form.errors === undefined,
enumerable: false
});
if (next) next();

@@ -50,0 +71,0 @@ };

@@ -11,2 +11,4 @@ var validator = require("validator"),

this.name = fieldname;
this.add = function(func) {

@@ -38,2 +40,19 @@ stack.push(func);

const MESSAGES = {
isEmail: "%s is not an email address",
isUrl: "%s is not a URL",
isIP: "%s is not an IP address",
isAlpha: "%s contains non-letter characters",
isAlphanumeric: "%s contains non alpha-numeric characters",
isNumeric: "%s is not numeric",
isLowercase: "%s contains uppercase letters",
isUppercase: "%s contains lowercase letters",
isInt: "%s is not an integer",
notNull: "%s is null",
isNull: "%s is not null",
notEmpty: "%s has no value or is only whitespace",
contains: "%s does not contain required characters",
notContains: "%s contains invalid characters"
};
Object.keys(ValidatorPrototype).forEach(function(name) {

@@ -50,3 +69,2 @@ if (name.match(/^(equals|check|validate|assert|error|len|isNumeric|isDecimal|isFloat|regex|notRegex|is|not)$/))

switch(name) {
case "equals":
case "contains":

@@ -60,2 +78,4 @@ case "notContains":

}
message = message || MESSAGES[name];

@@ -79,3 +99,3 @@ return this.add(function(value) {

if (value != other) {
throw new Error(message || "Not equal");
throw new Error(message || "%s does not equal " + String(other));
}

@@ -90,3 +110,3 @@ });

} else {
throw new Error(message || "Invalid number");
throw new Error(message || "%s is not a number");
}

@@ -101,3 +121,3 @@ });

} else {
throw new Error(message || "Invalid decimal");
throw new Error(message || "%s is not a decimal");
}

@@ -136,3 +156,3 @@ });

if (pattern.test(value) === false) {
throw new Error(message || "Invalid characters");
throw new Error(message || "%s has invalid characters");
}

@@ -169,3 +189,3 @@ });

if (pattern.test(value) === true) {
throw new Error(message || "Invalid characters");
throw new Error(message || "%s has invalid characters");
}

@@ -178,3 +198,3 @@ });

if (object.isUndefined(value) || value == null || value === '' || value == placeholderValue) {
throw new Error(message || "Missing value");
throw new Error(message || "%s is required");
}

@@ -187,3 +207,3 @@ });

if (value.toString().length < length) {
throw new Error(message || "Too short");
throw new Error(message || "%s is too short");
}

@@ -196,3 +216,3 @@ });

if (value.toString().length > length) {
throw new Error(message || "Too long");
throw new Error(message || "%s is too long");
}

@@ -207,3 +227,3 @@ });

} catch (e) {
throw new Error(message || e.message || "Invalid field");
throw new Error(message || e.message || "%s is invalid");
}

@@ -210,0 +230,0 @@ });

{
"name": "express-form",
"description": "Form validation and data filtering for Express",
"version": "0.3.0",
"version": "0.4.0",
"homepage": "https://github.com/dandean/express-form",

@@ -6,0 +6,0 @@ "repository": {

@@ -149,9 +149,9 @@ Express Form provides data filtering and validation as route middleware to your Express applications.

validate("username").required()
// -> "Missing field"
// -> "username is required"
validate("username").required("%s is a required field.")
// -> "username is a required field."
validate("username").required("What is your %s?")
// -> "What is your username?"
validate("username", "Username").required("%s is a required field.")
// -> "Username is a required field."
validate("username", "Username").required("What is your %s?")
// -> "What is your Username?"

@@ -285,8 +285,16 @@

isValid -> Boolean
errors -> Array or undefined
errors -> Array
// Example request handler:
getErrors(name) -> Array
- fieldname (String): The name of the field
Gets all errors for the field with the given name.
Example request handler:
function(req, res) {
if (req.isValid == false) {
console.log(req.errors);
console.log(req.getErrors("username"))
}

@@ -293,0 +301,0 @@ }

@@ -1,4 +0,4 @@

Mac OS X  2��ATTR�����#�#com.macromates.caret{
column = 52;
line = 26;
Mac OS X  2��ATTRΪJ��"�"com.macromates.caret{
column = 0;
line = 47;
}

@@ -1,7 +0,5 @@

Mac OS X  2 RATTR���R���#com.macromates.caret�scom.macromates.foldings{
column = 0;
line = 306;
}x��б
�0 н_�Q���������
�&�w�nr��G2c�9�6n�l;+�iS6-���WZ}پ�W�'��%�������$(-)
BPC����2Tp����VpL+���h�k�t=
Mac OS X  2n�ATTRΪL����"com.macromates.caret��com.macromates.foldings{
column = 36;
line = 5;
}x����
1E���-,2�<�F wA��]EY+�I�Lq����f���η�:�د��t<M˫���2���Sx��s���>�+��r��}P�$��$I��b �J$U�TE�8A5�fd5�R!��@�Ԑ0&�A&�����&@?�He�R�^�'��PDG�8Ǒ8N�q$Nq��>l���=G

@@ -27,3 +27,3 @@ var assert = require("assert"),

assert.strictEqual(req.form.isValid, true);
assert.strictEqual(req.form.errors, undefined);
assert.strictEqual(req.form.errors.length, 0);
res.send(JSON.stringify(req.form));

@@ -30,0 +30,0 @@ }

@@ -16,7 +16,36 @@ var assert = require("assert"),

assert.strictEqual(request.form.isValid, true);
assert["throws"](function() {
request.form.isValid = false;
});
request.form.isValid = false;
assert.strictEqual(request.form.isValid, true);
},
'form : getErrors': function() {
var request = {
body: {
field0: "win",
field1: "fail",
field2: "fail",
field3: "fail"
}
};
form(
validate("field0").equals("win"),
validate("field1").isEmail(),
validate("field2").isEmail().isUrl(),
validate("field3").isEmail().isUrl().isIP()
)(request, {});
assert.equal(request.form.isValid, false);
assert.equal(request.form.errors.length, 6);
assert.equal(request.form.getErrors("field0").length, 0);
assert.equal(request.form.getErrors("field1").length, 1);
assert.equal(request.form.getErrors("field2").length, 2);
assert.equal(request.form.getErrors("field3").length, 3);
},
'form : configure : dataSources': function() {

@@ -23,0 +52,0 @@ form.configure({ dataSources: 'other' });

@@ -10,3 +10,3 @@ var assert = require("assert"),

form(validate("field").isEmail())(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);

@@ -17,3 +17,3 @@ // Failure.

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Invalid email");
assert.equal(request.form.errors[0], "field is not an email address");

@@ -29,3 +29,3 @@ // Failure w/ custom message.

form(validate("field").isEmail())(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);
},

@@ -38,3 +38,3 @@

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Invalid URL");
assert.equal(request.form.errors[0], "field is not a URL");

@@ -50,3 +50,3 @@ // Failure w/ custom message.

form(validate("field").isUrl())(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);
},

@@ -59,3 +59,3 @@

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Invalid IP");
assert.equal(request.form.errors[0], "field is not an IP address");

@@ -71,3 +71,3 @@ // Failure w/ custom message.

form(validate("field").isIP())(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);
},

@@ -80,3 +80,3 @@

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Invalid characters");
assert.equal(request.form.errors[0], "field contains non-letter characters");

@@ -92,3 +92,3 @@ // Failure w/ custom message.

form(validate("field").isAlpha())(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);
},

@@ -101,3 +101,3 @@

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Invalid characters");
assert.equal(request.form.errors[0], "field contains non alpha-numeric characters");

@@ -113,3 +113,3 @@ // Failure w/ custom message.

form(validate("field").isAlphanumeric())(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);
},

@@ -122,3 +122,3 @@

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Invalid number");
assert.equal(request.form.errors[0], "field is not a number");

@@ -146,3 +146,3 @@ // Failure w/ custom message.

)(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);
},

@@ -155,3 +155,3 @@

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Invalid integer");
assert.equal(request.form.errors[0], "field is not an integer");

@@ -167,3 +167,3 @@ // Failure w/ custom message.

form(validate("field").isInt())(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);
},

@@ -176,3 +176,3 @@

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Invalid characters");
assert.equal(request.form.errors[0], "field contains uppercase letters");

@@ -188,3 +188,3 @@ // Failure w/ custom message.

form(validate("field").isLowercase())(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);
},

@@ -197,3 +197,3 @@

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Invalid characters");
assert.equal(request.form.errors[0], "field contains lowercase letters");

@@ -209,3 +209,3 @@ // Failure w/ custom message.

form(validate("field").isUppercase())(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);
},

@@ -219,3 +219,3 @@

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Invalid decimal");
assert.equal(request.form.errors[0], "field is not a decimal");

@@ -232,3 +232,3 @@ // Failure w/ custom message.

form(validate("field").isFloat())(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);
},

@@ -242,3 +242,3 @@

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Invalid characters");
assert.equal(request.form.errors[0], "field is null");

@@ -255,3 +255,3 @@ // Failure w/ custom message.

form(validate("field").notNull())(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);
},

@@ -265,3 +265,3 @@

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Invalid characters");
assert.equal(request.form.errors[0], "field is not null");

@@ -278,3 +278,3 @@ // Failure w/ custom message.

form(validate("field").isNull())(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);
},

@@ -288,3 +288,3 @@

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "String is whitespace");
assert.equal(request.form.errors[0], "field has no value or is only whitespace");

@@ -301,3 +301,3 @@ // Failure w/ custom message.

form(validate("field").notEmpty())(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);
},

@@ -310,3 +310,3 @@

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Not equal");
assert.equal(request.form.errors[0], "field does not equal other");

@@ -322,3 +322,3 @@ // Failure w/ custom message.

form(validate("field").equals("value"))(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);

@@ -335,3 +335,3 @@

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Not equal");
assert.equal(request.form.errors[0], "field1 does not equal value2");

@@ -346,3 +346,3 @@ // Success

form(validate("field1").equals("field::field2"))(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);
},

@@ -355,3 +355,3 @@

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Invalid characters");
assert.equal(request.form.errors[0], "field does not contain required characters");

@@ -367,3 +367,3 @@ // Failure w/ custom message.

form(validate("field").contains("alu"))(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);
},

@@ -376,3 +376,3 @@

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Invalid characters");
assert.equal(request.form.errors[0], "field contains invalid characters");

@@ -388,3 +388,3 @@ // Failure w/ custom message.

form(validate("field").notContains("win"))(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);
},

@@ -404,3 +404,3 @@

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Invalid characters");
assert.equal(request.form.errors[0], "field has invalid characters");

@@ -417,3 +417,3 @@ // Failure: RegExp with custom message.

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Invalid characters");
assert.equal(request.form.errors[0], "field has invalid characters");

@@ -423,3 +423,3 @@ // Success: String with modifiers

form(validate("field").regex("^VALUE$", "i"))(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);

@@ -442,3 +442,3 @@ // Failure: String with custom message

form(validate("field").regex(/^value$/))(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);
},

@@ -458,3 +458,3 @@

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Invalid characters");
assert.equal(request.form.errors[0], "field has invalid characters");

@@ -471,3 +471,3 @@ // Failure: RegExp with custom message.

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Invalid characters");
assert.equal(request.form.errors[0], "field has invalid characters");

@@ -477,3 +477,3 @@ // Success: String with modifiers

form(validate("field").notRegex("^win$", "i"))(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);

@@ -495,3 +495,3 @@ // Failure: String with custom message

form(validate("field").notRegex(/^win$/))(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);
},

@@ -504,3 +504,3 @@

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Too short");
assert.equal(request.form.errors[0], "field is too short");

@@ -516,3 +516,3 @@ // Failure w/ custom message.

form(validate("field").minLength(1))(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);
},

@@ -525,3 +525,3 @@

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Too long");
assert.equal(request.form.errors[0], "field is too long");

@@ -537,3 +537,3 @@ // Failure w/ custom message.

form(validate("field").maxLength(5))(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);
},

@@ -547,3 +547,3 @@

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Missing value");
assert.equal(request.form.errors[0], "field is required");

@@ -560,3 +560,3 @@ // Failure w/ placeholder value and custom message.

form(validate("field").required())(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);
},

@@ -572,3 +572,3 @@

assert.equal(request.form.errors.length, 1);
assert.equal(request.form.errors[0], "Invalid field");
assert.equal(request.form.errors[0], "field is invalid");

@@ -596,4 +596,4 @@ // Failure w/ custom message.

form(validate("field").custom(function(validate) {}))(request, {});
assert.equal(request.form.errors, undefined);
assert.equal(request.form.errors.length, 0);
}
};

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc