Add option for json validation

This commit is contained in:
Jan Eitzinger
2022-09-13 15:22:20 +02:00
parent 732801548f
commit 777f225e91
6 changed files with 26 additions and 7 deletions

View File

@@ -78,6 +78,9 @@ type ProgramConfig struct {
// do not write to the job-archive.
DisableArchive bool `json:"disable-archive"`
// Validate json input against schema
Validate bool `json:"validate"`
// For LDAP Authentication and user synchronisation.
LdapConfig *LdapConfig `json:"ldap"`
JwtConfig *JWTAuthConfig `json:"jwts"`

View File

@@ -5,10 +5,12 @@
package schema
import (
"encoding/json"
"fmt"
"io"
"github.com/santhosh-tekuri/jsonschema"
"github.com/ClusterCockpit/cc-backend/pkg/log"
"github.com/santhosh-tekuri/jsonschema/v5"
)
type Kind int
@@ -19,7 +21,7 @@ const (
ClusterCfg
)
func Validate(k Kind, v io.Reader) (err error) {
func Validate(k Kind, r io.Reader) (err error) {
var s *jsonschema.Schema
switch k {
@@ -37,9 +39,15 @@ func Validate(k Kind, v io.Reader) (err error) {
return err
}
if err = s.Validate(v); err != nil {
var v interface{}
if err := json.NewDecoder(r).Decode(&v); err != nil {
log.Error("schema.Validate() - Failed to decode %v", err)
return err
}
if err = s.Validate(v); err != nil {
return fmt.Errorf("%#v", err)
}
return nil
}