validator icon indicating copy to clipboard operation
validator copied to clipboard

Validate time.Time failing

Open pramodrj07 opened this issue 3 years ago • 7 comments

Package version eg. v9, v10:

v10

Issue, Question or Enhancement:

How to validate Start time.Time json:"travel_event_start" validate:"myfunc"

I really do not have an option to change the time.Time to a string

Code sample, to showcase or reproduce:

Error: Bad field type time.Time

pramodrj07 avatar Apr 19 '21 19:04 pramodrj07

@deankarn

pramodrj07 avatar Apr 19 '21 19:04 pramodrj07

Is this being used with JSON binding?

From testing it seems that your example should work no problem. time.Time values are well supported and some built-in validate functions even support them.

What is your custom validation function doing?

BrianLeishman avatar May 05 '21 20:05 BrianLeishman

i have similar issue when validating data with gqlgen

the model im using

type ReminderForm struct {
	Reminder time.Time `bson:"reminder" validate:"required,datetime"`
	Note     *string   `bson:"note"`
}

the error im getting

ad field type time.Time

goroutine 408 [running]:
runtime/debug.Stack(0x1, 0x0, 0x0)
        /usr/local/go/src/runtime/debug/stack.go:24 +0x9f
runtime/debug.PrintStack()
        /usr/local/go/src/runtime/debug/stack.go:16 +0x25
github.com/99designs/gqlgen/graphql.DefaultRecover(0x10a3a40, 0xc0005e6a20, 0xd90040, 0xc0004330f0, 0xc0000b7cb0, 0xd1e692)
        /home/nabihubadah/go/pkg/mod/github.com/99designs/[email protected]/graphql/recovery.go:16 +0xaa
github.com/99designs/gqlgen/graphql.(*OperationContext).Recover(0xc000312400, 0x10a3a40, 0xc0005e6a20, 0xd90040, 0xc0004330f0, 0x9, 0xef8e6e)
        /home/nabihubadah/go/pkg/mod/github.com/99designs/[email protected]/graphql/context_operation.go:110 +0x56
ms.jala.ai/lead/graph/generated.(*executionContext)._Mutation_setReminder.func1(0xc0004323a0, 0xc0004323e0, 0xc00044b210)
        /home/nabihubadah/jala/ms-lead/graph/generated/generated.go:6050 +0x7d
panic(0xd90040, 0xc0004330f0)
        /usr/local/go/src/runtime/panic.go:965 +0x1b9
github.com/go-playground/validator/v10.isDatetime(0x10ae8c8, 0xc0001c2a20, 0x1)
        /home/nabihubadah/go/pkg/mod/github.com/go-playground/validator/[email protected]/baked_in.go:2234 +0x1e5
github.com/go-playground/validator/v10.wrapFunc.func1(0x10a39d0, 0xc000038090, 0x10ae8c8, 0xc0001c2a20, 0xec2101)
        /home/nabihubadah/go/pkg/mod/github.com/go-playground/validator/[email protected]/baked_in.go:39 +0x39
github.com/go-playground/validator/v10.(*validate).traverseField(0xc0001c2a20, 0x10a39d0, 0xc000038090, 0xe328a0, 0xc000078560, 0x99, 0xec21c0, 0xc000078560, 0x99, 0xc000040240, ...)

itsnuba avatar Sep 07 '21 06:09 itsnuba

Same here. Seems datetime validation does not work.

ivanakimov avatar Sep 07 '21 22:09 ivanakimov

datetime validation on a time.Time type wouldn't do anything, since the type is already a time type. datetime validation is to check that a string is a valid date/time string. If you use a time.Time type in your struct, then the JSON unmarshalling will give you decode errors for invalid times.

BrianLeishman avatar Sep 07 '21 22:09 BrianLeishman

@BrianLeishman That makes sense, thanks for the clarification.

ivanakimov avatar Sep 08 '21 00:09 ivanakimov

since this is resolved by @BrianLeishman's comment, can we close it?

YashKumarVerma avatar Jul 11 '22 06:07 YashKumarVerma

Is there no datetime validation in v9?

Kartikdad-dunzo avatar Mar 09 '23 08:03 Kartikdad-dunzo

Is there no datetime validation in v9?

There is no mention of datetime in the docs for v9 as far as I can tell https://pkg.go.dev/gopkg.in/go-playground/[email protected]

BrianLeishman avatar Mar 09 '23 13:03 BrianLeishman

There is none in V9. Closing this as the original question is answered.

zemzale avatar Mar 10 '23 07:03 zemzale