-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add content type validation header (#27)
- Add content type validation header (optional check) - Support for multiple types (by swagger spec) - Support check only if body exists. - Refactor Error - Fix NSP badge
- Loading branch information
Showing
13 changed files
with
331 additions
and
107 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const Ajv = require('ajv'); | ||
|
||
module.exports = { | ||
compile: function contentTypeValidation(schema) { | ||
const regex = buildContentTypeRegex(schema.types); | ||
return function contentValidation(data) { | ||
contentValidation.errors = []; | ||
if (Number(data['content-length']) > 0) { | ||
if (!regex.test(data['content-type'])) { | ||
contentValidation.errors.push(new Ajv.ValidationError({ | ||
keyword: 'content-type', | ||
message: 'content-type must be one of ' + schema.types, | ||
params: { pattern: schema.pattern, types: schema.types, 'content-type': data['content-type'], 'content-length': data['content-length'] } | ||
})); | ||
return false; | ||
} | ||
} | ||
return true; | ||
}; | ||
}, | ||
errors: true | ||
}; | ||
|
||
function buildContentTypeRegex(contentTypes) { | ||
let pattern = ''; | ||
contentTypes.forEach(type => { | ||
pattern += `(${type.replace(/\//g, '\\/')}.*\\s*\\S*)|`; | ||
}); | ||
|
||
return new RegExp(pattern.substring(0, pattern.length - 1)); | ||
} |
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,74 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Represent an input validation error | ||
* errors field will include the `ajv` error | ||
* @class InputValidationError | ||
* @extends {Error} | ||
*/ | ||
class InputValidationError extends Error { | ||
constructor(errors, path, method, options) { | ||
super('Input validation error'); | ||
|
||
if (options.beautifyErrors && options.firstError) { | ||
this.errors = this.parseAjvError(errors[0], path, method); | ||
} else if (options.beautifyErrors) { | ||
this.errors = this.parseAjvErrors(errors, path, method); | ||
} else { | ||
this.errors = errors; | ||
} | ||
} | ||
|
||
parseAjvErrors(errors, path, method) { | ||
var parsedError = []; | ||
errors.forEach(function (error) { | ||
parsedError.push(this.parseAjvError(error, path, method)); | ||
}, this); | ||
|
||
return parsedError; | ||
} | ||
|
||
parseAjvError(error, path, method) { | ||
if (error.dataPath.startsWith('.header')) { | ||
error.dataPath = error.dataPath.replace('.', ''); | ||
error.dataPath = error.dataPath.replace('[', '/'); | ||
error.dataPath = error.dataPath.replace(']', ''); | ||
error.dataPath = error.dataPath.replace('\'', ''); | ||
error.dataPath = error.dataPath.replace('\'', ''); | ||
} | ||
|
||
if (error.dataPath.startsWith('.path')) { | ||
error.dataPath = error.dataPath.replace('.', ''); | ||
error.dataPath = error.dataPath.replace('.', '/'); | ||
} | ||
|
||
if (error.dataPath.startsWith('.query')) { | ||
error.dataPath = error.dataPath.replace('.', ''); | ||
error.dataPath = error.dataPath.replace('.', '/'); | ||
} | ||
|
||
if (error.dataPath.startsWith('.')) { | ||
error.dataPath = error.dataPath.replace('.', 'body/'); | ||
} | ||
|
||
if (error.dataPath.startsWith('[')) { | ||
error.dataPath = `body/${error.dataPath}`; | ||
} | ||
|
||
if (error.dataPath === '') { | ||
error.dataPath = 'body'; | ||
} | ||
|
||
if (error.keyword === 'enum') { | ||
error.message += ` [${error.params.allowedValues.toString()}]`; | ||
} | ||
|
||
if (error.validation) { | ||
error.message = error.errors.message; | ||
} | ||
|
||
return `${error.dataPath} ${error.message}`; | ||
} | ||
} | ||
|
||
module.exports = InputValidationError; |
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
File renamed without changes.
Oops, something went wrong.