This is a sample project showcasing how to use oapi-codegen to generate an Echo server and then use oapi-validator middleware to validate the incoming requests. This project uses the Petstore API as the API definition and adds validations to the API definition.
Make sure you have the following tools installed:
- Go (version 1.20)
- oapi-codegen (version 1.12.3)
- Taskfile (version 3.20.0)
Clone the repository:
git clone https://github.com/kolluria/oapi-validator-echo-sample.gitThe project is structured as follows:
server-gen/: Contains the generated server code.utils/: Contains the utility functions for the Service..gitignore: Contains the list of files to be ignored by Git.main.go: Contains the main function for the Service.README.md: Contains the README for the Service.Taskfile.yaml: Contains the tasks for the Service.swagger.yaml: Contains the OpenAPI specification for the Service.
To generate the server after making any changes to swagger.yaml, run the following command:
task generate-serverTo run the server, run the following command:
task runThe following is a brief explanation of the server generation process:
- The
generate-servertask inTaskfile.yamlgenerates the server code and models usingoapi-codegenand places it in theserver-gen/directory. - The configurations for the server and model generation are specified in
server-gen/server.cfg.yamlandserver-gen/models.cfg.yamlrespectively. - The config file for the server generation is as follows:
# Path: server-gen/server.cfg.yaml package: servergen output: server.go generate: echo-server: true embedded-spec: true strict-server: true
- The config file for the model generation is as follows:
# Path: server-gen/models.cfg.yaml package: servergen output: models.go generate: types: true
- Upon running the
generate-servertask, the directory structure looks like this:server-gen/ ├── models.go ├── models.cfg.yaml ├── server.go └── server.cfg.yaml
- Since go-playground/validator is a tag-based validator, we need tags on the models to validate them.
oapi-codegenprovides a way to add custom tags to the models usingx-oapi-codegen-extra-tagsextension.- To demonstrate this, I added the following baked-in tags to
swagger.yaml-validate: gte=1 lte=100to quantity inOrderschema.validate: datetimetoshipDateinOrderschema.validate: oneofto all the enums.validate: requiredto all the required fields.validate: emailtoemailinUserschema.
- Additionally, I added the following custom tags to
swagger.yaml-validate: base64topasswordinUserschema.- Added a custom tag validator for
base64inutils/validators.go.
- To see the validation in action, run the server and send a request with invalid data to the
POST /store/orderendpoint.
Since my vision for this project is to be a template for future projects, I plan to do the following in the future(preferably with the help of the community):
- Add more custom tags to the models.
- Add more custom tag validators.
- Add client generation.
- Add tests.
- Containerize the application.
This project is licensed under the MIT License - see the LICENSE file for details.