|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { SchemaValidationError } from '../../src/errors.js'; |
| 3 | +import { validationMiddleware } from '../../src/middleware.js'; |
| 4 | + |
| 5 | +const inboundSchema = { |
| 6 | + type: 'object', |
| 7 | + properties: { |
| 8 | + inputValue: { type: 'number' }, |
| 9 | + }, |
| 10 | + required: ['inputValue'], |
| 11 | + additionalProperties: false, |
| 12 | +}; |
| 13 | + |
| 14 | +const outboundSchema = { |
| 15 | + type: 'object', |
| 16 | + properties: { |
| 17 | + outputValue: { type: 'number' }, |
| 18 | + }, |
| 19 | + required: ['outputValue'], |
| 20 | + additionalProperties: false, |
| 21 | +}; |
| 22 | + |
| 23 | +describe('validatorMiddleware', () => { |
| 24 | + it('should validate inbound and outbound successfully', async () => { |
| 25 | + // Prepare |
| 26 | + const middleware = validationMiddleware({ inboundSchema, outboundSchema }); |
| 27 | + const handler = { |
| 28 | + event: { inputValue: 10 }, |
| 29 | + response: { outputValue: 20 }, |
| 30 | + }; |
| 31 | + // Act |
| 32 | + if (middleware.before) { |
| 33 | + await middleware.before(handler); |
| 34 | + } |
| 35 | + if (middleware.after) { |
| 36 | + await middleware.after(handler); |
| 37 | + } |
| 38 | + // Assess |
| 39 | + expect(handler.event).toEqual({ inputValue: 10 }); |
| 40 | + expect(handler.response).toEqual({ outputValue: 20 }); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should throw error on inbound validation failure', async () => { |
| 44 | + // Prepare |
| 45 | + const middleware = validationMiddleware({ inboundSchema }); |
| 46 | + const handler = { |
| 47 | + event: { inputValue: 'invalid' }, |
| 48 | + response: {}, |
| 49 | + }; |
| 50 | + // Act & Assess |
| 51 | + await expect(middleware.before?.(handler)).rejects.toThrow( |
| 52 | + SchemaValidationError |
| 53 | + ); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should throw error on outbound validation failure', async () => { |
| 57 | + // Prepare |
| 58 | + const middleware = validationMiddleware({ outboundSchema }); |
| 59 | + const handler = { |
| 60 | + event: {}, |
| 61 | + response: { outputValue: 'invalid' }, |
| 62 | + }; |
| 63 | + // Act & Assess |
| 64 | + await expect(middleware.after?.(handler)).rejects.toThrow( |
| 65 | + SchemaValidationError |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + it('should no-op when no schemas are provided', async () => { |
| 70 | + // Prepare |
| 71 | + const middleware = validationMiddleware({}); |
| 72 | + const handler = { |
| 73 | + event: { someKey: 'value' }, |
| 74 | + response: { anotherKey: 'value' }, |
| 75 | + }; |
| 76 | + // Act |
| 77 | + if (middleware.before) { |
| 78 | + await middleware.before(handler); |
| 79 | + } |
| 80 | + if (middleware.after) { |
| 81 | + await middleware.after(handler); |
| 82 | + } |
| 83 | + // Assess |
| 84 | + expect(handler.event).toEqual({ someKey: 'value' }); |
| 85 | + expect(handler.response).toEqual({ anotherKey: 'value' }); |
| 86 | + }); |
| 87 | +}); |
0 commit comments