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.git
The 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-server
To run the server, run the following command:
task run
The following is a brief explanation of the server generation process:
- The
generate-server
task inTaskfile.yaml
generates the server code and models usingoapi-codegen
and places it in theserver-gen/
directory. - The configurations for the server and model generation are specified in
server-gen/server.cfg.yaml
andserver-gen/models.cfg.yaml
respectively. - 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-server
task, 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-codegen
provides a way to add custom tags to the models usingx-oapi-codegen-extra-tags
extension.- To demonstrate this, I added the following baked-in tags to
swagger.yaml
-validate: gte=1 lte=100
to quantity inOrder
schema.validate: datetime
toshipDate
inOrder
schema.validate: oneof
to all the enums.validate: required
to all the required fields.validate: email
toemail
inUser
schema.
- Additionally, I added the following custom tags to
swagger.yaml
-validate: base64
topassword
inUser
schema.- Added a custom tag validator for
base64
inutils/validators.go
.
- To see the validation in action, run the server and send a request with invalid data to the
POST /store/order
endpoint.
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.