Skip to content

Commit

Permalink
validation: warning for missing 'user'
Browse files Browse the repository at this point in the history
  • Loading branch information
srerickson committed Nov 7, 2024
1 parent 8e5bd91 commit 0b68cd1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 15 deletions.
9 changes: 5 additions & 4 deletions ocflv1/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,10 @@ func (inv *Inventory) Validate() *ocfl.Validation {
err := fmt.Errorf("version %s missing recommended field: 'message'", vname)
v.AddWarn(ec(err, codes.W007(ocflV)))
}
if ver.User == nil {
err := fmt.Errorf("version %s missing recommended field: 'user'", vname)
v.AddWarn(ec(err, codes.W007(ocflV)))
}
if ver.User != nil {
if ver.User.Name == "" {
err := fmt.Errorf("version %s user missing required field: 'name'", vname)
Expand Down Expand Up @@ -532,10 +536,7 @@ func ValidateInventoryBytes(raw []byte, spec ocfl.Spec) (inv *Inventory, v *ocfl
if err := inv.setJsonDigest(raw); err != nil {
v.AddFatal(err)
}
if v.Err() != nil {
return nil, v
}
v = inv.Validate()
v.Add(inv.Validate())
if v.Err() != nil {
return nil, v
}
Expand Down
65 changes: 54 additions & 11 deletions ocflv1/inventory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ import (
"github.com/srerickson/ocfl-go/ocflv1"
)

func TestValidateInventory(t *testing.T) {
for desc, test := range testInventories {
t.Run(desc, func(t *testing.T) {
inv, vldr := ocflv1.ValidateInventoryBytes([]byte(test.data), ocfl.Spec1_0)
test.expect(t, inv, vldr)
})
}
}

type testInventory struct {
data string
expect func(t *testing.T, inv *ocflv1.Inventory, v *ocfl.Validation)
Expand Down Expand Up @@ -127,6 +136,51 @@ var testInventories = map[string]testInventory{
be.NilErr(t, v.Err())
},
},
// Warn inventories
`missing_version_user`: {
data: `{
"digestAlgorithm": "sha512",
"head": "v1",
"id": "http://example.org/minimal_no_content",
"manifest": {},
"type": "https://ocfl.io/1.0/spec/#inventory",
"versions": {
"v1": {
"created": "2019-01-01T02:03:04Z",
"message": "One version and no content",
"state": { }
}
}
}`,
expect: func(t *testing.T, inv *ocflv1.Inventory, v *ocfl.Validation) {
be.NilErr(t, v.Err())
testutil.ErrorsIncludeOCFLCode(t, "W007", v.WarnErrors()...)
},
},
// Warn inventories
`missing_version_message`: {
data: `{
"digestAlgorithm": "sha512",
"head": "v1",
"id": "http://example.org/minimal_no_content",
"manifest": {},
"type": "https://ocfl.io/1.0/spec/#inventory",
"versions": {
"v1": {
"created": "2019-01-01T02:03:04Z",
"state": { },
"user": {
"address": "mailto:[email protected]",
"name": "A Person"
}
}
}
}`,
expect: func(t *testing.T, inv *ocflv1.Inventory, v *ocfl.Validation) {
be.NilErr(t, v.Err())
testutil.ErrorsIncludeOCFLCode(t, "W007", v.WarnErrors()...)
},
},
// Bad inventories
`missing_id`: {
data: `{
Expand All @@ -149,7 +203,6 @@ var testInventories = map[string]testInventory{
},
},
`bad_digestAlgorithm`: {

data: `{
"digestAlgorithm": "sha51",
"head": "v1",
Expand Down Expand Up @@ -508,7 +561,6 @@ var testInventories = map[string]testInventory{
},
},
`bad_manifest_basepathconflict`: {

data: `{
"digestAlgorithm": "sha512",
"head": "v3",
Expand Down Expand Up @@ -691,12 +743,3 @@ var testInventories = map[string]testInventory{
},
},
}

func TestValidateInventory(t *testing.T) {
for desc, test := range testInventories {
t.Run(desc, func(t *testing.T) {
inv, vldr := ocflv1.ValidateInventoryBytes([]byte(test.data), ocfl.Spec1_0)
test.expect(t, inv, vldr)
})
}
}

0 comments on commit 0b68cd1

Please sign in to comment.