building-microservices-youtube
building-microservices-youtube copied to clipboard
Is there a way to avoid duplication between docs and validation?
I noticed that we have to duplicate the SKU pattern both in the documentation:
type Product struct {
...
// the SKU for the product
//
// required: true
// pattern: [a-z]+-[a-z]+-[a-z]+
SKU string `json:"sku" validate:"sku"`
}
and the validation function:
func validateSKU(fl validator.FieldLevel) bool {
// SKU must be in the format abc-abc-abc
re := regexp.MustCompile(`[a-z]+-[a-z]+-[a-z]+`)
In Clojure, I would create a spec (schema) that could be reused both in documentation and the validation. How can we achieve something similar in Go to avoid duplicating these patterns?