add custom regex validator

pull/753/head
Lucas Mendes Loureiro 4 years ago
parent b926bf0df9
commit c304f52add
  1. 7
      baked_in.go
  2. 9
      translations/en/en.go
  3. 5
      translations/es/es.go
  4. 5
      translations/fr/fr.go
  5. 5
      translations/id/id.go
  6. 5
      translations/ja/ja.go
  7. 5
      translations/nl/nl.go
  8. 5
      translations/pt/pt.go
  9. 5
      translations/pt_BR/pt_BR.go
  10. 5
      translations/ru/ru.go
  11. 5
      translations/tr/tr.go
  12. 5
      translations/zh/zh.go
  13. 5
      translations/zh_tw/zh_tw.go

@ -11,6 +11,7 @@ import (
"net/url" "net/url"
"os" "os"
"reflect" "reflect"
"regexp"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
@ -133,6 +134,7 @@ var (
"endswith": endsWith, "endswith": endsWith,
"startsnotwith": startsNotWith, "startsnotwith": startsNotWith,
"endsnotwith": endsNotWith, "endsnotwith": endsNotWith,
"regex": regex,
"isbn": isISBN, "isbn": isISBN,
"isbn10": isISBN10, "isbn10": isISBN10,
"isbn13": isISBN13, "isbn13": isISBN13,
@ -767,6 +769,11 @@ func fieldExcludes(fl FieldLevel) bool {
return !strings.Contains(field.String(), currentField.String()) return !strings.Contains(field.String(), currentField.String())
} }
func regex(fl FieldLevel) bool {
regex := regexp.MustCompile(fl.Param())
return regex.MatchString(fl.Field().String())
}
// IsNeField is the validation function for validating if the current field's value is not equal to the field specified by the param's value. // IsNeField is the validation function for validating if the current field's value is not equal to the field specified by the param's value.
func isNeField(fl FieldLevel) bool { func isNeField(fl FieldLevel) bool {

@ -1143,6 +1143,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return t return t
}, },
}, },
{
tag: "regex",
translation: "{0} is not in the correct format",
override: false,
},
{ {
tag: "isbn", tag: "isbn",
translation: "{0} must be a valid ISBN number", translation: "{0} must be a valid ISBN number",
@ -1325,8 +1330,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
tag: "json", tag: "json",
translation: "{0} must be a valid json string", translation: "{0} must be a valid json string",
override: false, override: false,
}, },
{ {
tag: "lowercase", tag: "lowercase",
translation: "{0} must be a lowercase string", translation: "{0} must be a lowercase string",
override: false, override: false,

@ -1143,6 +1143,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return t return t
}, },
}, },
{
tag: "regex",
translation: "{0} no tiene el formato correcto",
override: false,
},
{ {
tag: "isbn", tag: "isbn",
translation: "{0} debe ser un número ISBN válido", translation: "{0} debe ser un número ISBN válido",

@ -1138,6 +1138,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return t return t
}, },
}, },
{
tag: "regex",
translation: "{0} n'est pas au bon format",
override: false,
},
{ {
tag: "isbn", tag: "isbn",
translation: "{0} doit être un numéro ISBN valid", translation: "{0} doit être un numéro ISBN valid",

@ -1138,6 +1138,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return t return t
}, },
}, },
{
tag: "regex",
translation: "Format {0} salah",
override: false,
},
{ {
tag: "isbn", tag: "isbn",
translation: "{0} harus berupa nomor ISBN yang valid", translation: "{0} harus berupa nomor ISBN yang valid",

@ -1194,6 +1194,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return t return t
}, },
}, },
{
tag: "regex",
translation: "{0}は正しい形式ではありません",
override: false,
},
{ {
tag: "isbn", tag: "isbn",
translation: "{0}は正しいISBN番号でなければなりません", translation: "{0}は正しいISBN番号でなければなりません",

@ -1138,6 +1138,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return t return t
}, },
}, },
{
tag: "regex",
translation: "{0} is not in the correct format",
override: false,
},
{ {
tag: "isbn", tag: "isbn",
translation: "{0} moet een geldig ISBN nummer zijn", translation: "{0} moet een geldig ISBN nummer zijn",

@ -1143,6 +1143,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return t return t
}, },
}, },
{
tag: "regex",
translation: "{0} não está no formato correto",
override: false,
},
{ {
tag: "isbn", tag: "isbn",
translation: "{0} deve ser um número de ISBN válido", translation: "{0} deve ser um número de ISBN válido",

@ -1138,6 +1138,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return t return t
}, },
}, },
{
tag: "regex",
translation: "{0} não está no formato correto",
override: false,
},
{ {
tag: "isbn", tag: "isbn",
translation: "{0} deve ser um número ISBN válido", translation: "{0} deve ser um número ISBN válido",

@ -1143,6 +1143,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return t return t
}, },
}, },
{
tag: "regex",
translation: "{0} имеет неправильный формат",
override: false,
},
{ {
tag: "isbn", tag: "isbn",
translation: "{0} должен быть ISBN номером", translation: "{0} должен быть ISBN номером",

@ -1138,6 +1138,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return t return t
}, },
}, },
{
tag: "regex",
translation: "{0} doğru biçimde değil",
override: false,
},
{ {
tag: "isbn", tag: "isbn",
translation: "{0} geçerli bir ISBN numarası olmalıdır", translation: "{0} geçerli bir ISBN numarası olmalıdır",

@ -1135,6 +1135,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return t return t
}, },
}, },
{
tag: "regex",
translation: "{0}的格式不正確",
override: false,
},
{ {
tag: "isbn", tag: "isbn",
translation: "{0}必须是一个有效的ISBN编号", translation: "{0}必须是一个有效的ISBN编号",

@ -1131,6 +1131,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
return t return t
}, },
}, },
{
tag: "regex",
translation: "{0}的格式不正確",
override: false,
},
{ {
tag: "isbn", tag: "isbn",
translation: "{0}必須是一個有效的ISBN編號", translation: "{0}必須是一個有效的ISBN編號",

Loading…
Cancel
Save