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

Required Without and some other rules not working together #624

Closed
loeffel-io opened this issue Jun 9, 2020 · 6 comments
Closed

Required Without and some other rules not working together #624

loeffel-io opened this issue Jun 9, 2020 · 6 comments
Assignees
Labels

Comments

@loeffel-io
Copy link

loeffel-io commented Jun 9, 2020

Too many issues with this library!

Package version eg. v8, v9:

v10

Issue, Question or Enhancement:

required_without=Name,min=3 throws:

2020/06/09 15:35:51 [Recovery] 2020/06/09 - 15:35:51 panic recovered:
DELETE /api/auth/team/member HTTP/1.1
Host: localhost:3000
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: de,en-US;q=0.9,en;q=0.8,de-DE;q=0.7
Connection: keep-alive
Content-Length: 186
Content-Type: application/json
Cookie: _ga=GA1.1.743678832.1590427430; wp-settings-1=libraryContent%3Dbrowse%26wplink%3D1%26imgsize%3Dfull%26align%3Dnone%26editor%3Dtinymce%26hidetb%3D1%26mfold%3Do%26uploader%3D1; wp-settings-time-1=1590428588; jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhYmMiOjEsImV4cCI6MTU5MTcxMjgyOCwib3JpZ19pYXQiOjE1OTE3MDkyMjh9.2KtUMmsvVjz6U4A009UZoWJSh_rX45nuyv0-H4X-VEY
Origin: http://localhost:8080
Referer: http://localhost:8080/settings/team/member
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-site
Team: 45
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36


Bad field type *string
/Users/lucasloffel/go/pkg/mod/github.com/go-playground/validator/v10@v10.2.0/baked_in.go:1557 (0x156a4ad)
        isGte: panic(fmt.Sprintf("Bad field type %T", field.Interface()))
/Users/lucasloffel/go/pkg/mod/github.com/go-playground/validator/v10@v10.2.0/baked_in.go:1642 (0x156bb92)
        hasMinOf: return isGte(fl)
/Users/lucasloffel/go/pkg/mod/github.com/go-playground/validator/v10@v10.2.0/baked_in.go:36 (0x158a5e0)
        wrapFunc.func1: return fn(fl)
