github.com/blend/go-sdk@v1.20220411.3/validate/time.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package validate
     9  
    10  import (
    11  	"time"
    12  
    13  	"github.com/blend/go-sdk/ex"
    14  )
    15  
    16  // String errors
    17  const (
    18  	ErrTimeBefore ex.Class = "time should be before"
    19  	ErrTimeAfter  ex.Class = "time should be after"
    20  )
    21  
    22  // Time validator singleton.
    23  func Time(value *time.Time) TimeValidators {
    24  	return TimeValidators{value}
    25  }
    26  
    27  // TimeValidators implements validators for time.Time values.
    28  type TimeValidators struct {
    29  	Value *time.Time
    30  }
    31  
    32  // Before returns a validator that a time should be before a given time.
    33  func (t TimeValidators) Before(before time.Time) Validator {
    34  	return func() error {
    35  		if t.Value == nil {
    36  			return Errorf(ErrTimeBefore, nil, "before: %v", before)
    37  		}
    38  		if t.Value.After(before) {
    39  			return Errorf(ErrTimeBefore, *t.Value, "before: %v", before)
    40  		}
    41  		return nil
    42  	}
    43  }
    44  
    45  // BeforeNowUTC returns a validator that a time should be before a given time.
    46  func (t TimeValidators) BeforeNowUTC() Validator {
    47  	return func() error {
    48  		nowUTC := time.Now().UTC()
    49  		if t.Value == nil {
    50  			return Errorf(ErrTimeBefore, nil, "before: %v", nowUTC)
    51  		}
    52  		if t.Value.After(nowUTC) {
    53  			return Errorf(ErrTimeBefore, *t.Value, "before: %v", nowUTC)
    54  		}
    55  		return nil
    56  	}
    57  }
    58  
    59  // After returns a validator that a time should be after a given time.
    60  func (t TimeValidators) After(after time.Time) Validator {
    61  	return func() error {
    62  		if t.Value == nil {
    63  			return Errorf(ErrTimeAfter, nil, "after: %v", after)
    64  		}
    65  		if t.Value.Before(after) {
    66  			return Errorf(ErrTimeAfter, *t.Value, "after: %v", after)
    67  		}
    68  		return nil
    69  	}
    70  }
    71  
    72  // AfterNowUTC returns a validator that a time should be after a given time.
    73  func (t TimeValidators) AfterNowUTC() Validator {
    74  	return func() error {
    75  		nowUTC := time.Now().UTC()
    76  		if t.Value == nil {
    77  			return Errorf(ErrTimeAfter, nil, "after: %v", nowUTC)
    78  		}
    79  		if t.Value.Before(nowUTC) { // if value not after now == value is before now
    80  			return Errorf(ErrTimeAfter, *t.Value, "after: %v", nowUTC)
    81  		}
    82  		return nil
    83  	}
    84  }
    85  
    86  // Between returns a validator that a time should be after a given time.
    87  func (t TimeValidators) Between(start, end time.Time) Validator {
    88  	return func() error {
    89  		if t.Value == nil {
    90  			return Errorf(ErrTimeAfter, nil, "after: %v", start)
    91  		}
    92  		if t.Value.Before(start) {
    93  			return Errorf(ErrTimeAfter, *t.Value, "after: %v", start)
    94  		}
    95  		if t.Value.After(end) {
    96  			return Errorf(ErrTimeBefore, *t.Value, "before: %v", end)
    97  		}
    98  		return nil
    99  	}
   100  }