date_constraints

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2020 License: MIT Imports: 6 Imported by: 10

README

Date Constraints

Validate a date against constraints

Overview GoDoc

This module is heavily based on https://github.com/Masterminds/semver so kudos to Masterminds.

For now only RFC3339 dates are supported

Basic Comparisons

There are two elements to the comparisons. First, a comparison string is a list of space or comma separated AND comparisons. These are then separated by || (OR) comparisons. For example, ">= 2020-03-01T00:00:00Z < 2020-04-01T00:00:00Z || >= 2020-05-01T00:00:00Z" is will validate if a date is between 01/03/2020 till 01/04/2020 OR it's after 01/05/2020.

The basic comparisons are:

  • =: equal
  • !=: not equal
  • >: greater than
  • <: less than
  • >=: greater than or equal to
  • <=: less than or equal to

Usage


import "github.com/reflog/dateconstraints"
import "time"

func main(){

    date, _ := time.Parse(time.RFC3339, "2020-03-10T00:00:00Z")
    c, _ := date_constraints.NewConstraint("> 2020-03-01T00:00:00Z <= 2020-04-01T00:00:00Z")
    if c.Check(&date) {
        // date is in range!
    }
}

Validation

In addition to testing a date against a constraint, it can be validated against a constraint. When validation fails a slice of errors containing why a date didn't meet the constraint is returned. For example,

c, err := date_constraints.NewConstraint("<= 2020-03-01T00:00:00Z, >= 2020-04-10T00:00:00Z")
if err != nil {
    // Handle constraint not being parseable.
}
v, err := time.Parse(time.RFC3339, "2020-03-10T00:00:00Z")
if err != nil {
    // Handle date not being parseable.
}
// Validate a date against a constraint.
a, msgs := c.Validate(&v)
// a is false
for _, m := range msgs {
    fmt.Println(m)
    // Loops over the errors which would read
    // "2020-03-10T00:00:00Z is greater than 2020-03-01T00:00:00Z"
    // "2020-03-01T00:00:00Z is less than 2020-04-01T00:00:00Z"
}

Install

go get github.com/reflog/dateconstraints

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Constraints

type Constraints struct {
	// contains filtered or unexported fields
}

Constraints is one or more constraint that a date can be checked against.

func NewConstraint

func NewConstraint(c string) (*Constraints, error)

NewConstraint returns a Constraints instance that a time.Time instance can be checked against. If there is a parse error it will be returned.

func (Constraints) Check

func (cs Constraints) Check(v *time.Time) bool

Check tests if a date satisfies the constraints.

func (Constraints) String

func (cs Constraints) String() string

func (Constraints) Validate

func (cs Constraints) Validate(v *time.Time) (bool, []error)

Validate checks if a date satisfies a constraint. If not a slice of reasons for the failure are returned in addition to a bool.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL