From c304f52add2dadc07de013133d2b28e310f9d49b Mon Sep 17 00:00:00 2001 From: Lucas Mendes Loureiro Date: Thu, 15 Apr 2021 23:43:07 -0300 Subject: [PATCH] add custom regex validator --- baked_in.go | 7 +++++++ translations/en/en.go | 9 +++++++-- translations/es/es.go | 5 +++++ translations/fr/fr.go | 5 +++++ translations/id/id.go | 5 +++++ translations/ja/ja.go | 5 +++++ translations/nl/nl.go | 5 +++++ translations/pt/pt.go | 5 +++++ translations/pt_BR/pt_BR.go | 5 +++++ translations/ru/ru.go | 5 +++++ translations/tr/tr.go | 5 +++++ translations/zh/zh.go | 5 +++++ translations/zh_tw/zh_tw.go | 5 +++++ 13 files changed, 69 insertions(+), 2 deletions(-) diff --git a/baked_in.go b/baked_in.go index dd25705..ca6ac7a 100644 --- a/baked_in.go +++ b/baked_in.go @@ -11,6 +11,7 @@ import ( "net/url" "os" "reflect" + "regexp" "strconv" "strings" "sync" @@ -133,6 +134,7 @@ var ( "endswith": endsWith, "startsnotwith": startsNotWith, "endsnotwith": endsNotWith, + "regex": regex, "isbn": isISBN, "isbn10": isISBN10, "isbn13": isISBN13, @@ -767,6 +769,11 @@ func fieldExcludes(fl FieldLevel) bool { 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. func isNeField(fl FieldLevel) bool { diff --git a/translations/en/en.go b/translations/en/en.go index 1fdea95..863d81b 100644 --- a/translations/en/en.go +++ b/translations/en/en.go @@ -1143,6 +1143,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er return t }, }, + { + tag: "regex", + translation: "{0} is not in the correct format", + override: false, + }, { tag: "isbn", translation: "{0} must be a valid ISBN number", @@ -1325,8 +1330,8 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er tag: "json", translation: "{0} must be a valid json string", override: false, - }, - { + }, + { tag: "lowercase", translation: "{0} must be a lowercase string", override: false, diff --git a/translations/es/es.go b/translations/es/es.go index 2b87f10..afa4c19 100644 --- a/translations/es/es.go +++ b/translations/es/es.go @@ -1143,6 +1143,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er return t }, }, + { + tag: "regex", + translation: "{0} no tiene el formato correcto", + override: false, + }, { tag: "isbn", translation: "{0} debe ser un número ISBN válido", diff --git a/translations/fr/fr.go b/translations/fr/fr.go index ddebdf9..60577c9 100644 --- a/translations/fr/fr.go +++ b/translations/fr/fr.go @@ -1138,6 +1138,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er return t }, }, + { + tag: "regex", + translation: "{0} n'est pas au bon format", + override: false, + }, { tag: "isbn", translation: "{0} doit être un numéro ISBN valid", diff --git a/translations/id/id.go b/translations/id/id.go index d185dfc..014f89c 100644 --- a/translations/id/id.go +++ b/translations/id/id.go @@ -1138,6 +1138,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er return t }, }, + { + tag: "regex", + translation: "Format {0} salah", + override: false, + }, { tag: "isbn", translation: "{0} harus berupa nomor ISBN yang valid", diff --git a/translations/ja/ja.go b/translations/ja/ja.go index 43ec648..400eed2 100644 --- a/translations/ja/ja.go +++ b/translations/ja/ja.go @@ -1194,6 +1194,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er return t }, }, + { + tag: "regex", + translation: "{0}は正しい形式ではありません", + override: false, + }, { tag: "isbn", translation: "{0}は正しいISBN番号でなければなりません", diff --git a/translations/nl/nl.go b/translations/nl/nl.go index a64815d..16274c1 100644 --- a/translations/nl/nl.go +++ b/translations/nl/nl.go @@ -1138,6 +1138,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er return t }, }, + { + tag: "regex", + translation: "{0} is not in the correct format", + override: false, + }, { tag: "isbn", translation: "{0} moet een geldig ISBN nummer zijn", diff --git a/translations/pt/pt.go b/translations/pt/pt.go index 9dbaf5f..c37d245 100644 --- a/translations/pt/pt.go +++ b/translations/pt/pt.go @@ -1143,6 +1143,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er return t }, }, + { + tag: "regex", + translation: "{0} não está no formato correto", + override: false, + }, { tag: "isbn", translation: "{0} deve ser um número de ISBN válido", diff --git a/translations/pt_BR/pt_BR.go b/translations/pt_BR/pt_BR.go index df4fc67..9e41b7f 100644 --- a/translations/pt_BR/pt_BR.go +++ b/translations/pt_BR/pt_BR.go @@ -1138,6 +1138,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er return t }, }, + { + tag: "regex", + translation: "{0} não está no formato correto", + override: false, + }, { tag: "isbn", translation: "{0} deve ser um número ISBN válido", diff --git a/translations/ru/ru.go b/translations/ru/ru.go index 6f9d41b..434560c 100644 --- a/translations/ru/ru.go +++ b/translations/ru/ru.go @@ -1143,6 +1143,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er return t }, }, + { + tag: "regex", + translation: "{0} имеет неправильный формат", + override: false, + }, { tag: "isbn", translation: "{0} должен быть ISBN номером", diff --git a/translations/tr/tr.go b/translations/tr/tr.go index 2709e4b..eb793eb 100644 --- a/translations/tr/tr.go +++ b/translations/tr/tr.go @@ -1138,6 +1138,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er return t }, }, + { + tag: "regex", + translation: "{0} doğru biçimde değil", + override: false, + }, { tag: "isbn", translation: "{0} geçerli bir ISBN numarası olmalıdır", diff --git a/translations/zh/zh.go b/translations/zh/zh.go index 6b6cce1..c0de642 100644 --- a/translations/zh/zh.go +++ b/translations/zh/zh.go @@ -1135,6 +1135,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er return t }, }, + { + tag: "regex", + translation: "{0}的格式不正確", + override: false, + }, { tag: "isbn", translation: "{0}必须是一个有效的ISBN编号", diff --git a/translations/zh_tw/zh_tw.go b/translations/zh_tw/zh_tw.go index 391a010..400806a 100644 --- a/translations/zh_tw/zh_tw.go +++ b/translations/zh_tw/zh_tw.go @@ -1131,6 +1131,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er return t }, }, + { + tag: "regex", + translation: "{0}的格式不正確", + override: false, + }, { tag: "isbn", translation: "{0}必須是一個有效的ISBN編號",