-
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 <oguzhandurgun95@gmail.com>
- Loading branch information
1 parent
18b6a59
commit efbaffb
Showing
6 changed files
with
84 additions
and
24 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
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,40 @@ | ||
// Copyright 2021-2023 Zenauth Ltd. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package validator | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/bufbuild/protovalidate-go" | ||
"google.golang.org/protobuf/proto" | ||
|
||
policyv1 "github.com/cerbos/cerbos/api/genpb/cerbos/policy/v1" | ||
) | ||
|
||
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) | ||
} |