github.com/johnnyeven/libtools@v0.0.0-20191126065708-61829c1adf46/validate/validatetpl/unity_social_credit_code.go (about)

     1  package validatetpl
     2  
     3  import (
     4  	"regexp"
     5  )
     6  
     7  const (
     8  	InvalidUnitySocialCreditCodeType  = "社会信用代码类型错误"
     9  	InvalidUnitySocialCreditCodeValue = "无效的社会信用代码"
    10  )
    11  
    12  var (
    13  	unity_social_credit_code_regexp = regexp.MustCompile(`^\w{18}$`)
    14  )
    15  
    16  func ValidateUnitySocialCreditCode(v interface{}) (bool, string) {
    17  	s, ok := v.(string)
    18  	if !ok {
    19  		return false, InvalidUnitySocialCreditCodeType
    20  	}
    21  	if !unity_social_credit_code_regexp.MatchString(s) {
    22  		return false, InvalidUnitySocialCreditCodeValue
    23  	}
    24  	return true, ""
    25  }
    26  
    27  func ValidateUnitySocialCreditCodeOrEmpty(v interface{}) (bool, string) {
    28  	s, ok := v.(string)
    29  	if !ok {
    30  		return false, InvalidUnitySocialCreditCodeType
    31  	}
    32  	if s != "" && !unity_social_credit_code_regexp.MatchString(s) {
    33  		return false, InvalidUnitySocialCreditCodeValue
    34  	}
    35  	return true, ""
    36  }