-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhancement: Migrate to protovalidate
Signed-off-by: Oğuzhan Durgun <[email protected]>
- Loading branch information
1 parent
c9e8743
commit 1df6057
Showing
5 changed files
with
77 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2021-2023 Zenauth Ltd. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package validator | ||
|
||
import ( | ||
"fmt" | ||
policyv1 "github.com/cerbos/cerbos/api/genpb/cerbos/policy/v1" | ||
"log" | ||
|
||
"github.com/bufbuild/protovalidate-go" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
var Validator *protovalidate.Validator | ||
|
||
func init() { | ||
var err error | ||
if Validator, err = Init(); err != nil { | ||
log.Fatal(err.Error()) | ||
} | ||
} | ||
|
||
func Init() (*protovalidate.Validator, error) { | ||
v, err := protovalidate.New( | ||
protovalidate.WithMessages( | ||
&policyv1.Policy{}, | ||
), | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create validator: %w", err) | ||
} | ||
|
||
return v, nil | ||
} | ||
|
||
func Validate(msg proto.Message) error { | ||
return Validator.Validate(msg) | ||
} |