mirror of
https://github.com/speatzle/nfsense.git
synced 2025-09-13 07:19:07 +00:00
Add basic jsonschema validation
This commit is contained in:
parent
3fefb7ca7c
commit
6850d134fb
7 changed files with 126 additions and 0 deletions
40
internal/validation/schema.go
Normal file
40
internal/validation/schema.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
package validation
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/santhosh-tekuri/jsonschema/v5"
|
||||
)
|
||||
|
||||
//go:embed schemas/*.schema.json
|
||||
var schemasFS embed.FS
|
||||
var schema *jsonschema.Schema
|
||||
|
||||
func init() {
|
||||
all, err := schemasFS.ReadDir("schemas")
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("Reading Schemas: %w", err))
|
||||
}
|
||||
|
||||
c := jsonschema.NewCompiler()
|
||||
|
||||
for _, f := range all {
|
||||
data, err := schemasFS.Open(filepath.Join("schemas", f.Name()))
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("Reading Schema: %w", err))
|
||||
}
|
||||
err = c.AddResource("https://nfsense.net/"+f.Name(), data)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("Adding Schema: %w", err))
|
||||
}
|
||||
}
|
||||
|
||||
s, err := c.Compile("https://nfsense.net/config.schema.json")
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("Reading Schemas: %w", err))
|
||||
}
|
||||
|
||||
schema = s
|
||||
}
|
30
internal/validation/schemas/config.schema.json
Normal file
30
internal/validation/schemas/config.schema.json
Normal file
|
@ -0,0 +1,30 @@
|
|||
{
|
||||
"$id": "https://nfsense.net/config.schema.json",
|
||||
"title": "Config",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"config_version": {
|
||||
"type": "number"
|
||||
},
|
||||
"firewall": {
|
||||
"type": ["number","string","boolean","object","array", "null"]
|
||||
},
|
||||
"object": {
|
||||
"type": ["number","string","boolean","object","array", "null"]
|
||||
},
|
||||
"network": {
|
||||
"type": ["number","string","boolean","object","array", "null"]
|
||||
},
|
||||
"service": {
|
||||
"type": ["number","string","boolean","object","array", "null"]
|
||||
},
|
||||
"vpn": {
|
||||
"type": ["number","string","boolean","object","array", "null"]
|
||||
},
|
||||
"system": {
|
||||
"description": "System Settings",
|
||||
"$ref": "https://nfsense.net/system.schema.json"
|
||||
}
|
||||
},
|
||||
"required": ["config_version", "firewall", "object", "network", "service", "vpn", "system"]
|
||||
}
|
26
internal/validation/schemas/system.schema.json
Normal file
26
internal/validation/schemas/system.schema.json
Normal file
|
@ -0,0 +1,26 @@
|
|||
{
|
||||
"$id": "https://nfsense.net/system.schema.json",
|
||||
"title": "System",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"users": {
|
||||
"type": "object",
|
||||
"additionalProperties": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"comment": {
|
||||
"type": "string"
|
||||
},
|
||||
"hash": {
|
||||
"type": "string"
|
||||
},
|
||||
"salt": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": ["hash", "salt"]
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": ["users"]
|
||||
}
|
22
internal/validation/validation.go
Normal file
22
internal/validation/validation.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package validation
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func ValidateConfig(conf any) error {
|
||||
|
||||
// TODO find a better way validate config since jsonschema only takes a map[string]interface{}
|
||||
data, err := json.Marshal(conf)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("Marshal Error: %w", err))
|
||||
}
|
||||
var clone any
|
||||
err = json.Unmarshal(data, &clone)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("Unmarshal Error: %w", err))
|
||||
}
|
||||
|
||||
return schema.Validate(clone)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue