Skip to content

Commit df41d54

Browse files
DRickardksclarke
andauthored
[SERV-1241] Check visibility values (#151)
* [SERV-1241] new check, related error msg, some styling * [SERV-1241] fix typo, add test --------- Co-authored-by: Kevin S. Clarke <[email protected]>
1 parent a0a0c0c commit df41d54

File tree

3 files changed

+126
-20
lines changed

3 files changed

+126
-20
lines changed

errors/errors.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,25 @@ package errors
22

33
// Error messages
44
var (
5-
NilProfileErr = "supplied profile cannot be nil"
6-
NoPrefixErr = "ARK must start with 'ark:/'"
7-
NaanTooShortErr = "NAAN must be at least 5 digits long"
8-
NaanProfileErr = "The supplied NAAN is not allowed for the supplied profile"
9-
NoObjIdErr = "The ARK must contain an object identifier"
10-
InvalidObjIdErr = "The object identifier and qualifier is not valid"
11-
ArkValFailed = "ARK validation failed"
12-
EolFoundErr = "character for EOL found in cell"
13-
BadHeaderErr = "could not retrieve CSV header: %s"
14-
FieldNotFoundErr = "required field `%s` was not found"
15-
FieldDataNotFoundErr = "data for required field `%s` was not found"
16-
UnknownProfileErr = "unknown profile `%s`"
17-
ProfileConfigErr = "supplied profile has objTypes and notObjTypes set: %s"
18-
NoHostDir = "a HOST_DIR must be set"
19-
FileNotExist = "the file path given does not exist: %s"
20-
UrlFormatErr = "license URL is not in a proper format (check for HTTPS)"
21-
UrlConnectErr = "problem connecting to license URL"
22-
UrlReadErr = "problem reading body of license URL"
23-
TypeWhitespaceError = "field contains invalid characters (e.g., spaces, line breaks)"
24-
TypeValueError = "object type field doesn't contain valid value"
5+
NilProfileErr = "supplied profile cannot be nil"
6+
NoPrefixErr = "ARK must start with 'ark:/'"
7+
NaanTooShortErr = "NAAN must be at least 5 digits long"
8+
NaanProfileErr = "The supplied NAAN is not allowed for the supplied profile"
9+
NoObjIdErr = "The ARK must contain an object identifier"
10+
InvalidObjIdErr = "The object identifier and qualifier is not valid"
11+
ArkValFailed = "ARK validation failed"
12+
EolFoundErr = "character for EOL found in cell"
13+
BadHeaderErr = "could not retrieve CSV header: %s"
14+
FieldNotFoundErr = "required field `%s` was not found"
15+
FieldDataNotFoundErr = "data for required field `%s` was not found"
16+
UnknownProfileErr = "unknown profile `%s`"
17+
ProfileConfigErr = "supplied profile has objTypes and notObjTypes set: %s"
18+
NoHostDir = "a HOST_DIR must be set"
19+
FileNotExist = "the file path given does not exist: %s"
20+
UrlFormatErr = "license URL is not in a proper format (check for HTTPS)"
21+
UrlConnectErr = "problem connecting to license URL"
22+
UrlReadErr = "problem reading body of license URL"
23+
TypeWhitespaceError = "field contains invalid characters (e.g., spaces, line breaks)"
24+
TypeValueError = "object type field doesn't contain valid value"
25+
VisibilityValueError = "ovisibility field doesn't contain valid value"
2526
)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package checks
2+
3+
import (
4+
"regexp"
5+
6+
"github.com/UCLALibrary/validation-service/errors"
7+
"github.com/UCLALibrary/validation-service/validation/csv"
8+
"github.com/UCLALibrary/validation-service/validation/util"
9+
)
10+
11+
type VisibilityCheck struct{}
12+
13+
func NewVisibilityCheck(profiles *util.Profiles) (*VisibilityCheck, error) {
14+
if profiles == nil {
15+
return nil, csv.NewError(errors.NilProfileErr, csv.Location{}, "nil")
16+
}
17+
18+
return &VisibilityCheck{}, nil
19+
}
20+
21+
func (check *VisibilityCheck) Validate(profile string, location csv.Location, csvData [][]string) error {
22+
if err := csv.IsValidLocation(location, csvData, profile); err != nil {
23+
return err
24+
}
25+
26+
// find the header and determine if it matches Object Type
27+
header, err := csv.GetHeader(location, csvData, profile)
28+
29+
if err != nil {
30+
return err
31+
}
32+
33+
// Skip if we don't have an object tpe cell, or we're on the first (i.e., header) row
34+
if header != "Visibility" || location.RowIndex == 0 {
35+
return nil
36+
}
37+
38+
value := csvData[location.RowIndex][location.ColIndex]
39+
40+
whitespace := regexp.MustCompile(`\s`)
41+
if whitespace.MatchString(value) {
42+
return csv.NewError(errors.TypeWhitespaceError, location, profile)
43+
}
44+
valid := regexp.MustCompile(`open|ucla|private`)
45+
if !valid.MatchString(value) {
46+
return csv.NewError(errors.VisibilityValueError, location, profile)
47+
}
48+
49+
return nil
50+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//go:build unit
2+
3+
// Package checks consists of individual validation checks.
4+
//
5+
package checks
6+
7+
import (
8+
"github.com/UCLALibrary/validation-service/validation/csv"
9+
"github.com/UCLALibrary/validation-service/validation/util"
10+
"github.com/stretchr/testify/assert"
11+
"testing"
12+
)
13+
14+
// TestObjTypeCheck_Validate tests the Validate method on ObjTypeCheck.
15+
func TestVisibilityCheck_Validate(t *testing.T) {
16+
check, err := NewVisibilityCheck(util.NewProfiles())
17+
assert.NoError(t, err)
18+
19+
// Data variations to check the VisibilityCheck.Validate method against
20+
tests := []struct {
21+
name string
22+
location csv.Location
23+
data [][]string
24+
result bool
25+
}{
26+
{
27+
name: "valid",
28+
location: csv.Location{RowIndex: 1, ColIndex: 0},
29+
data: [][]string{{"Visibility"}, {"ucla"}},
30+
result: true,
31+
},
32+
{
33+
name: "invalid Bad Value",
34+
location: csv.Location{RowIndex: 1, ColIndex: 0},
35+
data: [][]string{{"Visibility"}, {"Other"}},
36+
result: false,
37+
},
38+
{
39+
name: "invalid Bad Chars",
40+
location: csv.Location{RowIndex: 1, ColIndex: 0},
41+
data: [][]string{{"Visibility"}, {"private "}},
42+
result: false,
43+
},
44+
}
45+
46+
// Iterate over test cases; fail if there isn't an error when we expect one or if there is an unexpected error
47+
for _, tt := range tests {
48+
t.Run(tt.name, func(t *testing.T) {
49+
err = check.Validate("default", tt.location, tt.data)
50+
if (err != nil && tt.result) || (err == nil && !tt.result) {
51+
t.Errorf("Expected '%v' response was not found: %v", tt.name, err)
52+
}
53+
})
54+
}
55+
}

0 commit comments

Comments
 (0)