Skip to content

constraint.DatetimeRange

marrow16 edited this page Jan 21, 2023 · 5 revisions

Prev Home Next

Constraint: DatetimeRange

Description

Check that a datetime (represented as string or time.Time) is within a specified time of day range

Note: this constraint is strict - if the property value is not a valid ISO date/datetime then this constraint fails

V8n Tag Abbreviation

dtrange

Fields

Field Type Description
Minimum string is the minimum datetime/date (if this is empty, then no minimum check is performed)
Maximum string is the maximum datetime/date (if this is empty, then no maximum check is performed)
ExcTime bool when set to true, excludes the time when comparing.
Note: This also excludes the effect of any timezone offsets specified in either of the compared values
ExclusiveMin bool if set to true, specifies the minimum value is exclusive
ExclusiveMax bool if set to true, specifies the maximum value is exclusive
Message string the violation message to be used if the constraint fails. If empty, the default message is used
Stop bool when set to true, Stop prevents further validation checks on the property if this constraint fails

Examples

Programmatic example...
package main

import (
    "fmt"

    "github.com/marrow16/valix"
)

func main() {
    validator := &valix.Validator{
        Properties: valix.Properties{
            "testDate": {
                Type: valix.JsonDatetime,
                Constraints: valix.Constraints{
                    &valix.DatetimeRange{
                        Minimum: "2022-09-24",
                        Maximum: "2022-09-25",
                    },
                },
            },
        },
    }

    ok, violations, _ := validator.ValidateString(`{"testDate": "2022-09-23T12:00:00Z"}`)
    fmt.Printf("Passed? %v\n", ok)
    for i, v := range violations {
        fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
    }

    ok, violations, _ = validator.ValidateString(`{"testDate": "2022-09-26T12:00:00Z"}`)
    fmt.Printf("Passed? %v\n", ok)
    for i, v := range violations {
        fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
    }

    ok, violations, _ = validator.ValidateString(`{"testDate": "2022-09-24T12:00:00Z"}`)
    fmt.Printf("Passed? %v\n", ok)
    for i, v := range violations {
        fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
    }
}

try on go-playground

Struct v8n tag example...
package main

import (
    "fmt"
    "time"

    "github.com/marrow16/valix"
)

type MyStruct struct {
    TestDate *time.Time `json:"testDate" v8n:"&dtrange{min:'2022-09-24', max:'2022-09-25'}"`
}

var validator = valix.MustCompileValidatorFor(MyStruct{}, nil)

func main() {
    ok, violations, _ := validator.ValidateString(`{"testDate": "2022-09-23T12:00:00Z"}`)
    fmt.Printf("Passed? %v\n", ok)
    for i, v := range violations {
        fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
    }

    ok, violations, _ = validator.ValidateString(`{"testDate": "2022-09-26T12:00:00Z"}`)
    fmt.Printf("Passed? %v\n", ok)
    for i, v := range violations {
        fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
    }

    ok, violations, _ = validator.ValidateString(`{"testDate": "2022-09-24T12:00:00Z"}`)
    fmt.Printf("Passed? %v\n", ok)
    for i, v := range violations {
        fmt.Printf("Violation[%d] Message: %s, Property: %s, Path: %s\n", i+1, v.Message, v.Property, v.Path)
    }
}

try on go-playground

Clone this wiki locally