Skip to content

Commit

Permalink
ozzo-validation Validate interface implemented for package time (#20)
Browse files Browse the repository at this point in the history
* Country and currency constants moved to separate packages, module version updated to v2, several minor improvements

* added DecimalPlaces method for currency

* Updated readme

* ozzo-validation Validate interface implemented for package time

Co-authored-by: Vadim Maslov <vadim.maslov@finteqhub.com>
  • Loading branch information
vadim-mve and vadim-mve authored Aug 5, 2022
1 parent f22a4da commit 3db7276
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
13 changes: 12 additions & 1 deletion time/date.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package time
import (
"encoding/json"
"encoding/xml"
"gopkg.in/guregu/null.v4"
"fmt"
"time"

"gopkg.in/guregu/null.v4"
)

const format = "2006-01-02"
Expand Down Expand Up @@ -64,6 +66,15 @@ func (d Date) format() *string {
return nil
}

// Validate implementation of ozzo-validation Validate interface
func (d Date) Validate() (err error) {
if !d.Valid {
return fmt.Errorf("'%s' is not valid date", d)
}

return
}

func ParseDateFromString(value string) (Date, error) {
if value == "" {
return Date{}, nil
Expand Down
12 changes: 11 additions & 1 deletion time/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"encoding/json"
"encoding/xml"
"fmt"
"gopkg.in/guregu/null.v4"
"time"

"gopkg.in/guregu/null.v4"
)

type Time null.Time
Expand Down Expand Up @@ -76,6 +77,15 @@ func (t *Time) parseTimeFromString(value string) (err error) {
return nil
}

// Validate implementation of ozzo-validation Validate interface
func (t Time) Validate() (err error) {
if !t.Valid {
return fmt.Errorf("'%s' is not valid time", t)
}

return
}

func ParseNullTimeFromString(value string) (t Time, err error) {
if err = t.parseTimeFromString(value); err != nil {
return t, err
Expand Down

0 comments on commit 3db7276

Please sign in to comment.