From 1356fc35db81d4cd2aec5629df9b03afe4f2b0ec Mon Sep 17 00:00:00 2001 From: Valentin Kuznetsov Date: Fri, 20 Dec 2024 14:45:16 -0500 Subject: [PATCH] add Validation --- data/int_environments.json | 38 ++++++++++++++++++++++++++++++++++++++ data/int_scripts.json | 38 ++++++++++++++++++++++++++++++++++++++ dbs/datasets.go | 1 + dbs/environmentrecord.go | 19 +++++++++++++++++++ dbs/environments.go | 9 +++++++-- dbs/osinforecord.go | 16 ++++++++++++++++ dbs/scriptrecord.go | 16 ++++++++++++++++ dbs/scripts.go | 5 +++++ 8 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 data/int_environments.json create mode 100644 data/int_scripts.json diff --git a/data/int_environments.json b/data/int_environments.json new file mode 100644 index 0000000..74852f8 --- /dev/null +++ b/data/int_environments.json @@ -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 + } +] diff --git a/data/int_scripts.json b/data/int_scripts.json new file mode 100644 index 0000000..d9e8bef --- /dev/null +++ b/data/int_scripts.json @@ -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 + } +] diff --git a/dbs/datasets.go b/dbs/datasets.go index 568097d..6ff105c 100644 --- a/dbs/datasets.go +++ b/dbs/datasets.go @@ -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 diff --git a/dbs/environmentrecord.go b/dbs/environmentrecord.go index 8284c0d..b2f6018 100644 --- a/dbs/environmentrecord.go +++ b/dbs/environmentrecord.go @@ -3,6 +3,8 @@ package dbs import ( "database/sql" "log" + + lexicon "github.com/CHESSComputing/golib/lexicon" ) // EnvironmentRecord represents data input for environment record @@ -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 +} diff --git a/dbs/environments.go b/dbs/environments.go index 6bf6af4..6840366 100644 --- a/dbs/environments.go +++ b/dbs/environments.go @@ -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 diff --git a/dbs/osinforecord.go b/dbs/osinforecord.go index 0c57548..b73757b 100644 --- a/dbs/osinforecord.go +++ b/dbs/osinforecord.go @@ -3,6 +3,8 @@ package dbs import ( "database/sql" "log" + + lexicon "github.com/CHESSComputing/golib/lexicon" ) // OsInfoRecord represent input os info record @@ -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 +} diff --git a/dbs/scriptrecord.go b/dbs/scriptrecord.go index 8672f2d..10b6a82 100644 --- a/dbs/scriptrecord.go +++ b/dbs/scriptrecord.go @@ -3,6 +3,8 @@ package dbs import ( "database/sql" "log" + + lexicon "github.com/CHESSComputing/golib/lexicon" ) // ScriptRecord represents script input data record @@ -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 +} diff --git a/dbs/scripts.go b/dbs/scripts.go index d3f2a77..6995f36 100644 --- a/dbs/scripts.go +++ b/dbs/scripts.go @@ -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 {