Skip to content

Commit

Permalink
Switch to golib lexicon instead of internal validator
Browse files Browse the repository at this point in the history
  • Loading branch information
vkuznet committed Mar 29, 2024
1 parent 909090a commit bb4b87a
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 420 deletions.
6 changes: 4 additions & 2 deletions dbs/buckets.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"fmt"
"io"
"log"

lexicon "github.com/CHESSComputing/golib/lexicon"
)

// Buckets represents Buckets DBS DB table
Expand Down Expand Up @@ -120,11 +122,11 @@ func (r *Buckets) Validate() error {
if err := RecordValidator.Struct(*r); err != nil {
return DecodeValidatorError(r, err)
}
if matched := unixTimePattern.MatchString(fmt.Sprintf("%d", r.CREATE_AT)); !matched {
if matched := lexicon.UnixTimePattern.MatchString(fmt.Sprintf("%d", r.CREATE_AT)); !matched {
msg := "invalid pattern for creation date"
return Error(InvalidParamErr, PatternErrorCode, msg, "dbs.buckets.Validate")
}
if matched := unixTimePattern.MatchString(fmt.Sprintf("%d", r.MODIFY_AT)); !matched {
if matched := lexicon.UnixTimePattern.MatchString(fmt.Sprintf("%d", r.MODIFY_AT)); !matched {
msg := "invalid pattern for last modification date"
return Error(InvalidParamErr, PatternErrorCode, msg, "dbs.buckets.Validate")
}
Expand Down
12 changes: 10 additions & 2 deletions dbs/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io"
"log"

lexicon "github.com/CHESSComputing/golib/lexicon"
"github.com/CHESSComputing/golib/utils"
)

Expand Down Expand Up @@ -85,6 +86,7 @@ func (a *API) GetDataset() error {
func (a *API) InsertDataset() error {
// the API provides Reader which will be used by Decode function to load the HTTP payload
// and cast it to Datasets data structure
log.Println("### InsertDataset", a)

// read given input
data, err := io.ReadAll(a.Reader)
Expand Down Expand Up @@ -114,6 +116,11 @@ func (a *API) InsertDataset() error {
CREATE_BY: a.CreateBy,
MODIFY_BY: a.CreateBy,
}
record.SetDefaults()
err = record.Validate()
if err != nil {
return Error(err, ValidateErrorCode, "validation error", "dbs.datasets.InsertDataset")
}
err = insertParts(&rec, &record)
if err != nil {
return Error(err, CommitErrorCode, "", "dbs.insertRecord")
Expand Down Expand Up @@ -330,10 +337,11 @@ func (r *Datasets) Insert(tx *sql.Tx) error {
//
//gocyclo:ignore
func (r *Datasets) Validate() error {
if err := CheckPattern("did", r.DID); err != nil {
log.Printf("### Validate %+v did=%v", r, lexicon.CheckPattern("did", r.DID))
if err := lexicon.CheckPattern("did", r.DID); err != nil {
return Error(err, PatternErrorCode, "", "dbs.datasets.Validate")
}
if matched := unixTimePattern.MatchString(fmt.Sprintf("%d", r.CREATE_AT)); !matched {
if matched := lexicon.UnixTimePattern.MatchString(fmt.Sprintf("%d", r.CREATE_AT)); !matched {
msg := "invalid pattern for creation date"
return Error(InvalidParamErr, PatternErrorCode, msg, "dbs.datasets.Validate")
}
Expand Down
5 changes: 3 additions & 2 deletions dbs/dbs.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"strings"
"time"

lexicon "github.com/CHESSComputing/golib/lexicon"
validator "github.com/go-playground/validator/v10"
)

Expand Down Expand Up @@ -606,9 +607,9 @@ func OperatorValue(arg string) (string, string) {
func ParseRuns(runs []string) ([]string, error) {
var out []string
for _, v := range runs {
if matched := intPattern.MatchString(v); matched {
if matched := lexicon.IntPattern.MatchString(v); matched {
out = append(out, v)
} else if matched := runRangePattern.MatchString(v); matched {
} else if matched := lexicon.RunRangePattern.MatchString(v); matched {
arr := strings.Split(v, "-")
if len(arr) != 2 {
msg := fmt.Sprintf("fail to parse run-range '%s'", v)
Expand Down
8 changes: 5 additions & 3 deletions dbs/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"io"
"log"
"strings"

lexicon "github.com/CHESSComputing/golib/lexicon"
)

// Files represents Files DBS DB table
Expand Down Expand Up @@ -137,14 +139,14 @@ func (r *Files) Validate() error {
if err := RecordValidator.Struct(*r); err != nil {
return DecodeValidatorError(r, err)
}
if err := CheckPattern("file", r.FILE); err != nil {
if err := lexicon.CheckPattern("file", r.FILE); err != nil {
return Error(err, PatternErrorCode, "", "dbs.files.Validate")
}
if matched := unixTimePattern.MatchString(fmt.Sprintf("%d", r.CREATE_AT)); !matched {
if matched := lexicon.UnixTimePattern.MatchString(fmt.Sprintf("%d", r.CREATE_AT)); !matched {
msg := "invalid pattern for creation date"
return Error(InvalidParamErr, PatternErrorCode, msg, "dbs.files.Validate")
}
if matched := unixTimePattern.MatchString(fmt.Sprintf("%d", r.MODIFY_AT)); !matched {
if matched := lexicon.UnixTimePattern.MatchString(fmt.Sprintf("%d", r.MODIFY_AT)); !matched {
msg := "invalid pattern for last modification date"
return Error(InvalidParamErr, PatternErrorCode, msg, "dbs.files.Validate")
}
Expand Down
6 changes: 4 additions & 2 deletions dbs/parents.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"fmt"
"io"
"log"

lexicon "github.com/CHESSComputing/golib/lexicon"
)

// Parents represents Parents DBS DB table
Expand Down Expand Up @@ -118,11 +120,11 @@ func (r *Parents) Validate() error {
if err := RecordValidator.Struct(*r); err != nil {
return DecodeValidatorError(r, err)
}
if matched := unixTimePattern.MatchString(fmt.Sprintf("%d", r.CREATE_AT)); !matched {
if matched := lexicon.UnixTimePattern.MatchString(fmt.Sprintf("%d", r.CREATE_AT)); !matched {
msg := "invalid pattern for creation date"
return Error(InvalidParamErr, PatternErrorCode, msg, "dbs.parents.Validate")
}
if matched := unixTimePattern.MatchString(fmt.Sprintf("%d", r.MODIFY_AT)); !matched {
if matched := lexicon.UnixTimePattern.MatchString(fmt.Sprintf("%d", r.MODIFY_AT)); !matched {
msg := "invalid pattern for last modification date"
return Error(InvalidParamErr, PatternErrorCode, msg, "dbs.parents.Validate")
}
Expand Down
6 changes: 4 additions & 2 deletions dbs/processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"fmt"
"io"
"log"

lexicon "github.com/CHESSComputing/golib/lexicon"
)

// Processing represents Processing DBS DB table
Expand Down Expand Up @@ -118,11 +120,11 @@ func (r *Processing) Validate() error {
if err := RecordValidator.Struct(*r); err != nil {
return DecodeValidatorError(r, err)
}
if matched := unixTimePattern.MatchString(fmt.Sprintf("%d", r.CREATE_AT)); !matched {
if matched := lexicon.UnixTimePattern.MatchString(fmt.Sprintf("%d", r.CREATE_AT)); !matched {
msg := "invalid pattern for creation date"
return Error(InvalidParamErr, PatternErrorCode, msg, "dbs.processing.Validate")
}
if matched := unixTimePattern.MatchString(fmt.Sprintf("%d", r.MODIFY_AT)); !matched {
if matched := lexicon.UnixTimePattern.MatchString(fmt.Sprintf("%d", r.MODIFY_AT)); !matched {
msg := "invalid pattern for last modification date"
return Error(InvalidParamErr, PatternErrorCode, msg, "dbs.processing.Validate")
}
Expand Down
6 changes: 4 additions & 2 deletions dbs/sites.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"fmt"
"io"
"log"

lexicon "github.com/CHESSComputing/golib/lexicon"
)

// Sites represents Sites DBS DB table
Expand Down Expand Up @@ -118,11 +120,11 @@ func (r *Sites) Validate() error {
if err := RecordValidator.Struct(*r); err != nil {
return DecodeValidatorError(r, err)
}
if matched := unixTimePattern.MatchString(fmt.Sprintf("%d", r.CREATE_AT)); !matched {
if matched := lexicon.UnixTimePattern.MatchString(fmt.Sprintf("%d", r.CREATE_AT)); !matched {
msg := "invalid pattern for creation date"
return Error(InvalidParamErr, PatternErrorCode, msg, "dbs.sites.Validate")
}
if matched := unixTimePattern.MatchString(fmt.Sprintf("%d", r.MODIFY_AT)); !matched {
if matched := lexicon.UnixTimePattern.MatchString(fmt.Sprintf("%d", r.MODIFY_AT)); !matched {
msg := "invalid pattern for last modification date"
return Error(InvalidParamErr, PatternErrorCode, msg, "dbs.sites.Validate")
}
Expand Down
Loading

0 comments on commit bb4b87a

Please sign in to comment.