string.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. package cc
  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 (pgv::Utf8Len({{ accessor . }}) != {{ $r.GetLen }}) {
  12. {{ err . "value must be " $r.GetLen " characters" }}
  13. }
  14. {{ else }}
  15. if (pgv::Utf8Len({{ accessor . }}) != {{ $r.GetMinLen }}) {
  16. {{ err . "value must be " $r.GetMinLen " characters" }}
  17. }
  18. {{ end }}
  19. {{ else if $r.MinLen }}
  20. {{ if $r.MaxLen }}
  21. {
  22. const auto length = pgv::Utf8Len({{ accessor . }});
  23. if (length < {{ $r.GetMinLen }} || length > {{ $r.GetMaxLen }}) {
  24. {{ err . "value must have between " $r.GetMinLen " and " $r.GetMaxLen " characters inclusive" }}
  25. }
  26. }
  27. {{ else }}
  28. if (pgv::Utf8Len({{ accessor . }}) < {{ $r.GetMinLen }}) {
  29. {{ err . "value length must be at least " $r.GetMinLen " characters" }}
  30. }
  31. {{ end }}
  32. {{ else if $r.MaxLen }}
  33. if (pgv::Utf8Len({{ accessor . }}) > {{ $r.GetMaxLen }}) {
  34. {{ err . "value length must be at most " $r.GetMaxLen " characters" }}
  35. }
  36. {{ end }}
  37. {{ if or $r.LenBytes (and $r.MinBytes $r.MaxBytes (eq $r.GetMinBytes $r.GetMaxBytes)) }}
  38. {
  39. const auto length = {{ accessor . }}.size();
  40. {{ if $r.LenBytes }}
  41. if (length != {{ $r.GetLenBytes }}) {
  42. {{ err . "value length must be " $r.GetLenBytes " bytes" }}
  43. }
  44. {{ else }}
  45. if (length != {{ $r.GetMinBytes }}) {
  46. {{ err . "value length must be " $r.GetMinBytes " bytes" }}
  47. }
  48. {{ end }}
  49. }
  50. {{ else if $r.MinBytes }}
  51. {
  52. const auto length = {{ accessor . }}.size();
  53. {{ if $r.MaxBytes }}
  54. {{ if eq $r.GetMinBytes $r.GetMaxBytes }}
  55. if (length != {{ $r.GetMinBytes }}) {
  56. {{ err . "value length must be " $r.GetMinBytes " bytes" }}
  57. }
  58. {{ else }}
  59. if (length < {{ $r.GetMinBytes }} || length > {{ $r.GetMaxBytes }}) {
  60. {{ err . "value length must be between " $r.GetMinBytes " and " $r.GetMaxBytes " bytes, inclusive" }}
  61. }
  62. {{ end }}
  63. {{ else }}
  64. if (length < {{ $r.GetMinBytes }}) {
  65. {{ err . "value length must be at least " $r.GetMinBytes " bytes" }}
  66. }
  67. {{ end }}
  68. }
  69. {{ else if $r.MaxBytes }}
  70. if ({{ accessor . }}.size() > {{ $r.GetMaxBytes }}) {
  71. {{ err . "value length must be at most " $r.GetMaxBytes " bytes" }}
  72. }
  73. {{ end }}
  74. {{ if $r.Prefix }}
  75. {
  76. const std::string prefix = {{ lit $r.GetPrefix }};
  77. if (!pgv::IsPrefix(prefix, {{ accessor . }})) {
  78. {{ err . "value does not have prefix " (lit $r.GetPrefix) }}
  79. }
  80. }
  81. {{ end }}
  82. {{ if $r.Suffix }}
  83. {
  84. const std::string suffix = {{ lit $r.GetSuffix }};
  85. const std::string& value = {{ accessor . }};
  86. if (!pgv::IsSuffix(suffix, value)) {
  87. {{ err . "value does not have suffix " (lit $r.GetSuffix) }}
  88. }
  89. }
  90. {{ end }}
  91. {{ if $r.Contains }}
  92. {
  93. if (!pgv::Contains({{ accessor . }}, {{ lit $r.GetContains }})) {
  94. {{ err . "value does not contain substring " (lit $r.GetContains) }}
  95. }
  96. }
  97. {{ end }}
  98. {{ if $r.NotContains }}
  99. {
  100. if (pgv::Contains({{ accessor . }}, {{ lit $r.GetNotContains }})) {
  101. {{ err . "value contains substring " (lit $r.GetNotContains) }}
  102. }
  103. }
  104. {{ end }}
  105. {{ if $r.Pattern }}
  106. {
  107. if (!RE2::FullMatch(re2::StringPiece({{ accessor . }}.c_str(), {{ accessor . }}.size()),
  108. {{ lookup $f "Pattern" }})) {
  109. {{ err . "value does not match regex pattern " (lit $r.GetPattern) }}
  110. }
  111. }
  112. {{ end }}
  113. {{ if $r.GetIp }}
  114. {
  115. const std::string& value = {{ accessor . }};
  116. if (!pgv::IsIp(value)) {
  117. {{ err . "value must be a valid IP Address" }}
  118. }
  119. }
  120. {{ else if $r.GetIpv4 }}
  121. {
  122. const std::string& value = {{ accessor . }};
  123. if (!pgv::IsIpv4(value)) {
  124. {{ err . "value must be a valid IPv4 Address" }}
  125. }
  126. }
  127. {{ else if $r.GetIpv6 }}
  128. {
  129. const std::string& value = {{ accessor . }};
  130. if (!pgv::IsIpv6(value)) {
  131. {{ err . "value must be a valid IPv6 Address" }}
  132. }
  133. }
  134. {{ else if $r.GetEmail }}
  135. {{ unimplemented "C++ email address validation is not implemented" }}
  136. {{/* TODO(akonradi) implement email address constraints
  137. if err := m._validateEmail({{ accessor . }}); err != nil {
  138. return {{ errCause . "err" "value must be a valid email address" }}
  139. }
  140. */}}
  141. {{ else if $r.GetAddress }}
  142. {
  143. const std::string& value = {{ accessor . }};
  144. if (!pgv::IsHostname(value) && !pgv::IsIp(value)) {
  145. {{ err . "value must be an ip address, or a hostname." }}
  146. }
  147. }
  148. {{ else if $r.GetHostname }}
  149. {
  150. const std::string& value = {{ accessor . }};
  151. if (!pgv::IsHostname(value)) {
  152. {{ err . "value must be a valid hostname" }}
  153. }
  154. }
  155. {{ else if $r.GetUri }}
  156. {{ unimplemented "C++ URI validation is not implemented" }}
  157. {{/* TODO(akonradi) implement URI constraints
  158. if uri, err := url.Parse({{ accessor . }}); err != nil {
  159. return {{ errCause . "err" "value must be a valid URI" }}
  160. } else if !uri.IsAbs() {
  161. return {{ err . "value must be absolute" }}
  162. }
  163. */}}
  164. {{ else if $r.GetUriRef }}
  165. {{ unimplemented "C++ URI validation is not implemented" }}
  166. {{/* TODO(akonradi) implement URI constraints
  167. if _, err := url.Parse({{ accessor . }}); err != nil {
  168. return {{ errCause . "err" "value must be a valid URI" }}
  169. }
  170. */}}
  171. {{ else if $r.GetUuid }}
  172. if (!RE2::FullMatch(re2::StringPiece({{ accessor . }}), pgv::validate::_uuidPattern)) {
  173. {{ err . "value must be a valid UUID" }}
  174. }
  175. {{ end }}
  176. {{ if $r.GetIgnoreEmpty }}
  177. }
  178. {{ end }}
  179. `