Skip to content

Commit

Permalink
add Validation
Browse files Browse the repository at this point in the history
  • Loading branch information
vkuznet committed Dec 20, 2024
1 parent 14d7ea1 commit 1356fc3
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 2 deletions.
38 changes: 38 additions & 0 deletions data/int_environments.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"description": "test environment insert API",
"method": "POST",
"endpoint": "/environment",
"url": "/environment",
"input": {
"name": "environment1", "version": "version", "details": "details"
},
"output": [],
"verbose": 0,
"code": 200
},
{
"description": "test environment insert API with parent environment",
"method": "POST",
"endpoint": "/environment",
"url": "/environment",
"input": {
"name": "environment2", "version": "version", "details": "details", "parent_environment": "environment1"
},
"output": [],
"verbose": 0,
"code": 200
},
{
"description": "test environment insert API with parent environment",
"method": "GET",
"endpoint": "/environment",
"url": "/environment",
"input": {
"name": "environment2"
},
"output": [],
"verbose": 2,
"code": 200
}
]
38 changes: 38 additions & 0 deletions data/int_scripts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"description": "test script insert API",
"method": "POST",
"endpoint": "/script",
"url": "/script",
"input": {
"name": "script1", "options": "-m -p"
},
"output": [],
"verbose": 0,
"code": 200
},
{
"description": "test script insert API with parent script",
"method": "POST",
"endpoint": "/script",
"url": "/script",
"input": {
"name": "script2", "options": "-m -p", "parent_script": "script1"
},
"output": [],
"verbose": 0,
"code": 200
},
{
"description": "test script insert API with parent script",
"method": "GET",
"endpoint": "/script",
"url": "/script",
"input": {
"name": "script2"
},
"output": [],
"verbose": 2,
"code": 200
}
]
1 change: 1 addition & 0 deletions dbs/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (a *API) GetDataset() error {
return nil
}

// InsertDataset inserts dataset into database
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
Expand Down
19 changes: 19 additions & 0 deletions dbs/environmentrecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package dbs
import (
"database/sql"
"log"

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

// EnvironmentRecord represents data input for environment record
Expand Down Expand Up @@ -36,3 +38,20 @@ func (e *EnvironmentRecord) Insert(tx *sql.Tx) (int64, error) {
err := r.Insert(tx)
return r.ENVIRONMENT_ID, err
}

// Validate implementation of EnvironmentRecord
func (r *EnvironmentRecord) Validate() error {
if err := lexicon.CheckPattern("env_name", r.Name); err != nil {
return Error(err, PatternErrorCode, "fail env.Name validation", "dbs.datasets.DatasetRecord.Validate")
}
if err := lexicon.CheckPattern("env_version", r.Version); err != nil {
return Error(err, PatternErrorCode, "fail env.Version validation", "dbs.datasets.DatasetRecord.Validate")
}
if err := lexicon.CheckPattern("env_details", r.Details); err != nil {
return Error(err, PatternErrorCode, "fail env.Details validation", "dbs.datasets.DatasetRecord.Validate")
}
if err := lexicon.CheckPattern("env_parent", r.Parent); err != nil {
return Error(err, PatternErrorCode, "fail env.Parent validation", "dbs.datasets.DatasetRecord.Validate")
}
return nil
}
9 changes: 7 additions & 2 deletions dbs/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,18 @@ func (a *API) InsertEnvironment() error {
data, err := io.ReadAll(a.Reader)
if err != nil {
log.Println("fail to read data", err)
return Error(err, ReaderErrorCode, "", "dbs.scripts.InsertEnvironment")
return Error(err, ReaderErrorCode, "", "dbs.environments.InsertEnvironment")
}
err = json.Unmarshal(data, &rec)
if err != nil {
msg := fmt.Sprintf("fail to decode record")
log.Println(msg)
return Error(err, DecodeErrorCode, msg, "dbs.scripts.InsertEnvironment")
return Error(err, DecodeErrorCode, msg, "dbs.environments.InsertEnvironment")
}

err = rec.Validate()
if err != nil {
return Error(err, ValidateErrorCode, "validation error", "dbs.environments.InsertEnvironment")
}

// start transaction
Expand Down
16 changes: 16 additions & 0 deletions dbs/osinforecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package dbs
import (
"database/sql"
"log"

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

// OsInfoRecord represent input os info record
Expand All @@ -26,3 +28,17 @@ func (o *OsInfoRecord) Insert(tx *sql.Tx) (int64, error) {
err := r.Insert(tx)
return r.OS_ID, err
}

// Validate implementation of OsInfoRecord
func (r *OsInfoRecord) Validate() error {
if err := lexicon.CheckPattern("osinfo_name", r.Name); err != nil {
return Error(err, PatternErrorCode, "fail osinfo.name validation", "dbs.datasets.DatasetRecord.Validate")
}
if err := lexicon.CheckPattern("osinfo_version", r.Version); err != nil {
return Error(err, PatternErrorCode, "fail osinfo.version validation", "dbs.datasets.DatasetRecord.Validate")
}
if err := lexicon.CheckPattern("osinfo_kernel", r.Kernel); err != nil {
return Error(err, PatternErrorCode, "fail osinfo.name validation", "dbs.datasets.DatasetRecord.Validate")
}
return nil
}
16 changes: 16 additions & 0 deletions dbs/scriptrecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package dbs
import (
"database/sql"
"log"

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

// ScriptRecord represents script input data record
Expand Down Expand Up @@ -35,3 +37,17 @@ func (e *ScriptRecord) Insert(tx *sql.Tx) (int64, error) {
err := r.Insert(tx)
return r.SCRIPT_ID, err
}

// Validate implementation of ScriptRecord
func (r *ScriptRecord) Validate() error {
if err := lexicon.CheckPattern("script_name", r.Name); err != nil {
return Error(err, PatternErrorCode, "fail script.Name validation", "dbs.datasets.DatasetRecord.Validate")
}
if err := lexicon.CheckPattern("script_options", r.Options); err != nil {
return Error(err, PatternErrorCode, "fail script.Options validation", "dbs.datasets.DatasetRecord.Validate")
}
if err := lexicon.CheckPattern("script_parent", r.Parent); err != nil {
return Error(err, PatternErrorCode, "fail script.Parent validation", "dbs.datasets.DatasetRecord.Validate")
}
return nil
}
5 changes: 5 additions & 0 deletions dbs/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ func (a *API) InsertScript() error {
return Error(err, DecodeErrorCode, msg, "dbs.scripts.InsertScript")
}

err = rec.Validate()
if err != nil {
return Error(err, ValidateErrorCode, "validation error", "dbs.scripts.InsertScript")
}

// start transaction
tx, err := DB.Begin()
if err != nil {
Expand Down

0 comments on commit 1356fc3

Please sign in to comment.