building-microservices-youtube icon indicating copy to clipboard operation
building-microservices-youtube copied to clipboard

Is there a way to avoid duplication between docs and validation?

Open D00mch opened this issue 1 year ago • 0 comments

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?

D00mch avatar Aug 04 '24 16:08 D00mch