Skip to content

Commit

Permalink
Optimized validator speed, changed params order to reduce misundersta…
Browse files Browse the repository at this point in the history
…ndings
  • Loading branch information
B3rn475 committed Feb 21, 2015
1 parent 1b409ac commit 2041d8c
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 74 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,13 @@ Parameters:

The predefined validators are:

- **equals(comparison, error)** - check if the string matches the comparison.
- **contains(seed, error)** - check if the string contains the seed.
- **matches(pattern [, modifiers], error)** - check if string matches the pattern. Either `matches(/foo/i, error)` or `matches('foo', 'i', error)`.
- **equals(error, comparison)** - check if the string matches the comparison.
- **contains(error, seed)** - check if the string contains the seed.
- **matches(error, pattern [, modifiers])** - check if string matches the pattern. Either `matches(/foo/i)` or `matches('foo', 'i')`.
- **isEmail(error)** - check if the string is an email.
- **isURL([options], error)** - check if the string is an URL. `options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false }`.
- **isFQDN([options], error)** - check if the string is a fully qualified domain name (e.g. domain.com). `options` is an object which defaults to `{ require_tld: true, allow_underscores: false, allow_trailing_dot: false }`.
- **isIP([version], error)** - check if the string is an IP (version 4 or 6).
- **isURL(error, [options])** - check if the string is an URL. `options` is an object which defaults to `{ protocols: ['http','https','ftp'], require_tld: true, require_protocol: false, allow_underscores: false, host_whitelist: false, host_blacklist: false, allow_trailing_dot: false }`.
- **isFQDN(error, [options])** - check if the string is a fully qualified domain name (e.g. domain.com). `options` is an object which defaults to `{ require_tld: true, allow_underscores: false, allow_trailing_dot: false }`.
- **isIP(error, [version])** - check if the string is an IP (version 4 or 6).
- **isAlpha(error)** - check if the string contains only letters (a-zA-Z).
- **isNumeric(error)** - check if the string contains only numbers.
- **isAlphanumeric(error)** - check if the string contains only letters and numbers.
Expand All @@ -128,18 +128,18 @@ The predefined validators are:
- **isUppercase(error)** - check if the string is uppercase.
- **isInt(error)** - check if the string is an integer.
- **isFloat(error)** - check if the string is a float.
- **isDivisibleBy(number, error)** - check if the string is a number that's divisible by another.
- **isDivisibleBy(error, number)** - check if the string is a number that's divisible by another.
- **isNull(error)** - check if the string is null.
- **isLength(min [, max], error)** - check if the string's length falls in a range. Note: this function takes into account surrogate pairs.
- **isByteLength(min [, max], error)** - check if the string's length (in bytes) falls in a range.
- **isUUID([version], error)** - check if the string is a UUID (version 3, 4 or 5).
- **isLength(error, min [, max])** - check if the string's length falls in a range. Note: this function takes into account surrogate pairs.
- **isByteLength(error, min [, max])** - check if the string's length (in bytes) falls in a range.
- **isUUID(error, [version])** - check if the string is a UUID (version 3, 4 or 5).
- **isDate(error)** - check if the string is a date.
- **isAfter([date], error)** - check if the string is a date that's after the specified date (defaults to now).
- **isBefore([date], error)** - check if the string is a date that's before the specified date.
- **isIn(values, error)** - check if the string is in a array of allowed values.
- **isAfter(error, [date])** - check if the string is a date that's after the specified date (defaults to now).
- **isBefore(error, [date])** - check if the string is a date that's before the specified date.
- **isIn(error, values)** - check if the string is in a array of allowed values.
- **isCreditCard(error)** - check if the string is a credit card.
- **isISBN([version], error)** - check if the string is an ISBN (version 10 or 13).
- **isMobilePhone([locale], error)** - check if the string is a mobile phone number, (locale should be locales, like 'zh-CN', currently only support 'zh-CN').
- **isISBN(error, [version])** - check if the string is an ISBN (version 10 or 13).
- **isMobilePhone(error, [locale])** - check if the string is a mobile phone number, (locale should be locales, like 'zh-CN', currently only support 'zh-CN').
- **isJSON(error)** - check if the string is valid JSON (note: uses JSON.parse).
- **isMultibyte(error)** - check if the string contains one or more multibyte chars.
- **isAscii(error)** - check if the string contains ASCII chars only.
Expand Down
123 changes: 65 additions & 58 deletions lib/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,50 +186,54 @@ function Field(name, location) {

// istanbul ignore next
[
'equals',
'contains',
'matches',
'isEmail',
'isURL',
'isFQDN',
'isIP',
'isAlpha',
'isNumeric',
'isAlphanumeric',
'isBase64',
'isHexadecimal',
'isHexColor',
'isLowercase',
'isUppercase',
'isInt',
'isFloat',
'isDivisibleBy',
'isNull',
'isLength',
'isByteLength',
'isUUID',
'isDate',
'isAfter',
'isBefore',
'isIn',
'isCreditCard',
'isISBN',
'isJSON',
'isMultibyte',
'isAscii',
'isFullWidth',
'isHalfWidth',
'isVariableWidth',
'isSurrogatePair',
'isMongoId'
].forEach(function (name) {
Field.prototype[name] = function () {
{ name: 'equals', params: 1 },
{ name: 'contains', params: 1 },
{ name: 'matches', params: 1 },
{ name: 'isEmail', params: 0 },
{ name: 'isURL', params: 0 },
{ name: 'isFQDN', params: 1 },
{ name: 'isIP', params: 0 },
{ name: 'isAlpha', params: 0 },
{ name: 'isNumeric', params: 0 },
{ name: 'isAlphanumeric', params: 0 },
{ name: 'isBase64', params: 0 },
{ name: 'isHexadecimal', params: 0 },
{ name: 'isHexColor', params: 0 },
{ name: 'isLowercase', params: 0 },
{ name: 'isUppercase', params: 0 },
{ name: 'isInt', params: 0 },
{ name: 'isFloat', params: 0 },
{ name: 'isDivisibleBy', params: 1 },
{ name: 'isNull', params: 0 },
{ name: 'isLength', params: 1 },
{ name: 'isByteLength', params: 1 },
{ name: 'isUUID', params: 1 },
{ name: 'isDate', params: 0 },
{ name: 'isAfter', params: 0 },
{ name: 'isBefore', params: 0 },
{ name: 'isIn', params: 1 },
{ name: 'isCreditCard', params: 0 },
{ name: 'isISBN', params: 0 },
{ name: 'isMobilePhone', params: 0 },
{ name: 'isJSON', params: 0 },
{ name: 'isMultibyte', params: 0 },
{ name: 'isAscii', params: 0 },
{ name: 'isFullWidth', params: 0 },
{ name: 'isHalfWidth', params: 0 },
{ name: 'isVariableWidth', params: 0 },
{ name: 'isSurrogatePair', params: 0 },
{ name: 'isMongoId', params: 0 }
].forEach(function (v) {
var method = validator[v.name];
Field.prototype[v.name] = function () {
if (arguments.length === 0) {
throw new Error('Missing error param');
}
var args = Array.prototype.slice.apply(arguments),
error = args.pop(),
method = validator[name];
var args = Array.prototype.slice.apply(arguments),
error = args.shift();
if (args.length < v.params) {
throw new Error('Missing params');
}
return this.validate(function (str, next) {
if (!method.apply(validator, [str].concat(args))) {
return next(undefined, error);
Expand All @@ -241,23 +245,26 @@ function Field(name, location) {

// istanbul ignore next
[
'toString',
'toDate',
'toFloat',
'toInt',
'toBoolean',
'trim',
'ltrim',
'rtrim',
'escape',
'stripLow',
'whitelist',
'blacklist',
'normalizeEmail'
].forEach(function (name) {
Field.prototype[name] = function () {
var args = Array.prototype.slice.apply(arguments),
method = validator[name];
{ name: 'toString', params: 0 },
{ name: 'toDate', params: 0 },
{ name: 'toFloat', params: 0 },
{ name: 'toInt', params: 0 },
{ name: 'toBoolean', params: 0 },
{ name: 'trim', params: 0 },
{ name: 'ltrim', params: 0 },
{ name: 'rtrim', params: 0 },
{ name: 'escape', params: 0 },
{ name: 'stripLow', params: 0 },
{ name: 'whitelist', params: 1 },
{ name: 'blacklist', params: 1 },
{ name: 'normalizeEmail', params: 0 }
].forEach(function (s) {
var method = validator[s.name];
Field.prototype[s.name] = function () {
var args = Array.prototype.slice.apply(arguments);
if (args.length < s.params) {
throw new Error('Missing params');
}
return this.sanitize(function (value, next) {
next(undefined, method.apply(validator, [value].concat(arguments)));
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "express-formwork",
"version": "0.1.1",
"version": "0.1.2",
"description": "",
"keywords": [
"express",
Expand Down

0 comments on commit 2041d8c

Please sign in to comment.