Skip to content

constraint.DatetimeLessThanOther

marrow16 edited this page Jan 21, 2023 · 5 revisions

Prev Home Next

Constraint: DatetimeLessThanOther

Description

Check that a date/time (as an ISO string) value is less than another named property value

Note: this constraint is strict - if either of the compared values is not a valid ISO datetime then this constraint fails

V8n Tag Abbreviation

dtlto

Fields

Field Type Description
PropertyName string the property name of the other value to compare against
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
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{
            "startDate": {
                Type: valix.JsonDatetime,
                Constraints: valix.Constraints{
                    &valix.DatetimeLessThanOther{
                        PropertyName: "endDate",
                        ExcTime:      true,
                    },
                },
            },
            "endDate": {
                Type: valix.JsonDatetime,
            },
        },
    }

    obj := `{"startDate": "2022-09-09", "endDate": "2022-09-08"}`
    ok, violations, _ := validator.ValidateString(obj)
    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)
    }

    obj = `{"startDate": "2022-09-08", "endDate": "2022-09-08"}`
    ok, violations, _ = validator.ValidateString(obj)
    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)
    }

    obj = `{"startDate": "2022-09-08", "endDate": "2022-09-09"}`
    ok, violations, _ = validator.ValidateString(obj)
    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 {
    StartDate *time.Time `json:"startDate" v8n:"&dtlto{PropertyName:'endDate'}"`
    EndDate   *time.Time `json:"endDate"`
}

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

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

    obj := `{"startDate": "2022-09-09T00:00:00Z", "endDate": "2022-09-08T00:00:00Z"}`
    ok, violations, _ := validator.ValidateStringInto(obj, 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)
    }

    obj = `{"startDate": "2022-09-09T00:00:00Z", "endDate": "2022-09-09T00:00:00Z"}`
    ok, violations, _ = validator.ValidateStringInto(obj, 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)
    }

    obj = `{"startDate": "2022-09-08T00:00:00Z", "endDate": "2022-09-09T00:00:00Z"}`
    ok, violations, _ = validator.ValidateStringInto(obj, 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