Skip to content

Commit 0f2f655

Browse files
committed
validate that json is concrete
this avoids the need to use a forked cue Signed-off-by: Evan Cordell <[email protected]>
1 parent 218888d commit 0f2f655

File tree

3 files changed

+30
-29
lines changed

3 files changed

+30
-29
lines changed

pkg/schema/validation/validate.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ func (c configValidator) Validate(b []byte, key string) error {
3939
c.logger.WithError(err).Debugf(key)
4040
return err
4141
}
42-
43-
err = json.Validate(b, v)
42+
jsonAsCue, err := json.Decode(c.runtime, c.instance.Dir, b)
4443
if err != nil {
44+
return fmt.Errorf("could not parse json: %v", err)
45+
}
46+
if err := v.Unify(jsonAsCue.Value()).Validate(cue.Concrete(true)); err != nil {
4547
c.logger.WithError(err).Debugf("Validation error")
4648
return err
4749
}

pkg/schema/validation/validate_test.go

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -127,32 +127,34 @@ func TestValidateConfig(t *testing.T) {
127127
}
128128

129129
for _, tt := range table {
130-
logger := logrus.NewEntry(logrus.New())
131-
// load schema for config definitions
132-
instance := load.Instances([]string{"."}, &load.Config{
133-
Dir: schemaPath,
134-
})
135-
if len(instance) > 1 {
136-
t.Fatalf("multiple instance loading currently not supported: %s", schemaPath)
137-
}
138-
if len(instance) < 1 {
139-
t.Fatalf("no instances found: %s", schemaPath)
140-
}
130+
t.Run(tt.description, func(t *testing.T) {
131+
logger := logrus.NewEntry(logrus.New())
132+
// load schema for config definitions
133+
instance := load.Instances([]string{"."}, &load.Config{
134+
Dir: schemaPath,
135+
})
136+
if len(instance) > 1 {
137+
t.Fatalf("multiple instance loading currently not supported: %s", schemaPath)
138+
}
139+
if len(instance) < 1 {
140+
t.Fatalf("no instances found: %s", schemaPath)
141+
}
141142

142-
// Config validator
143-
configValidator := NewConfigValidator(instance[0], logger)
144-
// Read json file
145-
content, err := ioutil.ReadFile(tt.filename)
146-
require.NoError(t, err)
143+
// Config validator
144+
configValidator := NewConfigValidator(instance[0], logger)
145+
// Read json file
146+
content, err := ioutil.ReadFile(tt.filename)
147+
require.NoError(t, err)
147148

148-
// Validate json against schema
149-
err = configValidator.Validate(content, tt.kind)
149+
// Validate json against schema
150+
err = configValidator.Validate(content, tt.kind)
150151

151-
if tt.hasError {
152-
require.Error(t, err)
153-
require.Contains(t, err.Error(), tt.errString)
154-
} else {
155-
require.NoError(t, err)
156-
}
152+
if tt.hasError {
153+
require.Error(t, err)
154+
require.Contains(t, err.Error(), tt.errString)
155+
} else {
156+
require.NoError(t, err)
157+
}
158+
})
157159
}
158160
}

vendor/cuelang.org/go/pkg/encoding/json/manual.go

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)