-
Notifications
You must be signed in to change notification settings - Fork 83
/
labeldef_validation.go
53 lines (46 loc) · 1.56 KB
/
labeldef_validation.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
package graphql
import (
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/kyma-incubator/compass/components/director/internal/model"
"github.com/kyma-incubator/compass/components/director/pkg/apperrors"
"github.com/kyma-incubator/compass/components/director/pkg/jsonschema"
"github.com/pkg/errors"
)
// Validate missing godoc
func (i LabelDefinitionInput) Validate() error {
return validation.Errors{
"rule.validSchema": i.validateSchema(),
"key": validation.Validate(i.Key, validation.Required, validation.In(model.ScenariosKey)),
"schema": validation.Validate(i.Schema),
}.Filter()
}
func (i LabelDefinitionInput) validateSchema() error {
if i.Schema != nil {
if _, err := jsonschema.NewValidatorFromStringSchema(string(*i.Schema)); err != nil {
return errors.Wrapf(err, "while validating schema: [%+v]", *i.Schema)
}
}
if i.Key == model.ScenariosKey {
if err := i.validateScenariosSchema(); err != nil {
return errors.Wrapf(err, "while validating schema for key %s", model.ScenariosKey)
}
}
return nil
}
func (i LabelDefinitionInput) validateScenariosSchema() error {
if i.Schema == nil {
return apperrors.NewInternalError("schema can not be nil")
}
validator, err := jsonschema.NewValidatorFromRawSchema(model.SchemaForScenariosSchema)
if err != nil {
return errors.Wrap(err, "while compiling validator schema")
}
result, err := validator.ValidateString(string(*i.Schema))
if err != nil {
return errors.Wrap(err, "while validating new schema")
}
if !result.Valid {
return result.Error
}
return nil
}