koa-async-validator
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -19,11 +19,2 @@ "use strict"; | ||
return `${input}`; | ||
}; | ||
_validator.default.toDate = date => { | ||
if (Object.prototype.toString.call(date) === '[object Date]') { | ||
return date; | ||
} | ||
const parsedDate = Date.parse(date); | ||
return !Number.isNaN(parsedDate) ? new Date(parsedDate) : null; | ||
}; // validators and sanitizers not prefixed with is/to | ||
@@ -33,3 +24,3 @@ | ||
const additionalValidators = ['contains', 'equals', 'matches']; | ||
const additionalSanitizers = ['trim', 'ltrim', 'rtrim', 'escape', 'stripLow', 'whitelist', 'blacklist', 'normalizeEmail']; | ||
const additionalSanitizers = ['trim', 'ltrim', 'rtrim', 'escape', 'unescape', 'stripLow', 'whitelist', 'blacklist', 'normalizeEmail']; | ||
/** | ||
@@ -79,2 +70,8 @@ * find location of param | ||
}; | ||
const defaultErrorFormatter = (param, msg, value) => ({ | ||
param, | ||
msg, | ||
value | ||
}); | ||
/** | ||
@@ -94,3 +91,4 @@ * Initializes a chain of validators | ||
constructor(param, failMsg, ctx, location, { | ||
errorFormatter | ||
errorFormatter = defaultErrorFormatter, | ||
skipValidationOnFirstError = false | ||
}) { | ||
@@ -104,2 +102,3 @@ const context = location === 'body' ? ctx.request[location] : ctx[location]; | ||
this.ctx = ctx; | ||
this.skipValidationOnFirstError = skipValidationOnFirstError; | ||
this.lastError = null; // used by withMessage to get the values of the last error | ||
@@ -287,2 +286,6 @@ | ||
}; | ||
if (this.skipValidationOnFirstError) { | ||
this.skipValidating = true; | ||
} | ||
} else { | ||
@@ -345,11 +348,3 @@ this.lastError = null; | ||
} | ||
/** | ||
* Adds validation methods to request object via express middleware | ||
* | ||
* @method koaValidator | ||
* @param {object} options | ||
* @return {function} middleware | ||
*/ | ||
// _.set validators and sanitizers as prototype methods on corresponding chains | ||
} // _.set validators and sanitizers as prototype methods on corresponding chains | ||
@@ -366,8 +361,10 @@ | ||
}); | ||
/** | ||
* Adds validation methods to request object via express middleware | ||
* | ||
* @method koaValidator | ||
* @param {object} options | ||
* @return {function} middleware | ||
*/ | ||
const defaultErrorFormatter = (param, msg, value) => ({ | ||
param, | ||
msg, | ||
value | ||
}); | ||
@@ -377,10 +374,4 @@ const koaValidator = ({ | ||
customSanitizers = {}, | ||
errorFormatter = defaultErrorFormatter, | ||
...rest | ||
...options | ||
} = {}) => { | ||
const options = { | ||
errorFormatter, | ||
...rest | ||
}; | ||
_lodash.default.forEach(customValidators, (method, customValidatorName) => { | ||
@@ -387,0 +378,0 @@ ValidatorChain.prototype[customValidatorName] = makeValidator(customValidatorName, customValidators); |
@@ -6,3 +6,3 @@ { | ||
"contributors": [], | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"homepage": "https://github.com/carlitux/koa-async-validator", | ||
@@ -9,0 +9,0 @@ "license": "MIT", |
@@ -105,4 +105,11 @@ # koa-async-validator | ||
### Middleware Options | ||
####`skipValidationOnFirstError` | ||
_{ 'skipValidationOnFirstError': boolean = false, ... }_ | ||
The `skipValidationOnFirstError` option is set default to `false` that works as before, runs all validations and save the errors. | ||
If set to `true` will skip next validations when the first sync validation fails. All async validation do not follow this rule. Each validation runs in order that was defiened. | ||
####`errorFormatter` | ||
_function(param,msg,value)_ | ||
_{ 'errorFormatter': function(param,msg,value), ... }_ | ||
@@ -109,0 +116,0 @@ The `errorFormatter` option can be used to specify a function that can be used to format the objects that populate the error array that is returned in `ctx.validationErrors()`. It should return an `Object` that has `param`, `msg`, and `value` keys defined. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
654975
463
0
376