Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhancement: Migrate to protovalidate #115

Merged
merged 1 commit into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bundle/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func (c *Client) parseBootstrapConf(input io.Reader) (*bootstrapv1.PDPConfig, er
return nil, fmt.Errorf("failed to unmarshal bootstrap proto: %w", err)
}

if err := out.ValidateAll(); err != nil {
if err := Validate(out); err != nil {
return out, fmt.Errorf("invalid bootstrap configuration: %w", err)
}

Expand Down
2 changes: 1 addition & 1 deletion bundle/client_conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (cc ClientConf) Validate() (outErr error) {

if cc.PDPIdentifier == nil {
outErr = multierr.Append(outErr, errMissingIdentifier)
} else if err := cc.PDPIdentifier.ValidateAll(); err != nil {
} else if err := Validate(cc.PDPIdentifier); err != nil {
outErr = multierr.Append(outErr, fmt.Errorf("invalid PDP identifier: %w", err))
}

Expand Down
40 changes: 40 additions & 0 deletions bundle/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2021-2023 Zenauth Ltd.
// SPDX-License-Identifier: Apache-2.0

package bundle

import (
"fmt"
"sync"

"github.com/bufbuild/protovalidate-go"
"google.golang.org/protobuf/proto"

bootstrapv1 "github.com/cerbos/cloud-api/genpb/cerbos/cloud/bootstrap/v1"
)

var (
validateFn func(proto.Message) error
validatorOnce sync.Once
)

func Validate[T proto.Message](obj T) error {
validatorOnce.Do(func() {
validator, err := protovalidate.New(
protovalidate.WithMessages(
&bootstrapv1.PDPConfig{},
),
)
if err != nil {
validateFn = func(_ proto.Message) error {
return fmt.Errorf("failed to initialize validator: %w", err)
}
} else {
validateFn = func(m proto.Message) error {
return validator.Validate(m)
}
}
})

return validateFn(obj)
}
76 changes: 38 additions & 38 deletions genpb/cerbos/cloud/apikey/v1/apikey.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading