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

Error Message Templating #5

Open
jimeh opened this issue Sep 4, 2021 · 0 comments
Open

Error Message Templating #5

jimeh opened this issue Sep 4, 2021 · 0 comments

Comments

@jimeh
Copy link
Member

jimeh commented Sep 4, 2021

We want to support some very basic templating features in error messages
returned by Validate() functions.

Specifically, within a Validate() method on a struct type, we want to support
field name normalization via the FieldNameFunc feature, to convert the field
name to it's JSON/YAML/Form tag.

This would allow a error message like:

only allowed ObjectType='book'

The ObjectType field name is the Go struct field name, if this object uses
JSON, the external name of the field in JSON might be object_type or
objectType. So we want to be able to indicate in the error message that
ObjectType is a field on the struct.

I think we could do this a few different ways. Assuming we the following struct:

type Item struct {
	ObjectType string `json:"object_type"`
	BookTitle  string `json:"book_title"`
}

We want the end result to be this:

only allowed object_type='book'

We could use a custom syntax, something like one of these:

only allowed #{ObjectType}='book'
only allowed ${ObjectType}='book'
only allowed {{ObjectType}}='book'

Or we could leverage the text/template package using a custom field
function:

only allowed {{ field "ObjectType" }}='book'

Some of the field validation helpers mentioned in #4 could benefit from this
feature too.

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