Add support for non-struct pointers #5

Merged
lunny merged 5 commits from KN4CK3R/binding:feature-pointer into master 2024-03-08 08:14:14 +00:00
Member

This PR adds support for pointers to optional fields in forms.

This example fails in the old version:

type TestForm struct {
	Test string `binding:"Url"`
	TestPtr *string `binding:"Url"`
}

empty := ""
form := &TestForm{
	Test: "",
	TestPtr: &empty,
}
_ := RawValidate(form)

The empty Test field is no problem because an empty field is valid url. The empty pointer field throws an error because the regex checks against the wrong field value of "0xc000011dc0". So the memory address is formatted as string and tested. After the change both fields pass because both are treated equal.

Fixes the workaround in https://github.com/go-gitea/gitea/pull/15690

This PR adds support for pointers to optional fields in forms. This example fails in the old version: ```go type TestForm struct { Test string `binding:"Url"` TestPtr *string `binding:"Url"` } empty := "" form := &TestForm{ Test: "", TestPtr: &empty, } _ := RawValidate(form) ``` The empty `Test` field is no problem because an empty field is valid url. The empty pointer field throws an error because the regex checks against the wrong field value of `"0xc000011dc0"`. So the memory address is formatted as string and tested. After the change both fields pass because both are treated equal. Fixes the workaround in https://github.com/go-gitea/gitea/pull/15690
KN4CK3R added 1 commit 2021-05-02 20:52:49 +00:00
KN4CK3R reviewed 2021-05-02 20:53:27 +00:00
binding.go Outdated
@ -353,7 +353,22 @@ func validateStruct(errors Errors, obj interface{}) Errors {
(field.Type.Kind() == reflect.Ptr && !reflect.DeepEqual(zero, fieldValue) &&
field.Type.Elem().Kind() == reflect.Struct) {
errors = validateStruct(errors, fieldValue)
continue
Author
Member

I'm not sure about the continue but I think it's right.

I'm not sure about the `continue` but I think it's right.
KN4CK3R added 1 commit 2021-05-03 07:38:27 +00:00
Added test cases.
All checks were successful
continuous-integration/drone/pr Build is passing
a47ca29023
KN4CK3R changed title from WIP: Add support for non-struct pointers to Add support for non-struct pointers 2021-05-03 07:39:06 +00:00
KN4CK3R added 1 commit 2021-05-05 06:26:41 +00:00
Merge branch 'master' of https://gitea.com/go-chi/binding into feature-pointer
Some checks reported errors
continuous-integration/drone/pr Build was killed
3bc5ae9786
lunny added the
enhancement
label 2021-05-05 08:41:50 +00:00
Owner

files conflicted.

files conflicted.
KN4CK3R added 1 commit 2021-06-09 13:23:29 +00:00
Merge branch 'master' of https://gitea.com/go-chi/binding into feature-pointer
All checks were successful
continuous-integration/drone/pr Build is passing
65ea96736d
Owner

Please resolve the conflicts.

Please resolve the conflicts.
Owner
@KN4CK3R ?
lunny added 1 commit 2024-03-01 08:24:18 +00:00
Merge branch 'master' into KN4CK3R-feature-pointer
All checks were successful
test / check and test (pull_request) Successful in 20m11s
ac89d36f38
lunny approved these changes 2024-03-01 08:27:13 +00:00
lunny merged commit 1d3a5e518f into master 2024-03-08 08:14:14 +00:00
lunny referenced this issue from a commit 2024-03-21 10:34:04 +00:00
Sign in to join this conversation.
No reviewers
No Milestone
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: go-chi/binding#5
No description provided.