cc-backend/pkg/schema/validate.go

54 lines
1.3 KiB
Go
Raw Normal View History

// Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
// All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package schema
import (
2022-09-13 15:22:20 +02:00
"encoding/json"
"fmt"
"io"
2022-09-13 15:22:20 +02:00
"github.com/ClusterCockpit/cc-backend/pkg/log"
"github.com/santhosh-tekuri/jsonschema/v5"
)
type Kind int
const (
Meta Kind = iota + 1
Data
ClusterCfg
)
2022-09-13 15:22:20 +02:00
func Validate(k Kind, r io.Reader) (err error) {
var s *jsonschema.Schema
switch k {
case Meta:
s, err = jsonschema.Compile("https://raw.githubusercontent.com/ClusterCockpit/cc-specifications/master/datastructures/job-meta.schema.json")
case Data:
s, err = jsonschema.Compile("https://raw.githubusercontent.com/ClusterCockpit/cc-specifications/master/datastructures/job-data.schema.json")
case ClusterCfg:
s, err = jsonschema.Compile("https://raw.githubusercontent.com/ClusterCockpit/cc-specifications/master/datastructures/cluster.schema.json")
default:
return fmt.Errorf("unkown schema kind ")
}
if err != nil {
return err
}
2022-09-13 15:22:20 +02:00
var v interface{}
if err := json.NewDecoder(r).Decode(&v); err != nil {
2022-09-13 15:40:32 +02:00
log.Errorf("schema.Validate() - Failed to decode %v", err)
return err
}
2022-09-13 15:22:20 +02:00
if err = s.Validate(v); err != nil {
return fmt.Errorf("%#v", err)
}
return nil
}