Skip to content

Commit 41e31bc

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

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
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: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,27 @@ const (
2525
DefaultExternalSchema = "/etc/cdi/schema/schema.json"
2626
)
2727

28+
type Validator interface {
29+
Validate(*cdi.Spec) error
30+
}
31+
2832
// 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-
}
33+
func WithSchema(s *Schema) Validator {
34+
return s
3835
}
3936

4037
// WithNamedSchema loads the named JSON schema and returns a CDI Spec
4138
// validator for it. If loading the schema fails a dummy validator is
4239
// returned.
43-
func WithNamedSchema(name string) func(*cdi.Spec) error {
40+
func WithNamedSchema(name string) Validator {
4441
s, _ := Load(name)
4542
return WithSchema(s)
4643
}
4744

4845
// WithDefaultSchema returns a CDI Spec validator that uses the default
4946
// external JSON schema, or the default builtin one if the external one
5047
// fails to load.
51-
func WithDefaultSchema() func(*cdi.Spec) error {
48+
func WithDefaultSchema() Validator {
5249
s, err := Load(DefaultExternalSchema)
5350
if err == nil {
5451
return WithSchema(s)

0 commit comments

Comments
 (0)