Skip to content

constraint.DatetimeFutureOrPresent

marrow16 edited this page Jan 21, 2023 · 6 revisions

Prev Home Next

Constraint: DatetimeFutureOrPresent

Description

Check that a datetime/date (represented as string or time.Time) is in the future or present

V8n Tag Abbreviation

dtfuturep

Fields

Field Type Description
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"
    "time"

    "github.com/marrow16/valix"
)

func main() {
    validator := &valix.Validator{
        Properties: valix.Properties{
            "foo": {
                Type: valix.JsonDatetime,
                Constraints: valix.Constraints{
                    &valix.DatetimeFutureOrPresent{
                        ExcTime: true,
                    },
                },
            },
        },
    }
    obj := "{\"foo\": \"" + time.Now().Add(0-(24*time.Hour)).Format(time.RFC3339) + "\"}"

    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\": \"" + time.Now().Format(time.RFC3339) + "\"}"

    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:"&dtfuturep{ExcTime: true}"`
}

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

func main() {
    obj := "{\"foo\": \"" + time.Now().Add(0-(24*time.Hour)).Format(time.RFC3339) + "\"}"

    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\": \"" + time.Now().Format(time.RFC3339) + "\"}"

    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