Skip to content

Commit 1d36b4a

Browse files
authored
Add .Validate() to ytypes.Schema and ygot.ValidateGoStruct() (#732)
* Add .Validate() to ytypes.Schema * Add ygot.ValidateGoStruct
1 parent 6259f3a commit 1d36b4a

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

ygot/struct_validation_map.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -411,18 +411,13 @@ func EmitJSON(gs GoStruct, opts *EmitJSONConfig) (string, error) {
411411
skipValidation = opts.SkipValidation
412412
}
413413

414-
s, ok := gs.(validatedGoStruct)
415-
if !ok {
416-
return "", fmt.Errorf("input GoStruct does not have ΛValidate() method")
417-
}
418-
419414
if !skipValidation {
420-
if err := s.ΛValidate(vopts...); err != nil {
415+
if err := ValidateGoStruct(gs, vopts...); err != nil {
421416
return "", fmt.Errorf("validation err: %v", err)
422417
}
423418
}
424419

425-
v, err := makeJSON(s, opts)
420+
v, err := makeJSON(gs, opts)
426421
if err != nil {
427422
return "", err
428423
}

ygot/types.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package ygot
1616

1717
import (
18+
"fmt"
1819
"reflect"
1920
)
2021

@@ -56,6 +57,15 @@ type ValidatedGoStruct interface {
5657
ΛBelongingModule() string
5758
}
5859

60+
// ValidateGoStruct validates a GoStruct.
61+
func ValidateGoStruct(goStruct GoStruct, vopts ...ValidationOption) error {
62+
vroot, ok := goStruct.(validatedGoStruct)
63+
if !ok {
64+
return fmt.Errorf("GoStruct cannot be validated: (%T, %v)", goStruct, goStruct)
65+
}
66+
return vroot.ΛValidate(vopts...)
67+
}
68+
5969
// validatedGoStruct is an interface used for validating GoStructs.
6070
// This interface is implemented by all Go structs (YANG container or lists),
6171
// regardless of generation flag.

ytypes/types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package ytypes
1616

1717
import (
18+
"errors"
1819
"reflect"
1920

2021
"github.com/openconfig/goyang/pkg/yang"
@@ -41,5 +42,13 @@ func (s *Schema) RootSchema() *yang.Entry {
4142
return s.SchemaTree[reflect.TypeOf(s.Root).Elem().Name()]
4243
}
4344

45+
// Validate performs schema validation on the schema root.
46+
func (s *Schema) Validate(vopts ...ygot.ValidationOption) error {
47+
if !s.IsValid() {
48+
return errors.New("invalid schema: not fully populated")
49+
}
50+
return ygot.ValidateGoStruct(s.Root, vopts...)
51+
}
52+
4453
// UnmarshalFunc defines a common signature for an RFC7951 to ygot.GoStruct unmarshalling function
4554
type UnmarshalFunc func([]byte, ygot.GoStruct, ...UnmarshalOpt) error

ytypes/types_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ func TestSchema(t *testing.T) {
6868
t.Errorf("did not get expected valid status, got: %v, want: %v", got, tt.wantValid)
6969
}
7070

71+
if got := tt.in.Validate(); (got == nil) != tt.wantValid {
72+
t.Errorf("did not get expected validate return, got: %v, want: %v", got, tt.wantValid)
73+
}
74+
7175
if !tt.wantValid {
7276
return
7377
}

0 commit comments

Comments
 (0)