@@ -3,9 +3,10 @@ package scim
33import (
44 "bytes"
55 "encoding/json"
6+ "errors"
67 "net/http"
78
8- "github.com/elimity-com/scim/errors"
9+ scimErrors "github.com/elimity-com/scim/errors"
910 "github.com/elimity-com/scim/internal/patch"
1011 "github.com/elimity-com/scim/optional"
1112 "github.com/elimity-com/scim/schema"
@@ -86,10 +87,10 @@ func (t ResourceType) schemaWithCommon() schema.Schema {
8687 return s
8788}
8889
89- func (t ResourceType ) validate (raw []byte ) (ResourceAttributes , * errors .ScimError ) {
90+ func (t ResourceType ) validate (raw []byte ) (ResourceAttributes , * scimErrors .ScimError ) {
9091 var m map [string ]interface {}
9192 if err := unmarshal (raw , & m ); err != nil {
92- return ResourceAttributes {}, & errors .ScimErrorInvalidSyntax
93+ return ResourceAttributes {}, & scimErrors .ScimErrorInvalidSyntax
9394 }
9495
9596 attributes , scimErr := t .schemaWithCommon ().Validate (m )
@@ -101,7 +102,7 @@ func (t ResourceType) validate(raw []byte) (ResourceAttributes, *errors.ScimErro
101102 extensionField := m [extension .Schema .ID ]
102103 if extensionField == nil {
103104 if extension .Required {
104- return ResourceAttributes {}, & errors .ScimErrorInvalidValue
105+ return ResourceAttributes {}, & scimErrors .ScimErrorInvalidValue
105106 }
106107 continue
107108 }
@@ -118,30 +119,30 @@ func (t ResourceType) validate(raw []byte) (ResourceAttributes, *errors.ScimErro
118119}
119120
120121// validatePatch parse and validate PATCH request.
121- func (t ResourceType ) validatePatch (r * http.Request ) ([]PatchOperation , * errors .ScimError ) {
122+ func (t ResourceType ) validatePatch (r * http.Request ) ([]PatchOperation , * scimErrors .ScimError ) {
122123 data , err := readBody (r )
123124 if err != nil {
124- return nil , & errors .ScimErrorInvalidSyntax
125+ return nil , & scimErrors .ScimErrorInvalidSyntax
125126 }
126127
127128 var req struct {
128129 Schemas []string
129130 Operations []json.RawMessage
130131 }
131132 if err := unmarshal (data , & req ); err != nil {
132- return nil , & errors .ScimErrorInvalidSyntax
133+ return nil , & scimErrors .ScimErrorInvalidSyntax
133134 }
134135
135136 // The body of each request MUST contain the "schemas" attribute with the URI value of
136137 // "urn:ietf:params:scim:api:messages:2.0:PatchOp".
137138 if len (req .Schemas ) != 1 || req .Schemas [0 ] != "urn:ietf:params:scim:api:messages:2.0:PatchOp" {
138- return nil , & errors .ScimErrorInvalidValue
139+ return nil , & scimErrors .ScimErrorInvalidValue
139140 }
140141
141142 // The body of an HTTP PATCH request MUST contain the attribute "Operations",
142143 // whose value is an array of one or more PATCH operations.
143144 if len (req .Operations ) < 1 {
144- return nil , & errors .ScimErrorInvalidValue
145+ return nil , & scimErrors .ScimErrorInvalidValue
145146 }
146147
147148 // Evaluation continues until all operations are successfully applied or until an error condition is encountered.
@@ -153,11 +154,15 @@ func (t ResourceType) validatePatch(r *http.Request) ([]PatchOperation, *errors.
153154 t .getSchemaExtensions ()... ,
154155 )
155156 if err != nil {
156- return nil , & errors .ScimErrorInvalidPath
157+ return nil , & scimErrors .ScimErrorInvalidPath
157158 }
158159 value , err := validator .Validate ()
159160 if err != nil {
160- return nil , & errors .ScimErrorInvalidValue
161+ var scimErr * scimErrors.ScimError
162+ if errors .As (err , & scimErr ) {
163+ return nil , scimErr
164+ }
165+ return nil , & scimErrors .ScimErrorInvalidValue
161166 }
162167 operations = append (operations , PatchOperation {
163168 Op : string (validator .Op ),
0 commit comments