Skip to content

Commit 229f0d3

Browse files
cschuijtgasperr
authored andcommitted
add basic validations for non-Organization resources
1 parent fdd8bbf commit 229f0d3

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

component/mcsd/validator.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,21 @@ func validateOrganizationResource(ctx context.Context, resource *fhir.Organizati
6262
if len(uraIdentifiers) > 1 {
6363
return fmt.Errorf("organization can't have multiple identifiers with system %s", coding.URANamingSystem)
6464
}
65-
// TODO: Enable check, fix test
6665

6766
if len(uraIdentifiers) == 0 && resource.PartOf == nil {
6867
return fmt.Errorf("organization must have an identifier with system %s or refer to another organization through 'partOf'", coding.URANamingSystem)
6968
}
7069

7170
// TODO: Support validation of organizations referring to a parent organization, without having a URA identifier
7271

73-
//if len(uraIdentifiers) == 0 && resource.PartOf != nil {
74-
//
75-
//}
72+
// if len(uraIdentifiers) == 0 && resource.PartOf != nil {
73+
// response, err := http.NewRequest(http.MethodGet, *resource.PartOf.Reference, bytes.NewReader())
74+
// if err != nil {
75+
// return fmt.Errorf("could not follow reference to parent Organization %s", resource.PartOf)
76+
// }
77+
// response.Body
78+
// }
79+
7680
return nil
7781
}
7882

@@ -85,13 +89,25 @@ func validateHealthcareServiceResource(ctx context.Context, resource *fhir.Healt
8589
}
8690

8791
func validatePractitionerRoleResource(ctx context.Context, resource *fhir.PractitionerRole) error {
92+
if resource.Organization == nil {
93+
return fmt.Errorf("practitioner role must have an organization reference")
94+
}
95+
8896
return nil
8997
}
9098

9199
func validateEndpointResource(ctx context.Context, resource *fhir.Endpoint) error {
100+
if resource.ManagingOrganization == nil {
101+
return fmt.Errorf("endpoint must have a 'managingOrganization' referencing an Organization")
102+
}
103+
92104
return nil
93105
}
94106

95107
func validateLocationResource(ctx context.Context, resource *fhir.Location) error {
108+
if resource.ManagingOrganization == nil {
109+
return fmt.Errorf("location must have a 'managingOrganization' referencing an Organization")
110+
}
111+
96112
return nil
97113
}

component/mcsd/validator_test.go

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,44 @@ import (
88
"github.com/zorgbijjou/golang-fhir-models/fhir-models/fhir"
99
)
1010

11+
// Test that the generic validators do their job. At this point, this only checks whether
12+
// the resource is of an allowed type before deferring to a resource-specific function.
13+
func TestGenericValidators(t *testing.T) {
14+
resources := []struct {
15+
name string
16+
resourceJSON []byte
17+
valid bool
18+
}{
19+
{
20+
name: "valid Organization",
21+
resourceJSON: []byte(`{"resourceType":"Organization", "identifier":[{"system":"http://fhir.nl/fhir/NamingSystem/ura","value":"12345"}]}`),
22+
valid: true,
23+
},
24+
{
25+
name: "invalid resource type",
26+
resourceJSON: []byte(`{"resourceType":"Patient"}`),
27+
valid: false,
28+
},
29+
}
30+
31+
for _, tt := range resources {
32+
t.Run(tt.name, func(t *testing.T) {
33+
err := ValidateUpdate(t.Context(), ValidationRules{AllowedResourceTypes: []string{"Organization"}}, tt.resourceJSON)
34+
35+
if tt.valid {
36+
require.NoError(t, err)
37+
} else {
38+
require.Error(t, err)
39+
}
40+
})
41+
}
42+
43+
}
44+
45+
// Test validator for Organization resources. An organization should either have an URA number
46+
// or should be a sub-organization to an Organization with an URA number.
47+
//
48+
// https://nuts-foundation.github.io/nl-generic-functions-ig/care-services.html#update-client
1149
func TestOrganizationValidator(t *testing.T) {
1250
organizations := []struct {
1351
name string
@@ -79,12 +117,12 @@ func TestOrganizationValidator(t *testing.T) {
79117

80118
for _, tt := range organizations {
81119
t.Run(tt.name, func(t *testing.T) {
82-
err2 := validateOrganizationResource(t.Context(), &tt.organization)
120+
err := validateOrganizationResource(t.Context(), &tt.organization)
83121

84122
if tt.valid {
85-
require.NoError(t, err2)
123+
require.NoError(t, err)
86124
} else {
87-
require.Error(t, err2)
125+
require.Error(t, err)
88126
}
89127
})
90128
}

0 commit comments

Comments
 (0)