From 52cb7ec27f6068de98f6d36db71bb8e4444069a6 Mon Sep 17 00:00:00 2001 From: Klaus Date: Wed, 8 Apr 2020 15:56:23 +0800 Subject: [PATCH] add GetValidate func to get the default validator (#558) * add GetValidate func to get the default validator * warden server expose GetValidate func Co-authored-by: John Sun --- pkg/net/http/blademaster/binding/binding.go | 3 +++ pkg/net/http/blademaster/binding/default_validator.go | 5 +++++ pkg/net/rpc/warden/validate.go | 5 +++++ 3 files changed, 13 insertions(+) diff --git a/pkg/net/http/blademaster/binding/binding.go b/pkg/net/http/blademaster/binding/binding.go index 46f1c770d..ea2c8e017 100644 --- a/pkg/net/http/blademaster/binding/binding.go +++ b/pkg/net/http/blademaster/binding/binding.go @@ -37,6 +37,9 @@ type StructValidator interface { // NOTE: if the key already exists, the previous validation function will be replaced. // NOTE: this method is not thread-safe it is intended that these all be registered prior to any validation RegisterValidation(string, validator.Func) error + + //GetValidate return the default validate + GetValidate() *validator.Validate } // Validator default validator. diff --git a/pkg/net/http/blademaster/binding/default_validator.go b/pkg/net/http/blademaster/binding/default_validator.go index 5dbf67ed2..9e1e8fc02 100644 --- a/pkg/net/http/blademaster/binding/default_validator.go +++ b/pkg/net/http/blademaster/binding/default_validator.go @@ -43,3 +43,8 @@ func kindOfData(data interface{}) reflect.Kind { } return valueType } + +func (v *defaultValidator) GetValidate() *validator.Validate { + v.lazyinit() + return v.validate +} \ No newline at end of file diff --git a/pkg/net/rpc/warden/validate.go b/pkg/net/rpc/warden/validate.go index f9444fca0..ddd715ca6 100644 --- a/pkg/net/rpc/warden/validate.go +++ b/pkg/net/rpc/warden/validate.go @@ -29,3 +29,8 @@ func (s *Server) validate() grpc.UnaryServerInterceptor { func (s *Server) RegisterValidation(key string, fn validator.Func) error { return validate.RegisterValidation(key, fn) } + +//GetValidate return the default validate +func (s *Server) GetValidate() *validator.Validate { + return validate +}