/Users/lucasloffel/go/pkg/mod/github.com/go-playground/validator/v10@v10.2.0/validator.go:445 (0x157ca6c)
        (*validate).traverseField: if !ct.fn(ctx, v) {
/Users/lucasloffel/go/pkg/mod/github.com/go-playground/validator/v10@v10.2.0/validator.go:77 (0x157b82f)
        (*validate).validateStruct: v.traverseField(ctx, parent, current.Field(f.idx), ns, structNs, f, f.cTags)
/Users/lucasloffel/go/pkg/mod/github.com/go-playground/validator/v10@v10.2.0/validator_instance.go:304 (0x1585c6a)
        (*Validate).StructCtx: vd.validateStruct(ctx, top, val, val.Type(), vd.ns[0:0], vd.actualNs[0:0], nil)
/Users/lucasloffel/go/pkg/mod/github.com/go-playground/validator/v10@v10.2.0/validator_instance.go:277 (0x181a5b4)
        (*Validate).Struct: return v.StructCtx(context.Background(), s)
/Users/lucasloffel/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/binding/default_validator.go:30 (0x181a53d)
        (*defaultValidator).ValidateStruct: if err := v.validate.Struct(obj); err != nil {
/Users/lucasloffel/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/binding/binding.go:115 (0x181f863)
        validate: return Validator.ValidateStruct(obj)
/Users/lucasloffel/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/binding/json.go:55 (0x181f7f5)
        decodeJSON: return validate(obj)
/Users/lucasloffel/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/binding/json.go:37 (0x181f558)
        jsonBinding.Bind: return decodeJSON(req.Body, obj)
/Users/lucasloffel/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/context.go:679 (0x1830902)
        (*Context).ShouldBindWith: return b.Bind(c.Request, obj)
/Users/lucasloffel/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/context.go:639 (0x1830917)
        (*Context).ShouldBind: return c.ShouldBindWith(obj, b)
/Users/lucasloffel/go/src/github.com/go-saas/go-saas/member_team.go:56 (0x190a4bd)
        (*Saas).removeMemberTeam.func1: if err = c.ShouldBind(user); err != nil {
/Users/lucasloffel/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/context.go:161 (0x182cf56)
        (*Context).Next: c.handlers[c.index](c)
/Users/lucasloffel/go/src/github.com/go-saas/go-saas/http/basic/middleware.go:79 (0x1925de5)
        (*Http).TeamOwnerMiddleware.func1: c.Next()
/Users/lucasloffel/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/context.go:161 (0x182cf56)
        (*Context).Next: c.handlers[c.index](c)
/Users/lucasloffel/go/pkg/mod/github.com/appleboy/gin-jwt/v2@v2.6.3/auth_jwt.go:393 (0x18581f6)
        (*GinJWTMiddleware).middlewareImpl: c.Next()
/Users/lucasloffel/go/pkg/mod/github.com/appleboy/gin-jwt/v2@v2.6.3/auth_jwt.go:355 (0x185c88b)
        (*GinJWTMiddleware).MiddlewareFunc.func1: mw.middlewareImpl(c)
/Users/lucasloffel/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/context.go:161 (0x182cf56)
        (*Context).Next: c.handlers[c.index](c)
/Users/lucasloffel/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/recovery.go:83 (0x184a3d5)
        RecoveryWithWriter.func1: c.Next()
/Users/lucasloffel/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/context.go:161 (0x182cf56)
        (*Context).Next: c.handlers[c.index](c)
/Users/lucasloffel/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/recovery.go:83 (0x184a3d5)
        RecoveryWithWriter.func1: c.Next()
/Users/lucasloffel/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/context.go:161 (0x182cf56)
        (*Context).Next: c.handlers[c.index](c)
/Users/lucasloffel/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/logger.go:241 (0x1848db5)
        LoggerWithConfig.func1: c.Next()
/Users/lucasloffel/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/context.go:161 (0x182cf56)
        (*Context).Next: c.handlers[c.index](c)
/Users/lucasloffel/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/gin.go:409 (0x183b4dd)
        (*Engine).handleHTTPRequest: c.Next()
/Users/lucasloffel/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/gin.go:367 (0x183a644)
        (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/usr/local/Cellar/go/1.14.2_1/libexec/src/net/http/server.go:2807 (0x14c11ee)
        serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/usr/local/Cellar/go/1.14.2_1/libexec/src/net/http/server.go:1895 (0x14ba837)
        (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/usr/local/Cellar/go/1.14.2_1/libexec/src/runtime/asm_amd64.s:1373 (0x10a3910)
        goexit: BYTE    $0x90   // NOP

Code sample, to showcase or reproduce:

type User struct {
	Name     *string `gorm:"not null" json:"name" binding:"required,min=4"`
	Password *string `gorm:"not null" json:"password" binding:"required_without=Name,min=3"`
}
@deankarn
Copy link
Contributor

Too many issues with this library!
First off I’m sorry you feel this way, but it is not very constructive.

Thanks for reporting, I’ll try to take a look at the required_* functionality this weekend now that you’ve found a repeatable issue :)

I’m curious about the validation, Name is set to be required always, so the required_without=Name seems unnecessary unless I’m missing something?

Also curious why the Name and password strings are pointers? Maybe there’s a use case beyond this small example?

@deankarn deankarn self-assigned this Jun 10, 2020
@deankarn deankarn added the bug label Jun 10, 2020
@loeffel-io
Copy link
Author

Thanks for reporting, I’ll try to take a look at the required_* functionality this weekend now that you’ve found a repeatable issue :)

Thanks!

I’m curious about the validation, Name is set to be required always, so the required_without=Name seems unnecessary unless I’m missing something?

Just an example

Also curious why the Name and password strings are pointers? Maybe there’s a use case beyond this small example?

There are pointers for the db orm to make sure the fields are NULL

@vuon9
Copy link
Contributor

vuon9 commented Aug 11, 2021

Could u try with required_without=Name,omitempty,min=3?

@johnstcn
Copy link

johnstcn commented Oct 7, 2021

TL;DR omitempty should go after required_with / required_without etc.

Looks like the order is important here. See this playground link for more information.

@hakankaan
Copy link

Could u try with required_without=Name,omitempty,min=3?

This resolved my issue. I could not find that i have to put omitempty.
Thank you!

@jmvbxx
Copy link

jmvbxx commented Jan 9, 2023

@loeffel-io Again, thank you for your question. It looks like the issue was resolved so we're going to go ahead and close this issue. As always, we encourage you to reach out if you have any follow-up questions.

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

No branches or pull requests

6 participants