validator icon indicating copy to clipboard operation
validator copied to clipboard

Errors thrown by `golangci-lint` for validator package

Open echarrod opened this issue 3 years ago • 4 comments

  • [X] I have looked at the documentation here first?
  • [X] I have looked at the examples provided that may showcase my question here?

Package version eg. v9, v10:

v9.31.0

Issue, Question or Enhancement:

When using the golangci-lint watcher provided by GoLand: image Every time a file is saved, I get errors from the validator package:

mypath/my_file.go:245:14: undeclared name: `validator` (typecheck)
	validate := validator.New()

and also:

mypath/my_file.go:16:2: "gopkg.in/go-playground/validator.v9" imported but not used (typecheck)
	"gopkg.in/go-playground/validator.v9"

For each one of the files I am using the package: image

I can see it runs the following command: golangci-lint run --disable=typecheck

Code sample, to showcase or reproduce:

https://goplay.tools/snippet/b--sydpv40H

package main

import (
	"github.com/gofrs/uuid"
	"gopkg.in/go-playground/validator.v9"
)

type MyRequest struct {
	UserID uuid.UUID `json:"user_id" validate:"required"`
}

func main() {
	request := &MyRequest{UserID: uuid.Must(uuid.NewV4())}

	validate := validator.New()
	err := validate.Struct(request)
	if err != nil {
		panic(err)
	}
}

Save file, then run:

golangci-lint run --disable=typecheck yourfile.go

echarrod avatar Jun 22 '22 14:06 echarrod

Are you sure your import path is correct ?

Instead of "gopkg.in/go-playground/validator.v9", "github.com/go-playground/validator/v10" is the path used in the examples and it is also the one referenced on https://pkg.go.dev/github.com/go-playground/validator/v10

hjwk avatar Jul 29 '22 09:07 hjwk

Hi, I believe this was the older recommended way of importing the package. Since v10, I know they have github.com/go-playground/validator/v10 on the docs. I also tried with this import, and get the same result

echarrod avatar Aug 06 '22 10:08 echarrod

have you tried to run your code ? Because it looks like the validator package was simply not downloaded and thus golangci-lint is unable to find a declaration for the validator object and throws the error/warning

hjwk avatar Aug 06 '22 13:08 hjwk

Yes, the code runs fine, but just when the watcher runs intermittently I get this error. It behaves functionally fine, as in it builds and runs, but this output just gets returned during the watcher run

echarrod avatar Aug 16 '22 14:08 echarrod