Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
D00mch opened this issue Aug 4, 2024 · 0 comments
Open

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

D00mch opened this issue Aug 4, 2024 · 0 comments

Comments

@D00mch
Copy link

D00mch commented Aug 4, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant