Skip to content

Commit

Permalink
Merge pull request #675 from sandrask/issue-674
Browse files Browse the repository at this point in the history
feat: Service endpoint object should accept list of strings and list of objects
  • Loading branch information
bstasyszyn authored Apr 28, 2022
2 parents 6eb6023 + 661ab4d commit a1567c3
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 5 deletions.
30 changes: 28 additions & 2 deletions pkg/versions/1_0/operationparser/patchvalidator/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,35 @@ func validateServiceEndpoint(serviceEndpoint interface{}) error {
return validateURI(uri)
}

_, ok = serviceEndpoint.([]interface{})
uris, ok := serviceEndpoint.([]string)
if ok {
return errors.New("service endpoint cannot be an array of objects")
return validateURIs(uris)
}

objs, ok := serviceEndpoint.([]interface{})
if ok {
return validateServiceEndpointObjects(objs)
}

return nil
}

func validateServiceEndpointObjects(objs []interface{}) error {
for _, obj := range objs {
uri, ok := obj.(string)
if ok {
return validateURI(uri)
}
}

return nil
}

func validateURIs(uris []string) error {
for _, uri := range uris {
if err := validateURI(uri); err != nil {
return err
}
}

return nil
Expand Down
53 changes: 50 additions & 3 deletions pkg/versions/1_0/operationparser/patchvalidator/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,43 @@ func TestValidateServices(t *testing.T) {
err = validateServices(doc.Services())
require.NoError(t, err)
})
t.Run("error - service endpoint cannot be an array of objects", func(t *testing.T) {
t.Run("success - service endpoint is an array of objects", func(t *testing.T) {
doc, err := document.DidDocumentFromBytes([]byte(serviceDocEndpointIsAnArrayOfObjects))
require.NoError(t, err)
err = validateServices(doc.Services())
require.NoError(t, err)
})
t.Run("success - service endpoint is an array of string objects", func(t *testing.T) {
doc, err := document.DidDocumentFromBytes([]byte(serviceDocEndpointIsAnArrayOfURLStrings))
require.NoError(t, err)
err = validateServices(doc.Services())
require.NoError(t, err)
})
t.Run("success - service endpoint is an array of strings", func(t *testing.T) {
servicesMap := make(map[string]interface{})
servicesMap["id"] = "someID"
servicesMap["type"] = "someType"
servicesMap["serviceEndpoint"] = []string{"https://hello.com", "https://there.com"}

err := validateServices([]document.Service{document.NewService(servicesMap)})
require.NoError(t, err)
})
t.Run("error - service endpoint is an array of invalid strings", func(t *testing.T) {
servicesMap := make(map[string]interface{})
servicesMap["id"] = "someID"
servicesMap["type"] = "someType"
servicesMap["serviceEndpoint"] = []string{"invalid-1", "invalid-2"}

err := validateServices([]document.Service{document.NewService(servicesMap)})
require.Error(t, err)
require.Contains(t, err.Error(), "service endpoint cannot be an array of objects")
require.Contains(t, err.Error(), "service endpoint 'invalid-1' is not a valid URI")
})
t.Run("error - service endpoint is an array of invalid string URL objects", func(t *testing.T) {
doc, err := document.DidDocumentFromBytes([]byte(serviceDocEndpointIsAnArrayOfInvalidURLStrings))
require.NoError(t, err)
err = validateServices(doc.Services())
require.Error(t, err)
require.Contains(t, err.Error(), "service endpoint 'hello' is not a valid URI")
})
t.Run("error - empty service endpoint URI", func(t *testing.T) {
doc, err := document.DidDocumentFromBytes([]byte(serviceDocNoServiceEndpointURI))
Expand Down Expand Up @@ -679,7 +710,23 @@ const serviceDocEndpointIsAnArrayOfObjects = `{
"service": [{
"id": "vcs",
"type": "type",
"serviceEndpoint": ["hello"]
"serviceEndpoint": [{"key":"value"},{"key2":"value2"}]
}]
}`

const serviceDocEndpointIsAnArrayOfURLStrings = `{
"service": [{
"id": "vcs",
"type": "type",
"serviceEndpoint": ["https://hello.com", "https://there.com"]
}]
}`

const serviceDocEndpointIsAnArrayOfInvalidURLStrings = `{
"service": [{
"id": "vcs",
"type": "type",
"serviceEndpoint": ["hello", "there"]
}]
}`

Expand Down

0 comments on commit a1567c3

Please sign in to comment.