-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
126 lines (116 loc) · 3.32 KB
/
validator.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//go:build js && wasm
package main
import (
"bytes"
"encoding/json"
"github.com/aml-org/amf-custom-validator/internal/validator"
c "github.com/aml-org/amf-custom-validator/pkg/config"
"syscall/js"
)
func validateWrapper() js.Func {
jsonFunc := js.FuncOf(func(this js.Value, args []js.Value) any {
if len(args) != 3 {
return "Invalid no of arguments passed"
}
profileString := args[0].String()
dataString := args[1].String()
debug := args[2].Bool()
res, err := validator.Validate(profileString, dataString, debug, nil)
if err != nil {
return err.Error()
}
return res
})
return jsonFunc
}
func validateWithConfigurationWrapper() js.Func {
jsonFunc := js.FuncOf(func(this js.Value, args []js.Value) any {
if len(args) != 5 {
return "Invalid no of arguments passed"
}
profileString := args[0].String()
dataString := args[1].String()
debug := args[2].Bool()
if !args[3].IsUndefined() {
return "ValidationConfiguration not yet supported, please set this parameter as 'undefined'. ValidationConfiguration needs to import functions from JS which is not supported by Go 1.19"
}
reportConfig := buildReportConfiguration(args[4])
res, err := validator.ValidateWithConfiguration(profileString, dataString, debug, nil, c.DefaultValidationConfiguration{}, reportConfig)
if err != nil {
return err.Error()
}
return res
})
return jsonFunc
}
func buildReportConfiguration(value js.Value) c.ReportConfiguration {
includeReportCreationTime := value.Get("IncludeReportCreationTime").Bool()
reportSchemaIri := value.Get("ReportSchemaIri").String()
lexicalSchemaIri := value.Get("LexicalSchemaIri").String()
return c.ReportConfiguration{
IncludeReportCreationTime: includeReportCreationTime,
ReportSchemaIri: reportSchemaIri,
LexicalSchemaIri: lexicalSchemaIri,
}
}
func genRegoWrapper() js.Func {
jsonFunc := js.FuncOf(func(this js.Value, args []js.Value) any {
if len(args) != 1 {
return "Invalid no of arguments passed"
}
profileString := args[0].String()
res, err := validator.GenerateRego(profileString, false, nil)
if err != nil {
return err.Error()
}
return res.Code
})
return jsonFunc
}
func normalizeInputWrapper() js.Func {
jsonFunc := js.FuncOf(func(this js.Value, args []js.Value) any {
if len(args) != 1 {
return "Invalid no of arguments passed"
}
dataString := args[0].String()
normalizedInput, err := validator.ProcessInput(dataString, false, nil)
if err != nil {
return err.Error()
}
var b bytes.Buffer
enc := json.NewEncoder(&b)
enc.SetIndent("", " ")
err = enc.Encode(normalizedInput)
if err != nil {
return err.Error()
}
return b.String()
})
return jsonFunc
}
func exitWrapper(c chan bool) js.Func {
jsonFunc := js.FuncOf(func(this js.Value, args []js.Value) any {
c <- true
return nil
})
return jsonFunc
}
func main() {
c := make(chan bool)
// validate
f := validateWrapper()
js.Global().Set("__AMF__validateCustomProfile", f)
// validate with configuration
f = validateWithConfigurationWrapper()
js.Global().Set("__AMF__validateCustomProfileWithConfiguration", f)
// gen rego
f = genRegoWrapper()
js.Global().Set("__AMF__generateRego", f)
// normalizeInput
f = normalizeInputWrapper()
js.Global().Set("__AMF__normalizeInput", f)
// exit
f = exitWrapper(c)
js.Global().Set("__AMF__terminateValidator", f)
<-c
}