Skip to content

constraint.DatetimeTimeOfDayRange

marrow16 edited this page Jan 21, 2023 · 4 revisions

Prev Home Next

Constraint: DatetimeTimeOfDayRange

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

Description

V8n Tag Abbreviation

dttodrange

Fields

Field Type Description
Minimum string is the minimum time of day (if this is empty, then no minimum check is performed)
Maximum string is the maximum time of day (if this is empty, then no maximum check is performed)
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{
            "deliveryTime": {
                Type: valix.JsonDatetime,
                Constraints: valix.Constraints{
                    &valix.DatetimeTimeOfDayRange{
                        Minimum: "09:00",
                        Maximum: "17:00",
                        Message: "Sorry, we only deliver between 09:00 and 17:00",
                    },
                },
            },
        },
    }

    ok, violations, _ := validator.ValidateString(`{"deliveryTime": "2022-09-23T08:30: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(`{"deliveryTime": "2022-09-23T17:10: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(`{"deliveryTime": "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)
    }
}

try on go-playground

Struct v8n tag example...
package main

import (
    "fmt"
    "time"

    "github.com/marrow16/valix"
)

type MyStruct struct {
    DeliveryTime *time.Time `json:"deliveryTime" v8n:"&dttodrange{min:'09:00', max:'17:00', message:'Sorry, we only deliver between 09:00 and 17:00'}"`
}

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

func main() {
    my := &MyStruct{}

    ok, violations, _ := validator.ValidateStringInto(`{"deliveryTime": "2022-09-23T08:30:00Z"}`, my)
    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.ValidateStringInto(`{"deliveryTime": "2022-09-23T17:10:00Z"}`, my)
    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.ValidateStringInto(`{"deliveryTime": "2022-09-23T12:00:00Z"}`, my)
    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