-
Notifications
You must be signed in to change notification settings - Fork 156
feat(validation): Add Middy.js middleware for JSON Schema validation #3694
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dreamorosi
merged 14 commits into
aws-powertools:main
from
VatsalGoel3:feature/validator-middy-middleware
Mar 5, 2025
Merged
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
9af077a
feat(validation): add @validator decorator for JSON Schema validation
VatsalGoel3 4e0ea04
Updated the test suite
VatsalGoel3 4984867
updated test suite
VatsalGoel3 81fff4b
Merge branch 'main' into feature/validator-decorator
VatsalGoel3 a4f1060
refactor(validation): update decorator with improved types and schema…
VatsalGoel3 5f5d470
Merge branch 'feature/validator-decorator' of https://github.com/Vats…
VatsalGoel3 8676b00
Update packages/validation/src/decorator.ts
dreamorosi e93f513
Updated imports and exports
VatsalGoel3 e121fa7
Merge branch 'feature/validator-decorator' of https://github.com/Vats…
VatsalGoel3 89c30fd
Merge branch 'main' into feature/validator-decorator
VatsalGoel3 59e171c
feat(validation): add Middy.js middleware for JSON Schema validation
VatsalGoel3 fe6dba9
Merge branch 'main' into feature/validator-middy-middleware
VatsalGoel3 9086bd6
feat(validation): add Middy.js middleware integration tests
VatsalGoel3 1971a25
Merge branch 'main' into feature/validator-middy-middleware
VatsalGoel3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { validate } from './validate.js'; | ||
export { SchemaValidationError } from './errors.js'; | ||
export { validator } from './decorator.js'; | ||
export { validationMiddleware } from './middleware.js'; | ||
This file contains hidden or 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,42 @@ | ||
import { SchemaValidationError } from './errors.js'; | ||
import type { ValidatorOptions } from './types.js'; | ||
import { validate } from './validate.js'; | ||
|
||
export function validationMiddleware(options: ValidatorOptions) { | ||
dreamorosi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!options.inboundSchema && !options.outboundSchema) { | ||
dreamorosi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return {}; | ||
} | ||
return { | ||
before: async (handler: { event: unknown }) => { | ||
if (options.inboundSchema) { | ||
try { | ||
handler.event = validate({ | ||
payload: handler.event, | ||
schema: options.inboundSchema, | ||
envelope: options.envelope, | ||
formats: options.formats, | ||
externalRefs: options.externalRefs, | ||
ajv: options.ajv, | ||
}); | ||
} catch (error) { | ||
throw new SchemaValidationError('Inbound validation failed', error); | ||
} | ||
} | ||
}, | ||
after: async (handler: { response: unknown }) => { | ||
if (options.outboundSchema) { | ||
try { | ||
handler.response = validate({ | ||
payload: handler.response, | ||
schema: options.outboundSchema, | ||
formats: options.formats, | ||
externalRefs: options.externalRefs, | ||
ajv: options.ajv, | ||
}); | ||
} catch (error) { | ||
throw new SchemaValidationError('Outbound validation failed', error); | ||
} | ||
} | ||
}, | ||
}; | ||
} |
dreamorosi marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or 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,87 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
import { SchemaValidationError } from '../../src/errors.js'; | ||
import { validationMiddleware } from '../../src/middleware.js'; | ||
|
||
const inboundSchema = { | ||
type: 'object', | ||
properties: { | ||
inputValue: { type: 'number' }, | ||
}, | ||
required: ['inputValue'], | ||
additionalProperties: false, | ||
}; | ||
|
||
const outboundSchema = { | ||
type: 'object', | ||
properties: { | ||
outputValue: { type: 'number' }, | ||
}, | ||
required: ['outputValue'], | ||
additionalProperties: false, | ||
}; | ||
|
||
describe('validatorMiddleware', () => { | ||
it('should validate inbound and outbound successfully', async () => { | ||
// Prepare | ||
const middleware = validationMiddleware({ inboundSchema, outboundSchema }); | ||
const handler = { | ||
event: { inputValue: 10 }, | ||
response: { outputValue: 20 }, | ||
}; | ||
// Act | ||
if (middleware.before) { | ||
await middleware.before(handler); | ||
} | ||
if (middleware.after) { | ||
await middleware.after(handler); | ||
} | ||
// Assess | ||
expect(handler.event).toEqual({ inputValue: 10 }); | ||
expect(handler.response).toEqual({ outputValue: 20 }); | ||
}); | ||
|
||
it('should throw error on inbound validation failure', async () => { | ||
// Prepare | ||
const middleware = validationMiddleware({ inboundSchema }); | ||
const handler = { | ||
event: { inputValue: 'invalid' }, | ||
response: {}, | ||
}; | ||
// Act & Assess | ||
await expect(middleware.before?.(handler)).rejects.toThrow( | ||
SchemaValidationError | ||
); | ||
}); | ||
|
||
it('should throw error on outbound validation failure', async () => { | ||
// Prepare | ||
const middleware = validationMiddleware({ outboundSchema }); | ||
const handler = { | ||
event: {}, | ||
response: { outputValue: 'invalid' }, | ||
}; | ||
// Act & Assess | ||
await expect(middleware.after?.(handler)).rejects.toThrow( | ||
SchemaValidationError | ||
); | ||
}); | ||
|
||
it('should no-op when no schemas are provided', async () => { | ||
// Prepare | ||
const middleware = validationMiddleware({}); | ||
const handler = { | ||
event: { someKey: 'value' }, | ||
response: { anotherKey: 'value' }, | ||
}; | ||
// Act | ||
if (middleware.before) { | ||
await middleware.before(handler); | ||
} | ||
if (middleware.after) { | ||
await middleware.after(handler); | ||
} | ||
// Assess | ||
expect(handler.event).toEqual({ someKey: 'value' }); | ||
expect(handler.response).toEqual({ anotherKey: 'value' }); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.