123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190 |
- package goshared
- const strTpl = `
- {{ $f := .Field }}{{ $r := .Rules }}
- {{ if $r.GetIgnoreEmpty }}
- if {{ accessor . }} != "" {
- {{ end }}
- {{ template "const" . }}
- {{ template "in" . }}
- {{ if or $r.Len (and $r.MinLen $r.MaxLen (eq $r.GetMinLen $r.GetMaxLen)) }}
- {{ if $r.Len }}
- if utf8.RuneCountInString({{ accessor . }}) != {{ $r.GetLen }} {
- err := {{ err . "value length must be " $r.GetLen " runes" }}
- if !all { return err }
- errors = append(errors, err)
- {{ else }}
- if utf8.RuneCountInString({{ accessor . }}) != {{ $r.GetMinLen }} {
- err := {{ err . "value length must be " $r.GetMinLen " runes" }}
- if !all { return err }
- errors = append(errors, err)
- {{ end }}
- }
- {{ else if $r.MinLen }}
- {{ if $r.MaxLen }}
- if l := utf8.RuneCountInString({{ accessor . }}); l < {{ $r.GetMinLen }} || l > {{ $r.GetMaxLen }} {
- err := {{ err . "value length must be between " $r.GetMinLen " and " $r.GetMaxLen " runes, inclusive" }}
- if !all { return err }
- errors = append(errors, err)
- }
- {{ else }}
- if utf8.RuneCountInString({{ accessor . }}) < {{ $r.GetMinLen }} {
- err := {{ err . "value length must be at least " $r.GetMinLen " runes" }}
- if !all { return err }
- errors = append(errors, err)
- }
- {{ end }}
- {{ else if $r.MaxLen }}
- if utf8.RuneCountInString({{ accessor . }}) > {{ $r.GetMaxLen }} {
- err := {{ err . "value length must be at most " $r.GetMaxLen " runes" }}
- if !all { return err }
- errors = append(errors, err)
- }
- {{ end }}
- {{ if or $r.LenBytes (and $r.MinBytes $r.MaxBytes (eq $r.GetMinBytes $r.GetMaxBytes)) }}
- {{ if $r.LenBytes }}
- if len({{ accessor . }}) != {{ $r.GetLenBytes }} {
- err := {{ err . "value length must be " $r.GetLenBytes " bytes" }}
- if !all { return err }
- errors = append(errors, err)
- }
- {{ else }}
- if len({{ accessor . }}) != {{ $r.GetMinBytes }} {
- err := {{ err . "value length must be " $r.GetMinBytes " bytes" }}
- if !all { return err }
- errors = append(errors, err)
- }
- {{ end }}
- {{ else if $r.MinBytes }}
- {{ if $r.MaxBytes }}
- if l := len({{ accessor . }}); l < {{ $r.GetMinBytes }} || l > {{ $r.GetMaxBytes }} {
- err := {{ err . "value length must be between " $r.GetMinBytes " and " $r.GetMaxBytes " bytes, inclusive" }}
- if !all { return err }
- errors = append(errors, err)
- }
- {{ else }}
- if len({{ accessor . }}) < {{ $r.GetMinBytes }} {
- err := {{ err . "value length must be at least " $r.GetMinBytes " bytes" }}
- if !all { return err }
- errors = append(errors, err)
- }
- {{ end }}
- {{ else if $r.MaxBytes }}
- if len({{ accessor . }}) > {{ $r.GetMaxBytes }} {
- err := {{ err . "value length must be at most " $r.GetMaxBytes " bytes" }}
- if !all { return err }
- errors = append(errors, err)
- }
- {{ end }}
- {{ if $r.Prefix }}
- if !strings.HasPrefix({{ accessor . }}, {{ lit $r.GetPrefix }}) {
- err := {{ err . "value does not have prefix " (lit $r.GetPrefix) }}
- if !all { return err }
- errors = append(errors, err)
- }
- {{ end }}
- {{ if $r.Suffix }}
- if !strings.HasSuffix({{ accessor . }}, {{ lit $r.GetSuffix }}) {
- err := {{ err . "value does not have suffix " (lit $r.GetSuffix) }}
- if !all { return err }
- errors = append(errors, err)
- }
- {{ end }}
- {{ if $r.Contains }}
- if !strings.Contains({{ accessor . }}, {{ lit $r.GetContains }}) {
- err := {{ err . "value does not contain substring " (lit $r.GetContains) }}
- if !all { return err }
- errors = append(errors, err)
- }
- {{ end }}
- {{ if $r.NotContains }}
- if strings.Contains({{ accessor . }}, {{ lit $r.GetNotContains }}) {
- err := {{ err . "value contains substring " (lit $r.GetNotContains) }}
- if !all { return err }
- errors = append(errors, err)
- }
- {{ end }}
- {{ if $r.GetIp }}
- if ip := net.ParseIP({{ accessor . }}); ip == nil {
- err := {{ err . "value must be a valid IP address" }}
- if !all { return err }
- errors = append(errors, err)
- }
- {{ else if $r.GetIpv4 }}
- if ip := net.ParseIP({{ accessor . }}); ip == nil || ip.To4() == nil {
- err := {{ err . "value must be a valid IPv4 address" }}
- if !all { return err }
- errors = append(errors, err)
- }
- {{ else if $r.GetIpv6 }}
- if ip := net.ParseIP({{ accessor . }}); ip == nil || ip.To4() != nil {
- err := {{ err . "value must be a valid IPv6 address" }}
- if !all { return err }
- errors = append(errors, err)
- }
- {{ else if $r.GetEmail }}
- if err := m._validateEmail({{ accessor . }}); err != nil {
- err = {{ errCause . "err" "value must be a valid email address" }}
- if !all { return err }
- errors = append(errors, err)
- }
- {{ else if $r.GetHostname }}
- if err := m._validateHostname({{ accessor . }}); err != nil {
- err = {{ errCause . "err" "value must be a valid hostname" }}
- if !all { return err }
- errors = append(errors, err)
- }
- {{ else if $r.GetAddress }}
- if err := m._validateHostname({{ accessor . }}); err != nil {
- if ip := net.ParseIP({{ accessor . }}); ip == nil {
- err := {{ err . "value must be a valid hostname, or ip address" }}
- if !all { return err }
- errors = append(errors, err)
- }
- }
- {{ else if $r.GetUri }}
- if uri, err := url.Parse({{ accessor . }}); err != nil {
- err = {{ errCause . "err" "value must be a valid URI" }}
- if !all { return err }
- errors = append(errors, err)
- } else if !uri.IsAbs() {
- err := {{ err . "value must be absolute" }}
- if !all { return err }
- errors = append(errors, err)
- }
- {{ else if $r.GetUriRef }}
- if _, err := url.Parse({{ accessor . }}); err != nil {
- err = {{ errCause . "err" "value must be a valid URI" }}
- if !all { return err }
- errors = append(errors, err)
- }
- {{ else if $r.GetUuid }}
- if err := m._validateUuid({{ accessor . }}); err != nil {
- err = {{ errCause . "err" "value must be a valid UUID" }}
- if !all { return err }
- errors = append(errors, err)
- }
- {{ end }}
- {{ if $r.Pattern }}
- if !{{ lookup $f "Pattern" }}.MatchString({{ accessor . }}) {
- err := {{ err . "value does not match regex pattern " (lit $r.GetPattern) }}
- if !all { return err }
- errors = append(errors, err)
- }
- {{ end }}
- {{ if $r.GetIgnoreEmpty }}
- }
- {{ end }}
- `
|