Skip to content

Commit

Permalink
Add .Validate() to ytypes.Schema and ygot.ValidateGoStruct() (#732)
Browse files Browse the repository at this point in the history
* Add .Validate() to ytypes.Schema

* Add ygot.ValidateGoStruct
  • Loading branch information
wenovus authored Sep 30, 2022
1 parent 6259f3a commit 1d36b4a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
9 changes: 2 additions & 7 deletions ygot/struct_validation_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,18 +411,13 @@ func EmitJSON(gs GoStruct, opts *EmitJSONConfig) (string, error) {
skipValidation = opts.SkipValidation
}

s, ok := gs.(validatedGoStruct)
if !ok {
return "", fmt.Errorf("input GoStruct does not have ΛValidate() method")
}

if !skipValidation {
if err := s.ΛValidate(vopts...); err != nil {
if err := ValidateGoStruct(gs, vopts...); err != nil {
return "", fmt.Errorf("validation err: %v", err)
}
}

v, err := makeJSON(s, opts)
v, err := makeJSON(gs, opts)
if err != nil {
return "", err
}
Expand Down
10 changes: 10 additions & 0 deletions ygot/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package ygot

import (
"fmt"
"reflect"
)

Expand Down Expand Up @@ -56,6 +57,15 @@ type ValidatedGoStruct interface {
ΛBelongingModule() string
}

// ValidateGoStruct validates a GoStruct.
func ValidateGoStruct(goStruct GoStruct, vopts ...ValidationOption) error {
vroot, ok := goStruct.(validatedGoStruct)
if !ok {
return fmt.Errorf("GoStruct cannot be validated: (%T, %v)", goStruct, goStruct)
}
return vroot.ΛValidate(vopts...)
}

// validatedGoStruct is an interface used for validating GoStructs.
// This interface is implemented by all Go structs (YANG container or lists),
// regardless of generation flag.
Expand Down
9 changes: 9 additions & 0 deletions ytypes/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package ytypes

import (
"errors"
"reflect"

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

// Validate performs schema validation on the schema root.
func (s *Schema) Validate(vopts ...ygot.ValidationOption) error {
if !s.IsValid() {
return errors.New("invalid schema: not fully populated")
}
return ygot.ValidateGoStruct(s.Root, vopts...)
}

// UnmarshalFunc defines a common signature for an RFC7951 to ygot.GoStruct unmarshalling function
type UnmarshalFunc func([]byte, ygot.GoStruct, ...UnmarshalOpt) error
4 changes: 4 additions & 0 deletions ytypes/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func TestSchema(t *testing.T) {
t.Errorf("did not get expected valid status, got: %v, want: %v", got, tt.wantValid)
}

if got := tt.in.Validate(); (got == nil) != tt.wantValid {
t.Errorf("did not get expected validate return, got: %v, want: %v", got, tt.wantValid)
}

if !tt.wantValid {
return
}
Expand Down

0 comments on commit 1d36b4a

Please sign in to comment.