Skip to content

Commit 21a575e

Browse files
committed
Use validator interface instead of function
Signed-off-by: Evan Lezar <[email protected]>
1 parent 3df5290 commit 21a575e

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

pkg/cdi/spec.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@ const (
3737
defaultSpecExt = ".yaml"
3838
)
3939

40+
type validator interface {
41+
Validate(*cdi.Spec) error
42+
}
43+
4044
var (
4145
// Externally set CDI Spec validation function.
42-
specValidator func(*cdi.Spec) error
46+
specValidator validator
4347
validatorLock sync.RWMutex
4448
)
4549

@@ -256,10 +260,10 @@ func ParseSpec(data []byte) (*cdi.Spec, error) {
256260
// SetSpecValidator sets a CDI Spec validator function. This function
257261
// is used for extra CDI Spec content validation whenever a Spec file
258262
// loaded (using ReadSpec() or written (using WriteSpec()).
259-
func SetSpecValidator(fn func(*cdi.Spec) error) {
263+
func SetSpecValidator(v validator) {
260264
validatorLock.Lock()
261265
defer validatorLock.Unlock()
262-
specValidator = fn
266+
specValidator = v
263267
}
264268

265269
// validateSpec validates the Spec using the extneral validator.
@@ -270,7 +274,7 @@ func validateSpec(raw *cdi.Spec) error {
270274
if specValidator == nil {
271275
return nil
272276
}
273-
err := specValidator(raw)
277+
err := specValidator.Validate(raw)
274278
if err != nil {
275279
return fmt.Errorf("Spec validation failed: %w", err)
276280
}

schema/schema.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232

3333
schema "github.com/xeipuuv/gojsonschema"
3434
"tags.cncf.io/container-device-interface/internal/validation"
35+
cdi "tags.cncf.io/container-device-interface/specs-go"
3536
)
3637

3738
const (
@@ -48,6 +49,13 @@ type Schema struct {
4849
schema *schema.Schema
4950
}
5051

52+
func (s *Schema) Validate(spec *cdi.Spec) error {
53+
if s == nil {
54+
return nil
55+
}
56+
return s.ValidateType(spec)
57+
}
58+
5159
// Error wraps a JSON validation result.
5260
type Error struct {
5361
Result *schema.Result
@@ -98,7 +106,7 @@ func ReadAndValidate(r io.Reader) ([]byte, error) {
98106

99107
// Validate validates the data read from an io.Reader against the active schema.
100108
func Validate(r io.Reader) error {
101-
return current.Validate(r)
109+
return current.ValidateReader(r)
102110
}
103111

104112
// ValidateData validates the given JSON document against the active schema.
@@ -165,8 +173,8 @@ func (s *Schema) ReadAndValidate(r io.Reader) ([]byte, error) {
165173
return data, s.validate(loader)
166174
}
167175

168-
// Validate validates the data read from an io.Reader against the schema.
169-
func (s *Schema) Validate(r io.Reader) error {
176+
// ValidateReader validates the data read from an io.Reader against the schema.
177+
func (s *Schema) ValidateReader(r io.Reader) error {
170178
_, err := s.ReadAndValidate(r)
171179
return err
172180
}

schema/validate.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,42 +16,31 @@
1616

1717
package schema
1818

19-
import (
20-
cdi "tags.cncf.io/container-device-interface/specs-go"
21-
)
22-
2319
const (
2420
// DefaultExternalSchema is the default JSON schema to load for validation.
2521
DefaultExternalSchema = "/etc/cdi/schema/schema.json"
2622
)
2723

2824
// WithSchema returns a CDI Spec validator that uses the given Schema.
29-
func WithSchema(s *Schema) func(*cdi.Spec) error {
30-
if s == nil {
31-
return func(*cdi.Spec) error {
32-
return nil
33-
}
34-
}
35-
return func(spec *cdi.Spec) error {
36-
return s.ValidateType(spec)
37-
}
25+
func WithSchema(s *Schema) *Schema {
26+
return s
3827
}
3928

4029
// WithNamedSchema loads the named JSON schema and returns a CDI Spec
4130
// validator for it. If loading the schema fails a dummy validator is
4231
// returned.
43-
func WithNamedSchema(name string) func(*cdi.Spec) error {
32+
func WithNamedSchema(name string) *Schema {
4433
s, _ := Load(name)
4534
return WithSchema(s)
4635
}
4736

4837
// WithDefaultSchema returns a CDI Spec validator that uses the default
4938
// external JSON schema, or the default builtin one if the external one
5039
// fails to load.
51-
func WithDefaultSchema() func(*cdi.Spec) error {
40+
func WithDefaultSchema() *Schema {
5241
s, err := Load(DefaultExternalSchema)
5342
if err == nil {
5443
return WithSchema(s)
5544
}
56-
return WithSchema(BuiltinSchema())
45+
return BuiltinSchema()
5746
}

0 commit comments

Comments
 (0)