node-restify-validation icon indicating copy to clipboard operation
node-restify-validation copied to clipboard

Ability to configure HTTP status code

Open nonplus opened this issue 7 years ago • 0 comments

When validation fails, the library used to return a 400 (Bad Request) response. Then this was changed to 409 (Conflict) for some unexplained reason. https://github.com/z0mt3c/node-restify-validation/commit/9ad8c8751ec0be533ebe7cfff03109d76b6a549b#diff-b83d27d3c5374e6d6de83271284db8c4

There doesn't seem to be 100% agreement on what an appropriate REST validation status code should be, but the most commonly used values are 400 or 422. A 409 response only makes sense in a subset of cases (i.e. parameters are structurally valid but there is a business logic conflict).

I believe I can add a custom error handler via configuration options, but it would be nice if I could simply configure the default response code for failed validation, i.e.:

server.use(restifyValidation.validationPlugin({
    failedValidationResponseCode: 400
}));

I'd be happy to provide a PR for this (let me know what the "failedValidationResponseCode" configuration property should be called).

nonplus avatar Jan 20 '18 15:01 nonplus