Skip to content

How to validate time.Duration type? #584

Open
@w84thesun

Description

@w84thesun

Package version eg. v8, v9:

v10

Question:

Is there any way to validate struct field of time.Duration type using this library?
The code sample below throws invalid syntax panic:
panic: strconv.ParseInt: parsing "100ms": invalid syntax

Code sample, to showcase or reproduce:

package main

import (
	"fmt"
	"time"

	"github.com/go-playground/validator/v10"
)

type Config struct {
	Interval time.Duration `validate:"required,gte=100ms"`
}

func main() {
	config := &Config{
		Interval: time.Millisecond,
	}

	validate := validator.New()

	err := validate.Struct(config)
	if err != nil {
		panic(err)
	}

	fmt.Println(config)
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions