-
Notifications
You must be signed in to change notification settings - Fork 8k
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
Binding tag: add 'skipped' on top of 'required' #610
Comments
+1 |
@manuel-huez @i19 You mean that skip this struct field from validation? |
i mean skipped from binding. |
i checked the code, and i found that gin use the tag gin first decode the user request to a if so, if i want to set an field as |
@i19 you can use type Login struct {
User string `form:"user" json:"user" binding:"required"`
Password string `form:"password" json:"password" binding:"-"` // HERE USE -
} |
@manuel-huez please read https://github.com/gin-gonic/gin#model-binding-and-validation thanks! |
Hhey @thinkerou thanks for the feedback! The issue is actually from a few years ago now, so we found some work-arounds at that time. |
@manuel-huez @thinkerou I don't think adding I have some unsafe fields in my application that I don't want to be bind by the request form, and |
@thinkerou Tests in my PR are actually passing. Builds are sometimes timing out, but it's happening even on the master branch: https://travis-ci.org/gin-gonic/gin/builds/475558492 |
* fix gin-gonic#1672 correct work with ptr - not create value if field is not set * avoid allocations on strings.Split() - change to strings.Index() * fix gin-gonic#610 tag value "-" is mean ignoring field * struct fields mapped like json.Unmarshal * map fields mapped like json.Unmarshal
#1733 merged! |
* refactor(form_mapping.go): mapping ptr, struct and map * fix #1672 correct work with ptr - not create value if field is not set * avoid allocations on strings.Split() - change to strings.Index() * fix #610 tag value "-" is mean ignoring field * struct fields mapped like json.Unmarshal * map fields mapped like json.Unmarshal * fix after @thinkerou review
Currently, when using
c.Bind()
, it is possible to set thebinding
tag torequired
to ensure that the field must be set. It would be useful to also be able to set this field to something likeskipped
so that when usingc.Bind()
, the field is skipped and isn't actually decoded.To give a bit of matter to this issue, my current use case is as follows: I have a model (struct) which I use to interact with my database and to also encode/decode json from my users requests (my application is an API). The model has an ID field which contains the ID of the object referenced in the database. This field should automatically be set by my application, but it should also be returned to the user when requested (when encoding the response's JSON).
Basically, being able to set the
binding
tag toskipped
would give the ability to have fields as read-only.The text was updated successfully, but these errors were encountered: