forked from topfreegames/pitaya
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add default struct validator pipeline (topfreegames#42)
- Loading branch information
Showing
6 changed files
with
88 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package defaultpipelines | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
validator "gopkg.in/go-playground/validator.v9" | ||
) | ||
|
||
// DefaultValidator is the default arguments validator for handlers | ||
// in pitaya | ||
type DefaultValidator struct { | ||
once sync.Once | ||
validate *validator.Validate | ||
} | ||
|
||
// Validate is the the function responsible for validating the 'in' parameter | ||
// based on the struct tags the parameter has. | ||
// This function has the pipeline.Handler signature so | ||
// it is possible to use it as a pipeline function | ||
func (v *DefaultValidator) Validate(ctx context.Context, in interface{}) (interface{}, error) { | ||
v.lazyinit() | ||
if err := v.validate.Struct(in); err != nil { | ||
return nil, err | ||
} | ||
|
||
return in, nil | ||
} | ||
|
||
func (v *DefaultValidator) lazyinit() { | ||
v.once.Do(func() { | ||
v.validate = validator.New() | ||
}) | ||
} |
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,17 @@ | ||
package defaultpipelines | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
// StructValidator is the interface that must be implemented | ||
// by a struct validator for the request arguments on pitaya. | ||
// | ||
// The default struct validator used by pitaya is https://github.com/go-playground/validator. | ||
type StructValidator interface { | ||
Validate(context.Context, interface{}) (interface{}, error) | ||
} | ||
|
||
// StructValidatorInstance holds the default validator | ||
// on start but can be overridden if needed. | ||
var StructValidatorInstance StructValidator = &DefaultValidator{} |
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