{"id":20901592,"url":"https://github.com/willfarrell/middy-ajv","last_synced_at":"2025-05-13T03:30:49.061Z","repository":{"id":40349920,"uuid":"374231716","full_name":"willfarrell/middy-ajv","owner":"willfarrell","description":"A faster and leaner alternative to @middy/validator","archived":false,"fork":false,"pushed_at":"2022-10-15T03:51:33.000Z","size":98,"stargazers_count":2,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-01T15:48:03.159Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/willfarrell.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-06-05T23:39:13.000Z","updated_at":"2024-08-28T00:57:09.000Z","dependencies_parsed_at":"2023-01-19T19:31:16.886Z","dependency_job_id":null,"html_url":"https://github.com/willfarrell/middy-ajv","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willfarrell%2Fmiddy-ajv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willfarrell%2Fmiddy-ajv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willfarrell%2Fmiddy-ajv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/willfarrell%2Fmiddy-ajv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/willfarrell","download_url":"https://codeload.github.com/willfarrell/middy-ajv/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253867347,"owners_count":21976208,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-18T11:36:08.816Z","updated_at":"2025-05-13T03:30:48.761Z","avatar_url":"https://github.com/willfarrell.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Middy AJV\n\n\u003cdiv align=\"center\"\u003e\n  \u003cimg alt=\"Middy logo\" src=\"https://raw.githubusercontent.com/middyjs/middy/master/docs/img/middy-logo.svg\"/\u003e\n\u003c/div\u003e\n\n\u003cdiv align=\"center\"\u003e\n  \u003cp\u003e\u003cstrong\u003eAJV middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda\u003c/strong\u003e\u003c/p\u003e\n\u003c/div\u003e\n\n\u003cdiv align=\"center\"\u003e\n\u003cp\u003e\n  \u003ca href=\"http://badge.fury.io/js/middy-ajv\"\u003e\n    \u003cimg src=\"https://badge.fury.io/js/middy-ajv.svg\" alt=\"npm version\" style=\"max-width:100%;\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://snyk.io/test/github/willfarrell/middy-ajv\"\u003e\n    \u003cimg src=\"https://snyk.io/test/github/willfarrell/middy-ajv/badge.svg\" alt=\"Known Vulnerabilities\" data-canonical-src=\"https://snyk.io/test/github/willfarrell/middy-ajv\" style=\"max-width:100%;\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://standardjs.com/\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/code_style-standard-brightgreen.svg\" alt=\"Standard Code Style\"  style=\"max-width:100%;\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://gitter.im/middyjs/Lobby\"\u003e\n    \u003cimg src=\"https://badges.gitter.im/gitterHQ/gitter.svg\" alt=\"Chat on Gitter\"  style=\"max-width:100%;\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\u003c/div\u003e\n\nThis middleware automatically validates incoming events and outgoing responses against custom\nschemas defined with the [JSON schema syntax](http://json-schema.org/).\n\nIf an incoming event fails validation a `BadRequest` error is raised.\nIf an outgoing response fails validation a `InternalServerError` error is\nraised.\n\nThis middleware can be used in combination with\n[`httpErrorHandler`](#httperrorhandler) to automatically return the right\nresponse to the user.\n\nIt can also be used in combination with [`httpcontentnegotiation`](#httpContentNegotiation) to load localised translations for the error messages (based on the currently requested language). This feature uses internally [`ajv-i18n`](http://npm.im/ajv-i18n) module, so reference to this module for options and more advanced use cases. By default the language used will be English (`en`), but you can redefine the default language by passing it in the `ajvOptions` options with the key `defaultLanguage` and specifying as value one of the [supported locales](https://www.npmjs.com/package/ajv-i18n#supported-locales).\n\nAlso, this middleware accepts an object with plugins to be applied to customize the internal `ajv` instance.\n\n## Install\n\nTo install this middleware you can use NPM:\n\n```bash\nnpm install --save middy-ajv\n```\n\nRequires: @middy/core:\u003e=2.0.0\n\n\n## Options\n\n- `eventSchema` (function) (default `undefined`): The JSON schema compiled ajv validator that will be used\n  to validate the input (`request.event`) of the Lambda handler.\n- `contextSchema` (function) (default `undefined`): The JSON schema object or compiled ajv validator that will be used\n  to validate the input (`request.context`) of the Lambda handler. Has additional support for `typeof` keyword to allow validation of `\"typeof\":\"function\"`.\n- `responseSchema` (function) (default `undefined`): The JSON schema compiled ajv validator that will be used\n  to validate the output (`request.response`) of the Lambda handler.\n- `availableLanguages` (object) (optional): Error messages can be returned in multiple languages using [`ajv-i18n`](https://www.npmjs.com/package/ajv-i18n). Language is selected based on `event.preferredLanguage` set by `@middy/http-content-negotiation`. Should be in the format: `{ 'en': require('ajv-i18n/localize/en') }`.\n- `defaultLanguage` (string) (default: `en`): The default language to use when `availableLanguages` is provided and `event.preferredLanguage` is not supported.\n\nNOTES:\n- At least one of `eventSchema` or `responseSchema` is required.\n\n## Sample usage\n\nExample for validation using precompiled schema:\n\n```javascript\nimport middy from '@middy/core'\nimport validator from 'middy-ajv'\n\nconst handler = middy((event, context) =\u003e {\n  return {}\n})\n\nconst eventSchema = require('schema.js')\n\nhandler.use(validator({ eventSchema }))\n\n// invokes the handler, note that property foo is missing\nconst event = {\n  body: JSON.stringify({something: 'somethingelse'})\n}\nhandler(event, {}, (err, res) =\u003e {\n  t.is(err.message,'Event object failed validation')\n})\n```\n\n## Build step\n\n### Folder Structure\n```shell\n{project}\n|-- handlers\n| |-- {endpoint}\n| | |-- index.js\n| | |-- schema.event.json\n| | |-- schema.context.json\n| | |-- schema.response.json\n```\n\n\n### Install\n```shell\n$ npm install -D ajv-cli\n```\n\n### Run\n```shell\n# or write your own\nnpx middy-ajv\n```\n\nAfter the compile script has been run on the `endpoint` folder, it will contain `schema.event.js`, `schema.context.js`, `schema.response.js` and `index.js`.\n\n## Middy documentation and examples\n\nFor more documentation and examples, refers to the main [Middy monorepo on GitHub](https://github.com/middyjs/middy) or [Middy official website](https://middy.js.org).\n\n\n## Contributing\n\nEveryone is very welcome to contribute to this repository. Feel free to [raise issues](https://github.com/middyjs/middy/issues) or to [submit Pull Requests](https://github.com/middyjs/middy/pulls).\n\n\n## License\n\nLicensed under [MIT License](LICENSE). Copyright (c) 2017-2021 will Farrell and the [Middy team](https://github.com/middyjs/middy/graphs/contributors).\n\n\u003ca href=\"https://app.fossa.io/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy?ref=badge_large\"\u003e\n  \u003cimg src=\"https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmiddyjs%2Fmiddy.svg?type=large\" alt=\"FOSSA Status\"  style=\"max-width:100%;\"\u003e\n\u003c/a\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillfarrell%2Fmiddy-ajv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwillfarrell%2Fmiddy-ajv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillfarrell%2Fmiddy-ajv/lists"}