string.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package goshared
  2. const strTpl = `
  3. {{ $f := .Field }}{{ $r := .Rules }}
  4. {{ if $r.GetIgnoreEmpty }}
  5. if {{ accessor . }} != "" {
  6. {{ end }}
  7. {{ template "const" . }}
  8. {{ template "in" . }}
  9. {{ if or $r.Len (and $r.MinLen $r.MaxLen (eq $r.GetMinLen $r.GetMaxLen)) }}
  10. {{ if $r.Len }}
  11. if utf8.RuneCountInString({{ accessor . }}) != {{ $r.GetLen }} {
  12. err := {{ err . "value length must be " $r.GetLen " runes" }}
  13. if !all { return err }
  14. errors = append(errors, err)
  15. {{ else }}
  16. if utf8.RuneCountInString({{ accessor . }}) != {{ $r.GetMinLen }} {
  17. err := {{ err . "value length must be " $r.GetMinLen " runes" }}
  18. if !all { return err }
  19. errors = append(errors, err)
  20. {{ end }}
  21. }
  22. {{ else if $r.MinLen }}
  23. {{ if $r.MaxLen }}
  24. if l := utf8.RuneCountInString({{ accessor . }}); l < {{ $r.GetMinLen }} || l > {{ $r.GetMaxLen }} {
  25. err := {{ err . "value length must be between " $r.GetMinLen " and " $r.GetMaxLen " runes, inclusive" }}
  26. if !all { return err }
  27. errors = append(errors, err)
  28. }
  29. {{ else }}
  30. if utf8.RuneCountInString({{ accessor . }}) < {{ $r.GetMinLen }} {
  31. err := {{ err . "value length must be at least " $r.GetMinLen " runes" }}
  32. if !all { return err }
  33. errors = append(errors, err)
  34. }
  35. {{ end }}
  36. {{ else if $r.MaxLen }}
  37. if utf8.RuneCountInString({{ accessor . }}) > {{ $r.GetMaxLen }} {
  38. err := {{ err . "value length must be at most " $r.GetMaxLen " runes" }}
  39. if !all { return err }
  40. errors = append(errors, err)
  41. }
  42. {{ end }}
  43. {{ if or $r.LenBytes (and $r.MinBytes $r.MaxBytes (eq $r.GetMinBytes $r.GetMaxBytes)) }}
  44. {{ if $r.LenBytes }}
  45. if len({{ accessor . }}) != {{ $r.GetLenBytes }} {
  46. err := {{ err . "value length must be " $r.GetLenBytes " bytes" }}
  47. if !all { return err }
  48. errors = append(errors, err)
  49. }
  50. {{ else }}
  51. if len({{ accessor . }}) != {{ $r.GetMinBytes }} {
  52. err := {{ err . "value length must be " $r.GetMinBytes " bytes" }}
  53. if !all { return err }
  54. errors = append(errors, err)
  55. }
  56. {{ end }}
  57. {{ else if $r.MinBytes }}
  58. {{ if $r.MaxBytes }}
  59. if l := len({{ accessor . }}); l < {{ $r.GetMinBytes }} || l > {{ $r.GetMaxBytes }} {
  60. err := {{ err . "value length must be between " $r.GetMinBytes " and " $r.GetMaxBytes " bytes, inclusive" }}
  61. if !all { return err }
  62. errors = append(errors, err)
  63. }
  64. {{ else }}
  65. if len({{ accessor . }}) < {{ $r.GetMinBytes }} {
  66. err := {{ err . "value length must be at least " $r.GetMinBytes " bytes" }}
  67. if !all { return err }
  68. errors = append(errors, err)
  69. }
  70. {{ end }}
  71. {{ else if $r.MaxBytes }}
  72. if len({{ accessor . }}) > {{ $r.GetMaxBytes }} {
  73. err := {{ err . "value length must be at most " $r.GetMaxBytes " bytes" }}
  74. if !all { return err }
  75. errors = append(errors, err)
  76. }
  77. {{ end }}
  78. {{ if $r.Prefix }}
  79. if !strings.HasPrefix({{ accessor . }}, {{ lit $r.GetPrefix }}) {
  80. err := {{ err . "value does not have prefix " (lit $r.GetPrefix) }}
  81. if !all { return err }
  82. errors = append(errors, err)
  83. }
  84. {{ end }}
  85. {{ if $r.Suffix }}
  86. if !strings.HasSuffix({{ accessor . }}, {{ lit $r.GetSuffix }}) {
  87. err := {{ err . "value does not have suffix " (lit $r.GetSuffix) }}
  88. if !all { return err }
  89. errors = append(errors, err)
  90. }
  91. {{ end }}
  92. {{ if $r.Contains }}
  93. if !strings.Contains({{ accessor . }}, {{ lit $r.GetContains }}) {
  94. err := {{ err . "value does not contain substring " (lit $r.GetContains) }}
  95. if !all { return err }
  96. errors = append(errors, err)
  97. }
  98. {{ end }}
  99. {{ if $r.NotContains }}
  100. if strings.Contains({{ accessor . }}, {{ lit $r.GetNotContains }}) {
  101. err := {{ err . "value contains substring " (lit $r.GetNotContains) }}
  102. if !all { return err }
  103. errors = append(errors, err)
  104. }
  105. {{ end }}
  106. {{ if $r.GetIp }}
  107. if ip := net.ParseIP({{ accessor . }}); ip == nil {
  108. err := {{ err . "value must be a valid IP address" }}
  109. if !all { return err }
  110. errors = append(errors, err)
  111. }
  112. {{ else if $r.GetIpv4 }}
  113. if ip := net.ParseIP({{ accessor . }}); ip == nil || ip.To4() == nil {
  114. err := {{ err . "value must be a valid IPv4 address" }}
  115. if !all { return err }
  116. errors = append(errors, err)
  117. }
  118. {{ else if $r.GetIpv6 }}
  119. if ip := net.ParseIP({{ accessor . }}); ip == nil || ip.To4() != nil {
  120. err := {{ err . "value must be a valid IPv6 address" }}
  121. if !all { return err }
  122. errors = append(errors, err)
  123. }
  124. {{ else if $r.GetEmail }}
  125. if err := m._validateEmail({{ accessor . }}); err != nil {
  126. err = {{ errCause . "err" "value must be a valid email address" }}
  127. if !all { return err }
  128. errors = append(errors, err)
  129. }
  130. {{ else if $r.GetHostname }}
  131. if err := m._validateHostname({{ accessor . }}); err != nil {
  132. err = {{ errCause . "err" "value must be a valid hostname" }}
  133. if !all { return err }
  134. errors = append(errors, err)
  135. }
  136. {{ else if $r.GetAddress }}
  137. if err := m._validateHostname({{ accessor . }}); err != nil {
  138. if ip := net.ParseIP({{ accessor . }}); ip == nil {
  139. err := {{ err . "value must be a valid hostname, or ip address" }}
  140. if !all { return err }
  141. errors = append(errors, err)
  142. }
  143. }
  144. {{ else if $r.GetUri }}
  145. if uri, err := url.Parse({{ accessor . }}); err != nil {
  146. err = {{ errCause . "err" "value must be a valid URI" }}
  147. if !all { return err }
  148. errors = append(errors, err)
  149. } else if !uri.IsAbs() {
  150. err := {{ err . "value must be absolute" }}
  151. if !all { return err }
  152. errors = append(errors, err)
  153. }
  154. {{ else if $r.GetUriRef }}
  155. if _, err := url.Parse({{ accessor . }}); err != nil {
  156. err = {{ errCause . "err" "value must be a valid URI" }}
  157. if !all { return err }
  158. errors = append(errors, err)
  159. }
  160. {{ else if $r.GetUuid }}
  161. if err := m._validateUuid({{ accessor . }}); err != nil {
  162. err = {{ errCause . "err" "value must be a valid UUID" }}
  163. if !all { return err }
  164. errors = append(errors, err)
  165. }
  166. {{ end }}
  167. {{ if $r.Pattern }}
  168. if !{{ lookup $f "Pattern" }}.MatchString({{ accessor . }}) {
  169. err := {{ err . "value does not match regex pattern " (lit $r.GetPattern) }}
  170. if !all { return err }
  171. errors = append(errors, err)
  172. }
  173. {{ end }}
  174. {{ if $r.GetIgnoreEmpty }}
  175. }
  176. {{ end }}
  177. `