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

Panic due to invalid validation struct tag #1

Open
romshark opened this issue Jul 4, 2024 · 0 comments
Open

Panic due to invalid validation struct tag #1

romshark opened this issue Jul 4, 2024 · 0 comments
Assignees

Comments

@romshark
Copy link
Owner

romshark commented Jul 4, 2024

This isn't really a bug of yamagiconf, instead this is rather undesired behavior caused by github.com/go-playground/validator.

The following code produces a panic but it probably should return an error instead.

package main

import (
	"fmt"
	"net/url"
	"os"

	"github.com/romshark/yamagiconf"
)

const conf = `
host: https://localhost:8080/path
`

type Config struct {
	Host URL `yaml:"host" validate:"url,required"`
}

type URL struct{ URL *url.URL }

func (u *URL) UnmarshalText(t []byte) error {
	x, err := url.Parse(string(t))
	if err != nil {
		return err
	}
	u.URL = x
	return nil
}

func main() {
	var c Config
	err := yamagiconf.Load(conf, &c)
	if err != nil {
		fmt.Println("ERR:", err)
		os.Exit(1)
	}
	fmt.Println(c)
}

https://go.dev/play/p/7J_UIHUbkZC

panic: Bad field type main.URL

goroutine 1 [running]:
github.com/go-playground/validator/v10.isURL({0x104a46ba0?, 0x14000000900?})
        /Users/romshark/go/pkg/mod/github.com/go-playground/validator/[email protected]/baked_in.go:1494 +0x124
github.com/go-playground/validator/v10.New.wrapFunc.func3({0x104a1e0e0?, 0x104a47bf8?}, {0x104a46ba0?, 0x14000000900?})
        /Users/romshark/go/pkg/mod/github.com/go-playground/validator/[email protected]/baked_in.go:43 +0x34
github.com/go-playground/validator/v10.(*validate).traverseField(0x14000000900, {0x104a45aa0, 0x104bea200}, {0x104a1e160?, 0x1400015e1b0?, 0x14000159cd8?}, {0x104a1e0e0?, 0x1400015e1b0?, 0x104c1fde8?}, {0x1400001c400, ...}, ...)
        /Users/romshark/go/pkg/mod/github.com/go-playground/validator/[email protected]/validator.go:456 +0xd54
github.com/go-playground/validator/v10.(*validate).validateStruct(0x14000000900, {0x104a45aa0, 0x104bea200}, {0x104a1e160?, 0x1400015e1b0?, 0x1?}, {0x104a1e160?, 0x1400015e1b0?, 0x0?}, {0x104a47bf8, ...}, ...)
        /Users/romshark/go/pkg/mod/github.com/go-playground/validator/[email protected]/validator.go:78 +0x518
github.com/go-playground/validator/v10.(*Validate).StructCtx(0x14000178070, {0x104a45aa0, 0x104bea200}, {0x104a1e160, 0x1400015e1b0})
        /Users/romshark/go/pkg/mod/github.com/go-playground/validator/[email protected]/validator_instance.go:395 +0x330
github.com/go-playground/validator/v10.(*Validate).Struct(...)
        /Users/romshark/go/pkg/mod/github.com/go-playground/validator/[email protected]/validator_instance.go:368
main.main()
        /Users/romshark/Work/yamagiconf/cmd/panictest/main.go:32 +0x7c
exit status 2
@romshark romshark self-assigned this Jul 4, 2024
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