You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by de...@apache.org on 2018/03/01 20:33:48 UTC
[incubator-trafficcontrol] 02/02: add validation to physlocations
This is an automated email from the ASF dual-hosted git repository.
dewrich pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-trafficcontrol.git
commit c27480ebf478a167077fb8376f4e470b46550037
Author: Dan Kirkwood <da...@apache.org>
AuthorDate: Wed Feb 28 13:32:45 2018 -0700
add validation to physlocations
---
lib/go-tc/physlocations.go | 60 ++++++++++------------
.../physlocation/phys_locations.go | 23 ++++++---
.../physlocation/phys_locations_test.go | 22 +++++++-
3 files changed, 65 insertions(+), 40 deletions(-)
diff --git a/lib/go-tc/physlocations.go b/lib/go-tc/physlocations.go
index 4d506b8..5942969 100644
--- a/lib/go-tc/physlocations.go
+++ b/lib/go-tc/physlocations.go
@@ -19,25 +19,21 @@ package tc
* under the License.
*/
-type PhysLocationsResponse struct {
- Response []PhysLocation `json:"response"`
-}
-
type PhysLocation struct {
- Address string `json:"address" db:"address"`
- City string `json:"city" db:"city"`
- Comments string `json:"comments" db:"comments"`
- Email string `json:"email" db:"email"`
- ID int `json:"id" db:"id"`
- LastUpdated Time `json:"lastUpdated" db:"last_updated"`
- Name string `json:"name" db:"name"`
- Phone string `json:"phone" db:"phone"`
- POC string `json:"poc" db:"poc"`
- RegionID int `json:"regionId" db:"region"`
- RegionName string `json:"region" db:"region_name"`
- ShortName string `json:"shortName" db:"short_name"`
- State string `json:"state" db:"state"`
- Zip string `json:"zip" db:"zip"`
+ Address string `json:"address" db:"address"`
+ City string `json:"city" db:"city"`
+ Comments string `json:"comments" db:"comments"`
+ Email string `json:"email" db:"email"`
+ ID int `json:"id" db:"id"`
+ LastUpdated TimeNoMod `json:"lastUpdated" db:"last_updated"`
+ Name string `json:"name" db:"name"`
+ Phone string `json:"phone" db:"phone"`
+ POC string `json:"poc" db:"poc"`
+ RegionID int `json:"regionId" db:"region"`
+ RegionName string `json:"region" db:"region_name"`
+ ShortName string `json:"shortName" db:"short_name"`
+ State string `json:"state" db:"state"`
+ Zip string `json:"zip" db:"zip"`
}
// PhysLocationNullable - a struct version that allows for all fields to be null
@@ -45,18 +41,18 @@ type PhysLocationNullable struct {
//
// NOTE: the db: struct tags are used for testing to map to their equivalent database column (if there is one)
//
- Address *string `json:"address" db:"address"`
- City *string `json:"city" db:"city"`
- Comments *string `json:"comments" db:"comments"`
- Email *string `json:"email" db:"email"`
- ID *int `json:"id" db:"id"`
- LastUpdated Time `json:"lastUpdated" db:"last_updated"`
- Name *string `json:"name" db:"name"`
- Phone *string `json:"phone" db:"phone"`
- POC *string `json:"poc" db:"poc"`
- RegionID *int `json:"regionId" db:"region"`
- RegionName *string `json:"region" db:"region_name"`
- ShortName *string `json:"shortName" db:"short_name"`
- State *string `json:"state" db:"state"`
- Zip *string `json:"zip" db:"zip"`
+ Address *string `json:"address" db:"address"`
+ City *string `json:"city" db:"city"`
+ Comments *string `json:"comments" db:"comments"`
+ Email *string `json:"email" db:"email"`
+ ID *int `json:"id" db:"id"`
+ LastUpdated TimeNoMod `json:"lastUpdated" db:"last_updated"`
+ Name *string `json:"name" db:"name"`
+ Phone *string `json:"phone" db:"phone"`
+ POC *string `json:"poc" db:"poc"`
+ RegionID *int `json:"regionId" db:"region"`
+ RegionName *string `json:"region" db:"region_name"`
+ ShortName *string `json:"shortName" db:"short_name"`
+ State *string `json:"state" db:"state"`
+ Zip *string `json:"zip" db:"zip"`
}
diff --git a/traffic_ops/traffic_ops_golang/physlocation/phys_locations.go b/traffic_ops/traffic_ops_golang/physlocation/phys_locations.go
index 7632343..451fd92 100644
--- a/traffic_ops/traffic_ops_golang/physlocation/phys_locations.go
+++ b/traffic_ops/traffic_ops_golang/physlocation/phys_locations.go
@@ -29,6 +29,8 @@ import (
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/api"
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/auth"
"github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/dbhelpers"
+ "github.com/apache/incubator-trafficcontrol/traffic_ops/traffic_ops_golang/tovalidate"
+ validation "github.com/go-ozzo/ozzo-validation"
"github.com/jmoiron/sqlx"
"github.com/lib/pq"
)
@@ -70,12 +72,19 @@ func (pl *TOPhysLocation) SetID(i int) {
}
func (pl *TOPhysLocation) Validate(db *sqlx.DB) []error {
- errs := []error{}
- name := pl.Name
- if name != nil && len(*name) < 1 {
- errs = append(errs, errors.New(`PhysLocation 'name' is required.`))
+ errs := validation.Errors{
+ "address": validation.Validate(pl.Address, validation.Required),
+ "city": validation.Validate(pl.City, validation.Required),
+ "name": validation.Validate(pl.Name, validation.Required),
+ "regionId": validation.Validate(pl.RegionID, validation.Required, validation.Min(0)),
+ "shortName": validation.Validate(pl.ShortName, validation.Required),
+ "state": validation.Validate(pl.State, validation.Required),
+ "zip": validation.Validate(pl.Zip, validation.Required),
}
- return errs
+ if errs != nil {
+ return tovalidate.ToErrors(errs)
+ }
+ return nil
}
func (pl *TOPhysLocation) Read(db *sqlx.DB, parameters map[string]string, user auth.CurrentUser) ([]interface{}, []error, tc.ApiErrorType) {
@@ -177,7 +186,7 @@ func (pl *TOPhysLocation) Update(db *sqlx.DB, user auth.CurrentUser) (error, tc.
}
defer resultRows.Close()
- var lastUpdated tc.Time
+ var lastUpdated tc.TimeNoMod
rowsAffected := 0
for resultRows.Next() {
rowsAffected++
@@ -242,7 +251,7 @@ func (pl *TOPhysLocation) Create(db *sqlx.DB, user auth.CurrentUser) (error, tc.
defer resultRows.Close()
var id int
- var lastUpdated tc.Time
+ var lastUpdated tc.TimeNoMod
rowsAffected := 0
for resultRows.Next() {
rowsAffected++
diff --git a/traffic_ops/traffic_ops_golang/physlocation/phys_locations_test.go b/traffic_ops/traffic_ops_golang/physlocation/phys_locations_test.go
index ff61e43..dd16bcf 100644
--- a/traffic_ops/traffic_ops_golang/physlocation/phys_locations_test.go
+++ b/traffic_ops/traffic_ops_golang/physlocation/phys_locations_test.go
@@ -20,6 +20,8 @@ package physlocation
*/
import (
+ "errors"
+ "reflect"
"testing"
"time"
@@ -39,7 +41,7 @@ func getTestPhysLocations() []tc.PhysLocation {
City: "Denver",
Email: "d.t@gmail.com",
ID: 1,
- LastUpdated: tc.Time{Time: time.Now()},
+ LastUpdated: tc.TimeNoMod{Time: time.Now()},
Name: "physLocation1",
Phone: "303-210-0000",
POC: "Dennis Thompson",
@@ -124,3 +126,21 @@ func TestInterfaces(t *testing.T) {
t.Errorf("PhysLocation must be Identifier")
}
}
+
+func TestValidate(t *testing.T) {
+ p := TOPhysLocation{}
+ errs := test.SortErrors(p.Validate(nil))
+ expected := test.SortErrors([]error{
+ errors.New("'state' cannot be blank"),
+ errors.New("'zip' cannot be blank"),
+ errors.New("'address' cannot be blank"),
+ errors.New("'city' cannot be blank"),
+ errors.New("'name' cannot be blank"),
+ errors.New("'regionId' cannot be blank"),
+ errors.New("'shortName' cannot be blank"),
+ })
+
+ if !reflect.DeepEqual(expected, errs) {
+ t.Errorf("expected %++v, got %++v", expected, errs)
+ }
+}
--
To stop receiving notification emails like this one, please contact
dewrich@apache.org.