-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidate.go
46 lines (40 loc) · 1.17 KB
/
validate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"encoding/json"
"fmt"
"log"
"strings"
mapset "github.com/deckarep/golang-set/v2"
)
func validate(input []byte) []byte {
var validationRequest RawValidationRequest
validationRequest.Settings = Settings{
ForbiddenResources: mapset.NewSet[string](),
}
decoder := json.NewDecoder(strings.NewReader(string(input)))
decoder.DisallowUnknownFields()
err := decoder.Decode(&validationRequest)
if err != nil {
//nolint:mnd
return marshalValidationResponseOrFail(
RejectRequest(
Message(fmt.Sprintf("Error deserializing validation request: %v", err)),
Code(400)))
}
return marshalValidationResponseOrFail(
mutateRequest(&validationRequest.Settings, &validationRequest.Request))
}
func marshalValidationResponseOrFail(response ValidationResponse) []byte {
responseBytes, err := json.Marshal(&response)
if err != nil {
log.Fatalf("cannot marshal validation response: %v", err)
}
return responseBytes
}
func mutateRequest(settings *Settings, request *Request) ValidationResponse {
if settings.ForbiddenResources.Contains(request.Resource) {
request.Resource = settings.DefaultResource
return MutateRequest(request)
}
return AcceptRequest()
}