express-validator
Advanced tools
Comparing version 4.0.0 to 4.1.0
module.exports = req => decorateAsValidationResult({}, req._validationErrors); | ||
function decorateAsValidationResult(obj, errors) { | ||
let formatter = error => error; | ||
obj.isEmpty = () => !errors.length; | ||
obj.array = ({ onlyFirstError } = {}) => { | ||
const used = {}; | ||
return !onlyFirstError ? errors : errors.filter(error => { | ||
let filteredErrors = !onlyFirstError ? errors : errors.filter(error => { | ||
if (used[error.param]) { | ||
@@ -15,2 +17,4 @@ return false; | ||
}); | ||
return filteredErrors.map(formatter); | ||
}; | ||
@@ -20,3 +24,3 @@ | ||
if (!mapping[error.param]) { | ||
mapping[error.param] = error; | ||
mapping[error.param] = formatter(error); | ||
} | ||
@@ -27,5 +31,10 @@ | ||
obj.formatWith = errorFormatter => { | ||
formatter = errorFormatter; | ||
return obj; | ||
}; | ||
obj.throw = () => { | ||
if (errors.length) { | ||
throw decorateAsValidationResult(new Error('Validation failed'), errors); | ||
throw decorateAsValidationResult(new Error('Validation failed'), errors).formatWith(formatter); | ||
} | ||
@@ -32,0 +41,0 @@ }; |
@@ -85,4 +85,5 @@ var deprecate = require('util').deprecate; | ||
customSanitizers: {}, | ||
errorFormatter: function(param, msg, value) { | ||
errorFormatter: function(param, msg, value, location) { | ||
return { | ||
location, | ||
param: param, | ||
@@ -224,2 +225,10 @@ msg: msg, | ||
/** | ||
* Error formatter delegator to the legacy format | ||
* @param {*} error | ||
*/ | ||
function errorFormatter({ param, msg, value, location }) { | ||
return options.errorFormatter(param, msg, value, location); | ||
} | ||
// _.set sanitizers as prototype methods on corresponding chains | ||
@@ -246,10 +255,11 @@ _.forEach(validator, function(method, methodName) { | ||
const result = validatorCfg.validator(field.value, ...validatorCfg.options); | ||
const errorObj = options.errorFormatter( | ||
utils.formatParamOutput(field.path), | ||
utils.replaceArgs( | ||
const errorObj = { | ||
location: field.location, | ||
value: field.value, | ||
param: utils.formatParamOutput(field.path), | ||
msg: utils.replaceArgs( | ||
validatorCfg.message || context.message || 'Invalid value', | ||
[field.value, ...validatorCfg.options] | ||
), | ||
field.value | ||
); | ||
) | ||
}; | ||
@@ -290,3 +300,3 @@ if (result && result.then) { | ||
var result = validationResult(req); | ||
var result = validationResult(req).formatWith(errorFormatter); | ||
if (result.isEmpty()) { | ||
@@ -314,3 +324,3 @@ return false; | ||
return Promise.all(req._asyncValidationErrors).then(() => { | ||
return validationResult(req); | ||
return validationResult(req).formatWith(errorFormatter); | ||
}); | ||
@@ -317,0 +327,0 @@ }; |
@@ -14,3 +14,3 @@ { | ||
], | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"homepage": "https://github.com/ctavan/express-validator", | ||
@@ -17,0 +17,0 @@ "license": "MIT", |
@@ -26,3 +26,2 @@ # express-validator | ||
## Installation | ||
``` | ||
@@ -32,2 +31,4 @@ npm install express-validator | ||
Also make sure that you have Node.js 6 or newer in order to use it. | ||
## Usage | ||
@@ -224,3 +225,3 @@ > The version 3 style of doing validations is still available. | ||
Each error returned by `.array()` and `.mapped()` methods have the following format: | ||
Each error returned by `.array()` and `.mapped()` methods have the following format by default: | ||
@@ -244,2 +245,7 @@ ```js | ||
### `.formatWith(formatter)` | ||
- `formatter(error)`: the function to use to format when returning errors. | ||
The `error` argument is an object in the format of `{ location, msg, param, value, nestedErrors }`, as described above. | ||
> *Returns:* this validation result instance | ||
### `.array([options])` | ||
@@ -290,3 +296,3 @@ - `options` *(optional)*: an object of options. Defaults to `{ onlyFirstError: false }` | ||
### Middleware options | ||
- `errorFormatter (param, msg, value)`: a function that formats the error objects before returning them to your route handlers. | ||
- `errorFormatter (param, msg, value, location)`: a function that formats the error objects before returning them to your route handlers. | ||
- `customValidators`: an object where you can specify custom validators. | ||
@@ -293,0 +299,0 @@ The key will be the name of the validator, while the value is the validation function, receiving the value and any option. |
@@ -121,2 +121,3 @@ export type URLProtocol = 'http' | 'https' | 'ftp' | ||
not(): this; | ||
exists(): this; | ||
optional(options?: Options.OptionalOptions): this; | ||
@@ -140,2 +141,7 @@ withMessage(message: string): this; | ||
/** | ||
* @return The current validation result instance | ||
*/ | ||
formatWith(formatter: ErrorFormatter): this; | ||
/** | ||
* @return All errors for all validated parameters will be included, unless you specify that you want only the first | ||
@@ -163,2 +169,6 @@ * error of each param by invoking `result.useFirstErrorOnly()`. | ||
export interface ErrorFormatter { | ||
(error: { location: Location, param: string, msg: any, value: any }): any; | ||
} | ||
declare namespace Options { | ||
@@ -169,3 +179,3 @@ | ||
customSanitizers?: { [sanitizername: string]: (value: any) => any } | ||
errorFormatter?: (param?: string, msg?: string, value?: any) => any | ||
errorFormatter?: (param?: string, msg?: string, value?: any, location?: Location) => any | ||
} | ||
@@ -172,0 +182,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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
67621
1207
432
0