-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement middleware for schema validations (#306)
* feat: Implement middleware for schema validations * chore: Re add schema validations * chore: Add tests * chore: Remove schema test
- Loading branch information
1 parent
280cc80
commit 66d68ed
Showing
6 changed files
with
129 additions
and
33 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
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,90 @@ | ||
import supertest from 'supertest' | ||
import express from 'express' | ||
import { Schema } from 'ajv' | ||
import { buildURL } from '../../spec/utils' | ||
import { app } from '../server' | ||
import { withSchemaValidation } from './schemaValidator' | ||
|
||
const schema: Schema = Object.freeze({ | ||
type: 'object', | ||
properties: { | ||
id: { type: 'string' }, | ||
}, | ||
additionalProperties: false, | ||
required: ['id'], | ||
}) | ||
|
||
const simpleResponseHandler = (_: express.Request, res: express.Response) => { | ||
res.status(200).end() | ||
} | ||
|
||
app | ||
.getRouter() | ||
.post('/test', withSchemaValidation(schema), simpleResponseHandler) | ||
const server = supertest(app.getApp()) | ||
|
||
let data: any | ||
describe('when posting invalid data', () => { | ||
beforeEach(() => { | ||
data = { id: 234 } | ||
}) | ||
|
||
it('should respond with a 400 and a detailed description of the validation errors', () => { | ||
return server | ||
.post(buildURL('/test')) | ||
.send(data) | ||
.expect(400) | ||
.then((response) => { | ||
expect(response.body).toEqual({ | ||
error: 'Invalid request body', | ||
data: [ | ||
{ | ||
dataPath: '/id', | ||
keyword: 'type', | ||
message: 'should be string', | ||
params: { type: 'string' }, | ||
schemaPath: '#/properties/id/type', | ||
}, | ||
], | ||
ok: false, | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('when posting no data', () => { | ||
it('should respond with a 400 and a detailed description of the validation errors', () => { | ||
return server | ||
.post(buildURL('/test')) | ||
.expect(400) | ||
.then((response) => { | ||
expect(response.body).toEqual({ | ||
error: 'Invalid request body', | ||
data: [ | ||
{ | ||
dataPath: '', | ||
keyword: 'required', | ||
message: "should have required property 'id'", | ||
params: { missingProperty: 'id' }, | ||
schemaPath: '#/required', | ||
}, | ||
], | ||
ok: false, | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('when posting the correct data', () => { | ||
beforeEach(() => { | ||
data = { id: 'anId' } | ||
}) | ||
|
||
it("should respond with the handler's response", () => { | ||
return server | ||
.post(buildURL('/test')) | ||
.send(data) | ||
.expect(200) | ||
.then(() => undefined) | ||
}) | ||
}) |
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,26 @@ | ||
import { Schema } from 'ajv' | ||
import express from 'express' | ||
import { HTTPError, STATUS_CODES } from '../common/HTTPError' | ||
import { getValidator } from '../utils/validator' | ||
|
||
const validator = getValidator() | ||
|
||
export const withSchemaValidation = (schema: Schema) => ( | ||
req: express.Request, | ||
_: express.Response, | ||
next: express.NextFunction | ||
) => { | ||
const validate = validator.compile(schema) | ||
const valid = validate(req.body) | ||
|
||
if (!valid) { | ||
next( | ||
new HTTPError( | ||
'Invalid request body', | ||
validate.errors, | ||
STATUS_CODES.badRequest | ||
) | ||
) | ||
} | ||
next() | ||
} |