{"id":14957614,"url":"https://github.com/naz/swagger-express-validator","last_synced_at":"2025-05-02T07:32:05.640Z","repository":{"id":39373625,"uuid":"77843047","full_name":"naz/swagger-express-validator","owner":"naz","description":"Lightweight requrest/response validation middleware based on swagger/OpenAPI schema","archived":false,"fork":false,"pushed_at":"2022-12-30T17:23:26.000Z","size":514,"stargazers_count":56,"open_issues_count":19,"forks_count":24,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-10-29T23:14:52.742Z","etag":null,"topics":["express","express-js","middleware","open-api","schema-validation","swagger","validation"],"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/naz.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":"2017-01-02T15:31:10.000Z","updated_at":"2023-10-25T22:22:46.000Z","dependencies_parsed_at":"2023-01-31T12:16:19.012Z","dependency_job_id":null,"html_url":"https://github.com/naz/swagger-express-validator","commit_stats":null,"previous_names":["gargol/swagger-express-validator"],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/naz%2Fswagger-express-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/naz%2Fswagger-express-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/naz%2Fswagger-express-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/naz%2Fswagger-express-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/naz","download_url":"https://codeload.github.com/naz/swagger-express-validator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224305842,"owners_count":17289446,"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":["express","express-js","middleware","open-api","schema-validation","swagger","validation"],"created_at":"2024-09-24T13:15:14.520Z","updated_at":"2024-11-12T16:03:42.638Z","avatar_url":"https://github.com/naz.png","language":"JavaScript","readme":"Swagger Express Validator\n=========================\n\n[![Build Status](https://travis-ci.org/gargol/swagger-express-validator.svg?branch=master)](https://travis-ci.org/gargol/swagger-express-validator)\n\n`swagger-express-validator` is a lightweight middleware for request/response validation based on\n[OpenAPI v2.0](http://swagger.io/specification/) (aka swagger) specification.\n\nThe main difference of this package to alternatives like\n[swagger-tools](https://github.com/apigee-127/swagger-tools) is that this package is very\nconfigurable and only concentrates on validation against provided schema. You can choose the\nbehavior of invalid validation like returning a 500 or just logging an error to your logger.\n\n## Requirements\n- express ^4.0.0\n- body-parser ^1.0.0\n\n## Features\n* Configurable validation behavior\n* [Fastest](https://github.com/ebdrup/json-schema-benchmark) available JSON schema validation based on [ajv](https://github.com/epoberezkin/ajv) library\n* Optional validations for either request parameters or response values\n* Independent from express application structure. This is a simple drop-in middleware without additional\n alterations to your swagger definitions or application routing.\n* Support for nullable field validation though `x-nullable` attribute\n\n## Installation\nStart using this library with `npm install swagger-express-validator --save`\n\n## Sample use\nTo set up simple validation for your requests and responses:\n```javascript\nconst util = require('util');\nconst express = require('express');\nconst bodyParser = require('body-parser');\nconst validator = require('swagger-express-validator');\nconst schema = require('./api-schema.json');\n\nconst server = express();\nserver.use(bodyParser.json());\n\nconst opts = {\n  schema, // Swagger schema\n  preserveResponseContentType: false, // Do not override responses for validation errors to always be JSON, default is true\n  returnRequestErrors: true, // Include list of request validation errors with response, default is false\n  returnResponseErrors: true, // Include list of response validation errors with response, default is false\n  validateRequest: true,\n  validateResponse: true,\n  requestValidationFn: (req, data, errors) =\u003e {\n    console.log(`failed request validation: ${req.method} ${req.originalUrl}\\n ${util.inspect(errors)}`)\n  },\n  responseValidationFn: (req, data, errors) =\u003e {\n    console.log(`failed response validation: ${req.method} ${req.originalUrl}\\n ${util.inspect(errors)}`)\n  },\n};\nserver.use(validator(opts));\n\nserver.use('/status', (req, res) =\u003e {\n  res.json({\n    status: 'OK',\n  })\n});\nserver.use((err, req, res, next) =\u003e {\n  res.status(500);\n  res.json(err);\n});\n\nreturn server.listen(3000);\n\n```\n\n## Ajv configuration\n\n`swagger-express-validator` uses Ajv for schema validation under the hood. You can tweak many validation parameters by passing Ajv configuration overrides:\n\n```javascript\nserver.use(validator({\n  schema,\n  preserveResponseContentType: false,\n  returnRequestErrors: true,\n  returnResponseErrors: true,\n  validateRequest: true,\n  validateResponse: true,\n  ajvRequestOptions: {\n    coerceTypes: true,\n  },\n  ajvResponseOptions: {\n    coerceTypes: true,\n  },\n}));\n```\n\nSee Ajv documentation for supported values.\n## Debugging\nTo see debug output use `DEBUG=swagger-express-validator` as an environmental variable when starting\nyour project, eg.: `DEBUG=swagger-express-validator node server.js`. To gain more insights\non how this works see documentation of [debug](https://github.com/visionmedia/debug) library\n\n## Contributors\n- [Naz](https://github.com/naz)\n- [Isaac Stefanek](https://github.com/iadknet)\n- [James Gregory](https://github.com/jagregory)\n- [Igor Savin](https://github.com/kibertoad)\n\n## Licence\n[MIT](https://github.com/gargol/swagger-express-validator/blob/master/LICENSE)\n\nSpecial thanks to [@bgaluszka](https://github.com/bgaluszka) for initial inspiration :)\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnaz%2Fswagger-express-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnaz%2Fswagger-express-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnaz%2Fswagger-express-validator/lists"}