{"id":15597394,"url":"https://github.com/cdimascio/express-openapi-validator-example","last_synced_at":"2025-04-28T10:47:15.849Z","repository":{"id":49614682,"uuid":"177008824","full_name":"cdimascio/express-openapi-validator-example","owner":"cdimascio","description":"simple openapi validation examplewith express-openapi-validator","archived":false,"fork":false,"pushed_at":"2022-12-10T17:00:12.000Z","size":214,"stargazers_count":11,"open_issues_count":4,"forks_count":4,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-18T15:16:42.701Z","etag":null,"topics":["api","express","file-upload","openapi","request-validation","response-validation","security","validation"],"latest_commit_sha":null,"homepage":"https://github.com/cdimascio/express-openapi-validator","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cdimascio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-03-21T19:11:07.000Z","updated_at":"2023-12-21T02:43:22.000Z","dependencies_parsed_at":"2023-01-26T09:31:58.592Z","dependency_job_id":null,"html_url":"https://github.com/cdimascio/express-openapi-validator-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdimascio%2Fexpress-openapi-validator-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdimascio%2Fexpress-openapi-validator-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdimascio%2Fexpress-openapi-validator-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cdimascio%2Fexpress-openapi-validator-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cdimascio","download_url":"https://codeload.github.com/cdimascio/express-openapi-validator-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251298198,"owners_count":21566971,"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":["api","express","file-upload","openapi","request-validation","response-validation","security","validation"],"created_at":"2024-10-03T01:21:36.714Z","updated_at":"2025-04-28T10:47:15.819Z","avatar_url":"https://github.com/cdimascio.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# express-openapi-validator-example\n\nProvides a simple example demonstrating how [express-openapi-validator](https://github.com/cdimascio/express-openapi-validator) can be used to automatically validate api requests.\n\n## Setup the example project\n\n```shell\n# 1. clone this repo\ngit clone https://github.com/cdimascio/express-openapi-validator-example\n\n# 2. install dependencies\nnpm install\n```\n\n## Run it\n\nStart the Api server\n\n```shell\nnpm start\n```\n\n## Try it\n\nTry the out the following requests.\n\nThe [express-openapi-validator](https://github.com/cdimascio/express-openapi-validator) automatically validates each request against an [openapi 3 specification](openapi.yaml). If a request is does not match the spec, [express-openapi-validator](https://github.com/cdimascio/express-openapi-validator) automatically returns an appropriate error response.\n\n### Validate a query parameter with a value constraint\n\n```shell\nccurl -s http://localhost:3000/v1/pets/as |jq\n{\n  \"errors\": [\n    {\n      \"path\": \".params.id\",\n      \"message\": \"should be integer\",\n      \"errorCode\": \"type.openapi.validation\"\n    }\n  ]\n}\n```\n\n### Validate a query parameter with a range constraint\n\n```shell\ncurl -s http://localhost:3000/v1/pets\\?limit\\=1 |jq\n{\n  \"errors\": [\n    {\n      \"path\": \".query.limit\",\n      \"message\": \"should be \u003e= 5\",\n      \"errorCode\": \"minimum.openapi.validation\"\n    },\n    {\n      \"path\": \".query.test\",\n      \"message\": \"should have required property 'test'\",\n      \"errorCode\": \"required.openapi.validation\"\n    }\n  ]\n}\n```\n\n### Validate the query parameter's value type\n\n```shell\ncurl -s --request POST \\\n  --url http://localhost:3000/v1/pets \\\n  --header 'content-type: application/xml' \\\n  --data '{\n        \"name\": \"test\"\n}' |jq\n  \"message\": \"unsupported media type application/xml\",\n  \"errors\": [\n    {\n      \"path\": \"/v1/pets\",\n      \"message\": \"unsupported media type application/xml\"\n    }\n  ]\n}\n```\n\n### Validate a POST body to ensure required parameters are present\n\n```shell\nλ  curl -s --request POST \\\n  --url http://localhost:3000/v1/pets \\\n  --header 'content-type: application/json' \\\n  --data '{\n}'|jq\n{\n  \"message\": \"request.body should have required property 'name'\",\n  \"errors\": [\n    {\n      \"path\": \".body.name\",\n      \"message\": \"should have required property 'name'\",\n      \"errorCode\": \"required.openapi.validation\"\n    }\n  ]\n}\n```\n\n### File upload example\n\n```shell\ncurl -XPOST http://localhost:3000/v1/pets/10/photos -F file=@app.js|jq\n{\n  \"files_metadata\": [\n    {\n      \"originalname\": \"app.js\",\n      \"encoding\": \"7bit\",\n      \"mimetype\": \"application/octet-stream\"\n    }\n  ]\n}\n```\n\n### Validate security\n\nUsing ApiKeyAuth\n\n```shell\ncurl -XPOST http://localhost:3000/v1/pets |jq\n\n{\n  \"message\": \"'X-API-Key' header required.\",\n  \"errors\": [\n    {\n      \"path\": \"/v1/pets\",\n      \"message\": \"'X-API-Key' header required.\"\n    }\n  ]\n}\n```\n\nwith the api key and [security handler](https://github.com/cdimascio/express-openapi-validator-example/blob/master/app.js#L24)\n\n```shell\ncurl -XPOST http://localhost:3000/v1/pets --header 'X-Api-Key: XXXXX' --header 'content-type: application/json' -d '{\"name\": \"carmine\"}' |jq\n{\n  \"name\": \"sparky\"\n}\n```\n\n### ...and much more. Try it out!\n\n## Fetch the spec\n\ncurl http://localhost:3000/spec\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcdimascio%2Fexpress-openapi-validator-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcdimascio%2Fexpress-openapi-validator-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcdimascio%2Fexpress-openapi-validator-example/lists"}