-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathevent_schema.go
More file actions
51 lines (44 loc) · 1.17 KB
/
event_schema.go
File metadata and controls
51 lines (44 loc) · 1.17 KB
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
//go:build !novalidator
package cotlib
import (
"encoding/xml"
"fmt"
"log/slog"
"github.com/NERVsystems/cotlib/validator"
)
// eventPointSchema holds the compiled schema for CoT event points.
var eventPointSchema *validator.Schema
// initErr stores any error encountered during schema compilation.
var initErr error
func init() {
var err error
eventPointSchema, err = validator.Compile(validator.EventPointXSD())
if err != nil {
initErr = fmt.Errorf("compile event point schema: %w", err)
slog.Error("failed to compile event point schema", "error", err)
}
}
// ValidateAgainstSchema validates the given CoT event XML against the point schema.
func ValidateAgainstSchema(data []byte) error {
if initErr != nil {
return initErr
}
var p struct {
Point Point `xml:"point"`
}
if err := xml.Unmarshal(data, &p); err != nil {
return fmt.Errorf("unmarshal: %w", err)
}
if (p.Point == Point{}) {
return fmt.Errorf("missing point element")
}
wrapper := struct {
XMLName xml.Name `xml:"point"`
Point
}{Point: p.Point}
ptXML, err := xml.Marshal(wrapper)
if err != nil {
return fmt.Errorf("marshal point: %w", err)
}
return eventPointSchema.Validate(ptXML)
}