-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Exported validation function for request params validation
- Loading branch information
1 parent
ac50dd1
commit 0cf9c51
Showing
7 changed files
with
168 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
'use strict'; | ||
import Joi from '@hapi/joi' | ||
import ValidationError from './validation-error' | ||
import { assignIn, find, defaults } from 'lodash' | ||
|
||
const defaultOptions = { | ||
contextRequest: false, | ||
allowUnknownHeaders: true, | ||
allowUnknownBody: true, | ||
allowUnknownQuery: true, | ||
allowUnknownParams: true, | ||
allowUnknownCookies: true, | ||
status: 400, | ||
statusText: 'Bad Request' | ||
}; | ||
let globalOptions = {}; | ||
|
||
// maps the corresponding request object to an `express-validation` option | ||
const unknownMap: any = { | ||
headers: 'allowUnknownHeaders', | ||
body: 'allowUnknownBody', | ||
query: 'allowUnknownQuery', | ||
params: 'allowUnknownParams', | ||
cookies: 'allowUnknownCookies' | ||
}; | ||
|
||
export default function expressValidation(schema: any) { | ||
if (!schema) throw new Error('Please provide a validation schema'); | ||
|
||
return async (req: any, res: any, next: any) => { | ||
const errors: any = []; | ||
|
||
// Set default options | ||
const options = defaults({}, schema.options || {}, globalOptions, defaultOptions); | ||
|
||
// NOTE: mutates `errors` | ||
const requestInputType = ['headers', 'body', 'query', 'params', 'cookies']; | ||
|
||
// tslint:disable-next-line:prefer-for-of | ||
for (let index = 0; index < requestInputType.length; index++) { | ||
const key = requestInputType[index]; | ||
const allowUnknown = options[unknownMap[key]]; | ||
const entireContext = options.contextRequest ? req : null; | ||
if (schema[key]) { | ||
await validate(errors, req[key], schema[key], key, allowUnknown, entireContext); | ||
} | ||
} | ||
if (errors && errors.length === 0) return next(); | ||
|
||
// tslint:disable-next-line:new-parens | ||
return next(new (ValidationError as any)(errors, options)); | ||
}; | ||
}; | ||
|
||
exports.ValidationError = ValidationError; | ||
|
||
exports.options = (opts: any) => { | ||
if (!opts) { | ||
globalOptions = {}; | ||
return; | ||
} | ||
|
||
globalOptions = defaults({}, globalOptions, opts); | ||
}; | ||
|
||
/** | ||
* validate checks the current `Request` for validations | ||
* NOTE: mutates `request` in case the object is valid. | ||
*/ | ||
async function validate(errObj: any, request: any, schema: any, location: any, allowUnknown: any, context: any) { | ||
if (!request || !schema) return; | ||
|
||
const joiOptions = { | ||
context: context || request, | ||
allowUnknown, | ||
abortEarly: false | ||
}; | ||
|
||
const { | ||
error, | ||
value | ||
} = await Joi.object(schema).validate(request, joiOptions); | ||
|
||
const errors = error; | ||
if (!errors || errors.details.length === 0) { | ||
assignIn(request, value); // joi responses are parsed into JSON | ||
return; | ||
} | ||
// tslint:disable-next-line:no-shadowed-variable | ||
errors.details.forEach((error) => { | ||
const errorExists = find(errObj, (item)=> { | ||
if (item && item.field === error.path && item.location === location) { | ||
item.messages.push(error.message); | ||
item.types.push(error.type); | ||
return item; | ||
} | ||
return; | ||
}); | ||
|
||
if (!errorExists) { | ||
errObj.push({ | ||
field: error.path, | ||
location, | ||
messages: [error.message], | ||
types: [error.type] | ||
}); | ||
} | ||
|
||
}); | ||
return errObj; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
import { map, flatten } from 'lodash' | ||
|
||
function ValidationError (this: any, errors : any, options: any) { | ||
this.name = 'ValidationError'; | ||
this.message = 'validation error'; | ||
this.errors = errors; | ||
this.flatten = options.flatten; | ||
this.status = options.status; | ||
this.statusText = options.statusText; | ||
}; | ||
ValidationError.prototype = Object.create(Error.prototype); | ||
|
||
ValidationError.prototype.toString = function () { | ||
return JSON.stringify(this.toJSON()); | ||
}; | ||
|
||
ValidationError.prototype.toJSON = function () { | ||
if (this.flatten) return flatten(map(this.errors, 'messages')); | ||
return { | ||
status: this.status, | ||
statusText: this.statusText, | ||
errors: this.errors | ||
}; | ||
}; | ||
|
||
export default ValidationError; |