Skip to content

constraint.DatetimeLessThanOrEqual

marrow16 edited this page Jan 21, 2023 · 5 revisions

Prev Home Next

Constraint: DatetimeLessThanOrEqual

Description

Check that a date/time (as an ISO string) value is less than or equal to a specified 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

dtlte

Fields

Field Type Description
Value string the value to compare against (a string representation of date or datetime in ISO format)
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{
            "foo": {
                Type: valix.JsonDatetime,
                Constraints: valix.Constraints{
                    &valix.DatetimeLessThanOrEqual{
                        Value:   "2022-09-09",
                        ExcTime: true,
                    },
                },
            },
        },
    }

    obj := `{"foo": "2022-09-10"}`
    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 = `{"foo": "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 = `{"foo": "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 {
    Foo *time.Time `json:"foo" v8n:"&dtlte{Value:'2022-09-09', ExcTime: true}"`
}

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

func main() {
    obj := `{"foo": "2022-09-10"}`
    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 = `{"foo": "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 = `{"foo": "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

Clone this wiki locally