-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
958dc68
commit 10983a2
Showing
4 changed files
with
84 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import middy from '@middy/core' | ||
import { Type } from '@sinclair/typebox' | ||
import type { Context } from 'aws-lambda' | ||
import assert from 'node:assert' | ||
import { describe, it } from 'node:test' | ||
import { | ||
ResponseValidationFailedError, | ||
validateResponse, | ||
} from './validateResponse.js' | ||
|
||
void describe('validateResponse()', () => { | ||
void it('should validate the response', async () => | ||
assert.equal( | ||
await middy() | ||
.use(validateResponse(Type.Boolean({ title: 'A boolean' }))) | ||
.handler(async () => true)('Some event', {} as Context), | ||
true, | ||
)) | ||
|
||
void it('should throw an Error in case the response is invalid', async () => | ||
assert.rejects( | ||
async () => | ||
middy() | ||
.use(validateResponse(Type.Boolean())) | ||
.handler(async () => 42)('Some event', {} as Context), | ||
ResponseValidationFailedError, | ||
)) | ||
}) |
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,39 @@ | ||
import { | ||
formatTypeBoxErrors, | ||
validateWithTypeBox, | ||
} from '@hello.nrfcloud.com/proto' | ||
import type middy from '@middy/core' | ||
import type { TSchema } from '@sinclair/typebox' | ||
import type { ValueError } from '@sinclair/typebox/errors' | ||
import { ValidationFailedError } from './validateInput.js' | ||
|
||
export class ResponseValidationFailedError extends ValidationFailedError { | ||
constructor(errors: ValueError[]) { | ||
super(errors, 'Response validation failed') | ||
this.name = 'ResponseValidationFailedError' | ||
} | ||
} | ||
|
||
export const validateResponse = <ResponseSchema extends TSchema>( | ||
schema: ResponseSchema, | ||
): middy.MiddlewareObj => { | ||
const validator = validateWithTypeBox(schema) | ||
return { | ||
after: async (req) => { | ||
const maybeValid = validator(req.response) | ||
if ('errors' in maybeValid) { | ||
console.error( | ||
`[validateResponse]`, | ||
`Response validation failed`, | ||
JSON.stringify({ | ||
response: req.response, | ||
errors: formatTypeBoxErrors(maybeValid.errors), | ||
}), | ||
) | ||
throw new ResponseValidationFailedError(maybeValid.errors) | ||
} | ||
console.debug(`[validateResponse]`, `Response is`, schema.title) | ||
return undefined | ||
}, | ||
} | ||
} |