github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/znet/valid.go (about)

     1  package znet
     2  
     3  import (
     4  	"github.com/sohaha/zlsgo/zvalid"
     5  )
     6  
     7  func (c *Context) ValidRule() zvalid.Engine {
     8  	return zvalid.New()
     9  }
    10  
    11  // ValidParam get and verify routing parameters
    12  func (c *Context) ValidParam(defRule zvalid.Engine, key string, name ...string) zvalid.Engine {
    13  	return valid(defRule, c.GetParam(key), key, name...)
    14  }
    15  
    16  // ValidQuery get and verify query
    17  func (c *Context) ValidQuery(defRule zvalid.Engine, key string, name ...string) zvalid.Engine {
    18  	return valid(defRule, c.DefaultQuery(key, ""), key, name...)
    19  }
    20  
    21  // ValidForm get and verify postform
    22  func (c *Context) ValidForm(defRule zvalid.Engine, key string, name ...string) zvalid.Engine {
    23  	return valid(defRule, c.DefaultPostForm(key, ""), key, name...)
    24  }
    25  
    26  // ValidJSON get and verify json
    27  func (c *Context) ValidJSON(defRule zvalid.Engine, key string, name ...string) zvalid.Engine {
    28  	return valid(defRule, c.GetJSON(key).String(), key, name...)
    29  }
    30  
    31  // Valid Valid from -> query -> parame
    32  func (c *Context) Valid(defRule zvalid.Engine, key string, name ...string) zvalid.Engine {
    33  	value, contentType := "", c.ContentType()
    34  	if contentType == c.ContentType(ContentTypeJSON) || contentType == c.ContentType(ContentTypePlain) {
    35  		value = c.GetJSON(key).String()
    36  	}
    37  	if value == "" {
    38  		value = c.DefaultFormOrQuery(key, "")
    39  	}
    40  	if value == "" {
    41  		value = c.GetParam(key)
    42  	}
    43  	return valid(defRule, value, key, name...)
    44  }
    45  
    46  func valid(defRule zvalid.Engine, value, key string, name ...string) (valid zvalid.Engine) {
    47  	return defRule.Verifi(value, name...)
    48  }