Skip to content

Commit c49c42c

Browse files
committed
#### Version 1.5.3
* New feature: HttpServer add Validator which be called by Context.Validate() * New feature: Context add Validate(interface{}) used to validate data with HttpServer::Validator * Update: use routerExpress_Split replace "_" when padding data to Router::RouterExpress * 2018-07-12 12:00
1 parent 403e3e8 commit c49c42c

File tree

7 files changed

+34
-2
lines changed

7 files changed

+34
-2
lines changed

context.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ type (
6666
Inline(file string, name string) error
6767
Bind(i interface{}) error
6868
BindJsonBody(i interface{}) error
69+
// Validate validates provided `i`. It is usually called after `Context#Bind()`.
70+
Validate(i interface{}) error
6971
GetRouterName(key string) string
7072
RemoteIP() string
7173
SetCookieValue(name, value string, maxAge int)
@@ -426,6 +428,14 @@ func (ctx *HttpContext) Bind(i interface{}) error {
426428
func (ctx *HttpContext) BindJsonBody(i interface{}) error {
427429
return ctx.httpServer.Binder().BindJsonBody(i, ctx)
428430
}
431+
// Validate validates data with HttpServer::Validator
432+
// We will implementing inner validator on next version
433+
func (ctx *HttpContext) Validate(i interface{}) error {
434+
if ctx.httpServer.Validator == nil {
435+
return ErrValidatorNotRegistered
436+
}
437+
return ctx.httpServer.Validator.Validate(i)
438+
}
429439

430440
// GetRouterName get router name
431441
func (ctx *HttpContext) GetRouterName(key string) string {

dotweb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ func (app *DotWeb) initBindMiddleware() {
436436
router := app.HttpServer.Router().(*router)
437437
//bind app middlewares
438438
for fullExpress, _ := range router.allRouterExpress {
439-
expresses := strings.Split(fullExpress, "_")
439+
expresses := strings.Split(fullExpress, routerExpress_Split)
440440
if len(expresses) < 2 {
441441
continue
442442
}

errors.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package dotweb
2+
3+
import "errors"
4+
5+
var ErrValidatorNotRegistered = errors.New("validator not registered")

router.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ const (
3131
RouteMethod_WebSocket = "WEBSOCKET"
3232
)
3333

34+
const(
35+
routerExpress_Split = "^$^"
36+
)
37+
3438
var (
3539
HttpMethodMap map[string]string
3640
valueNodePool sync.Pool
@@ -500,7 +504,7 @@ func (r *router) add(method, path string, handle RouterHandle, m ...Middleware)
500504
//fmt.Println("Handle => ", method, " - ", *root, " - ", path)
501505
outnode = root.addRoute(path, handle, m...)
502506
outnode.fullPath = path
503-
r.allRouterExpress[method+"_"+path] = struct{}{}
507+
r.allRouterExpress[method+routerExpress_Split+path] = struct{}{}
504508
return
505509
}
506510

server.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type (
2929
groups []Group
3030
Modules []*HttpModule
3131
DotApp *DotWeb
32+
Validator Validator
3233
sessionManager *session.SessionManager
3334
lock_session *sync.RWMutex
3435
pool *pool

validator.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package dotweb
2+
3+
// Validator is the interface that wraps the Validate function.
4+
type Validator interface {
5+
Validate(i interface{}) error
6+
}

version.MD

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## dotweb版本记录:
22

3+
#### Version 1.5.3
4+
* New feature: HttpServer add Validator which be called by Context.Validate()
5+
* New feature: Context add Validate(interface{}) used to validate data with HttpServer::Validator
6+
* Update: use routerExpress_Split replace "_" when padding data to Router::RouterExpress
7+
* 2018-07-12 12:00
8+
39
#### Version 1.5.2
410
* New feature: dotweb.innerRenderer add cache mode, default is enabled
511
* New feature: dotweb.innerRenderer add NewInnerRendererNoCache() used to disabled cache

0 commit comments

Comments
 (0)