如何在Go语言中使用ozzo验证库进行最大时间验证。

huangapple go评论86阅读模式
英文:

How to make time maximum validation with ozzo validation in golang

问题

我想要创建一个时间的最大输入,格式为时间(例如23:30:00),最大时间为09:30:00。我尝试使用ozzo validation进行验证,并找到了函数及其调用"Date(layout string)"。

这是文档链接:https://github.com/go-ozzo/ozzo-validation

从文档中我看到函数Date有Min和Max来检查指定的范围,但问题是我不知道如何填写参数。数据类型是time.Time。

以下是我的代码:

if err := validation.Validate(c.ReleasedTime, validation.Date("15:04:05").Max(????)); err != nil {
	logger.E(err)
	return shared.NewMultiStringValidationError(shared.HTTPErrorBadRequest, map[string]string{
		"en": "Format date",
		"id": "format tanggal",
	})
}

在这里,我需要填写Max参数"???",因为我还不确定如何填写它。
也许你们可以帮助我找到这个解决方案,或者使用其他包进行验证,我将非常感激。谢谢。

英文:

I want to make a maximum input for time with format time like (23:30:00) and the maximum time is (09:30:00), i tried the validation using ozzo validation and i find the function and its call "Date(layout string)"

It is the documentation https://github.com/go-ozzo/ozzo-validation

From the documentation i see that the function Date has Min and Max to check specified range but the problem is i don't know how to fill the arguments. The data type is time.Time.

Here is my code

if err := validation.Validate(c.ReleasedTime, validation.Date("15:04:05").Max(????)); err != nil {
	logger.E(err)
	return shared.NewMultiStringValidationError(shared.HTTPErrorBadRequest, map[string]string{
		"en": "Format date",
		"id": "format tanggal",
	})
}

From there i fill the Max arguments "???" because i still confuse how to fill it.
Maybe you all can help me to find the this solution or make this validation using another package, i'll be appreciated. Thank you

答案1

得分: 2

在测试代码中有一个示例。

你可以在这里看到它:https://github.com/go-ozzo/ozzo-validation/blob/master/date_test.go#L71

但是当我们只需要比较小时时,需要进行一些小的修改。

import (
	"fmt"
	"time"

	validation "github.com/go-ozzo/ozzo-validation/v4"
)

func main() {
	layout := "2006-01-02T15:04:05"
    // 这里添加了一个基准日期到小时,这样我们就有了一个有效的time.Time对象。
	base := "2020-01-01"
	max, _ := time.Parse(layout, base+"T"+"23:30:00")
	fmt.Println(max)
	r := validation.Date(layout).Max(max)
	fmt.Println(r.Validate(base + "T" + "09:00:00")) // 正常
	fmt.Println(r.Validate(base + "T" + "23:40:00")) // 这里应该会引发错误
}

英文:

There's an example in the testing code.

You could see it here: https://github.com/go-ozzo/ozzo-validation/blob/master/date_test.go#L71

But when we only need compare hour, so it will be a little hack here.

import (
	"fmt"
	"time"

	validation "github.com/go-ozzo/ozzo-validation/v4"
)

func main() {
	layout := "2006-01-02T15:04:05"
    // this add a base date to hour, so we have a valid time.Time object.
	base := "2020-01-01"
	max, _ := time.Parse(layout, base+"T"+"23:30:00")
	fmt.Println(max)
	r := validation.Date(layout).Max(max)
	fmt.Println(r.Validate(base + "T" + "09:00:00")) // ok
	fmt.Println(r.Validate(base + "T" + "23:40:00")) // this should raise error
}

huangapple
  • 本文由 发表于 2022年5月19日 11:04:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/72298154.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定