Added ulid validation (#826)

pull/875/head
Markus Tenghamn 2 years ago committed by GitHub
parent ec2071b383
commit 8fe074c546
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      README.md
  2. 6
      baked_in.go
  3. 6
      doc.go
  4. 2
      regexes.go
  5. 5
      translations/en/en.go
  6. 5
      translations/en/en_test.go
  7. 5
      translations/es/es.go
  8. 7
      translations/es/es_test.go
  9. 5
      translations/fa/fa.go
  10. 5
      translations/fa/fa_test.go
  11. 5
      translations/fr/fr.go
  12. 5
      translations/fr/fr_test.go
  13. 5
      translations/id/id.go
  14. 7
      translations/id/id_test.go
  15. 5
      translations/ja/ja.go
  16. 7
      translations/ja/ja_test.go
  17. 5
      translations/nl/nl.go
  18. 7
      translations/nl/nl_test.go
  19. 5
      translations/pt/pt.go
  20. 5
      translations/pt/pt_test.go
  21. 5
      translations/pt_BR/pt_BR.go
  22. 7
      translations/pt_BR/pt_BR_test.go
  23. 5
      translations/ru/ru.go
  24. 5
      translations/ru/ru_test.go
  25. 5
      translations/tr/tr.go
  26. 7
      translations/tr/tr_test.go
  27. 5
      translations/zh/zh.go
  28. 5
      translations/zh/zh_test.go
  29. 5
      translations/zh_tw/zh_tw.go
  30. 5
      translations/zh_tw/zh_tw_test.go
  31. 40
      validator_test.go

@ -189,6 +189,7 @@ Baked-in Validations
| uuid5 | Universally Unique Identifier UUID v5 |
| uuid5_rfc4122 | Universally Unique Identifier UUID v5 RFC4122 |
| uuid_rfc4122 | Universally Unique Identifier UUID RFC4122 |
| ulid | Universally Unique Lexicographically Sortable Identifier ULID |
### Comparisons:
| Tag | Description |

@ -148,6 +148,7 @@ var (
"uuid3_rfc4122": isUUID3RFC4122,
"uuid4_rfc4122": isUUID4RFC4122,
"uuid5_rfc4122": isUUID5RFC4122,
"ulid": isULID,
"ascii": isASCII,
"printascii": isPrintableASCII,
"multibyte": hasMultiByteCharacter,
@ -499,6 +500,11 @@ func isUUIDRFC4122(fl FieldLevel) bool {
return uUIDRFC4122Regex.MatchString(fl.Field().String())
}
// isULID is the validation function for validating if the field's value is a valid ULID.
func isULID(fl FieldLevel) bool {
return uLIDRegex.MatchString(fl.Field().String())
}
// isISBN is the validation function for validating if the field's value is a valid v10 or v13 ISBN.
func isISBN(fl FieldLevel) bool {
return isISBN10(fl) || isISBN13(fl)

@ -1007,6 +1007,12 @@ This validates that a string value contains a valid version 5 UUID. Uppercase U
Usage: uuid5
Universally Unique Lexicographically Sortable Identifier ULID
This validates that a string value contains a valid ULID value.
Usage: ulid
ASCII
This validates that a string value contains only ASCII characters.

@ -29,6 +29,7 @@ const (
uUID4RFC4122RegexString = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$"
uUID5RFC4122RegexString = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-5[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$"
uUIDRFC4122RegexString = "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"
uLIDRegexString = "^[A-HJKMNP-TV-Z0-9]{26}$"
aSCIIRegexString = "^[\x00-\x7F]*$"
printableASCIIRegexString = "^[\x20-\x7E]*$"
multibyteRegexString = "[^\x00-\x7F]"
@ -81,6 +82,7 @@ var (
uUID4RFC4122Regex = regexp.MustCompile(uUID4RFC4122RegexString)
uUID5RFC4122Regex = regexp.MustCompile(uUID5RFC4122RegexString)
uUIDRFC4122Regex = regexp.MustCompile(uUIDRFC4122RegexString)
uLIDRegex = regexp.MustCompile(uLIDRegexString)
aSCIIRegex = regexp.MustCompile(aSCIIRegexString)
printableASCIIRegex = regexp.MustCompile(printableASCIIRegexString)
multibyteRegex = regexp.MustCompile(multibyteRegexString)

@ -1136,6 +1136,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
translation: "{0} must be a valid version 5 UUID",
override: false,
},
{
tag: "ulid",
translation: "{0} must be a valid ULID",
override: false,
},
{
tag: "ascii",
translation: "{0} must contain only ascii characters",

@ -103,6 +103,7 @@ func TestTranslations(t *testing.T) {
UUID3 string `validate:"uuid3"`
UUID4 string `validate:"uuid4"`
UUID5 string `validate:"uuid5"`
ULID string `validate:"ulid"`
ASCII string `validate:"ascii"`
PrintableASCII string `validate:"printascii"`
MultiByte string `validate:"multibyte"`
@ -323,6 +324,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.UUID5",
expected: "UUID5 must be a valid version 5 UUID",
},
{
ns: "Test.ULID",
expected: "ULID must be a valid ULID",
},
{
ns: "Test.ISBN",
expected: "ISBN must be a valid ISBN number",

@ -1178,6 +1178,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
translation: "{0} debe ser una versión válida 5 UUID",
override: false,
},
{
tag: "ulid",
translation: "{0} debe ser un ULID válido",
override: false,
},
{
tag: "ascii",
translation: "{0} debe contener sólo caracteres ascii",

@ -4,9 +4,9 @@ import (
"testing"
"time"
. "github.com/go-playground/assert/v2"
spanish "github.com/go-playground/locales/es"
ut "github.com/go-playground/universal-translator"
. "github.com/go-playground/assert/v2"
"github.com/go-playground/validator/v10"
)
@ -104,6 +104,7 @@ func TestTranslations(t *testing.T) {
UUID3 string `validate:"uuid3"`
UUID4 string `validate:"uuid4"`
UUID5 string `validate:"uuid5"`
ULID string `validate:"ulid"`
ASCII string `validate:"ascii"`
PrintableASCII string `validate:"printascii"`
MultiByte string `validate:"multibyte"`
@ -312,6 +313,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.UUID5",
expected: "UUID5 debe ser una versión válida 5 UUID",
},
{
ns: "Test.ULID",
expected: "ULID debe ser un ULID válido",
},
{
ns: "Test.ISBN",
expected: "ISBN debe ser un número ISBN válido",

@ -1136,6 +1136,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
translation: "{0} باید یک UUID نسخه 5 معتبر باشد",
override: false,
},
{
tag: "ulid",
translation: "{0} باید یک ULID معتبر باشد",
override: false,
},
{
tag: "ascii",
translation: "{0} باید فقط شامل کاراکترهای اسکی باشد",

@ -103,6 +103,7 @@ func TestTranslations(t *testing.T) {
UUID3 string `validate:"uuid3"`
UUID4 string `validate:"uuid4"`
UUID5 string `validate:"uuid5"`
ULID string `validate:"ulid"`
ASCII string `validate:"ascii"`
PrintableASCII string `validate:"printascii"`
MultiByte string `validate:"multibyte"`
@ -322,6 +323,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.UUID5",
expected: "UUID5 باید یک UUID نسخه 5 معتبر باشد",
},
{
ns: "Test.ULID",
expected: "ULID باید یک ULID معتبر باشد",
},
{
ns: "Test.ISBN",
expected: "ISBN باید یک شابک معتبر باشد",

@ -1173,6 +1173,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
translation: "{0} doit être un UUID version 5 valid",
override: false,
},
{
tag: "ulid",
translation: "{0} doit être une ULID valide",
override: false,
},
{
tag: "ascii",
translation: "{0} ne doit contenir que des caractères ascii",

@ -104,6 +104,7 @@ func TestTranslations(t *testing.T) {
UUID3 string `validate:"uuid3"`
UUID4 string `validate:"uuid4"`
UUID5 string `validate:"uuid5"`
ULID string `validate:"ulid"`
ASCII string `validate:"ascii"`
PrintableASCII string `validate:"printascii"`
MultiByte string `validate:"multibyte"`
@ -306,6 +307,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.UUID5",
expected: "UUID5 doit être un UUID version 5 valid",
},
{
ns: "Test.ULID",
expected: "ULID doit être une ULID valide",
},
{
ns: "Test.ISBN",
expected: "ISBN doit être un numéro ISBN valid",

@ -1173,6 +1173,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
translation: "{0} harus berupa UUID versi 5 yang valid",
override: false,
},
{
tag: "ulid",
translation: "{0} harus berupa ULID yang valid",
override: false,
},
{
tag: "ascii",
translation: "{0} hanya boleh berisi karakter ascii",

@ -4,9 +4,9 @@ import (
"testing"
"time"
. "github.com/go-playground/assert/v2"
indonesia "github.com/go-playground/locales/id"
ut "github.com/go-playground/universal-translator"
. "github.com/go-playground/assert/v2"
"github.com/go-playground/validator/v10"
)
@ -104,6 +104,7 @@ func TestTranslations(t *testing.T) {
UUID3 string `validate:"uuid3"`
UUID4 string `validate:"uuid4"`
UUID5 string `validate:"uuid5"`
ULID string `validate:"ulid"`
ASCII string `validate:"ascii"`
PrintableASCII string `validate:"printascii"`
MultiByte string `validate:"multibyte"`
@ -306,6 +307,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.UUID5",
expected: "UUID5 harus berupa UUID versi 5 yang valid",
},
{
ns: "Test.ULID",
expected: "ULID harus berupa ULID yang valid",
},
{
ns: "Test.ISBN",
expected: "ISBN harus berupa nomor ISBN yang valid",

@ -1229,6 +1229,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
translation: "{0}はバージョンが5の正しいUUIDでなければなりません",
override: false,
},
{
tag: "ulid",
translation: "{0}は正しいULIDでなければなりません",
override: false,
},
{
tag: "ascii",
translation: "{0}はASCII文字のみを含まなければなりません",

@ -4,9 +4,9 @@ import (
"testing"
"time"
. "github.com/go-playground/assert/v2"
ja_locale "github.com/go-playground/locales/ja"
ut "github.com/go-playground/universal-translator"
. "github.com/go-playground/assert/v2"
"github.com/go-playground/validator/v10"
)
@ -104,6 +104,7 @@ func TestTranslations(t *testing.T) {
UUID3 string `validate:"uuid3"`
UUID4 string `validate:"uuid4"`
UUID5 string `validate:"uuid5"`
ULID string `validate:"ulid"`
ASCII string `validate:"ascii"`
PrintableASCII string `validate:"printascii"`
MultiByte string `validate:"multibyte"`
@ -306,6 +307,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.UUID5",
expected: "UUID5はバージョンが5の正しいUUIDでなければなりません",
},
{
ns: "Test.ULID",
expected: "ULIDは正しいULIDでなければなりません",
},
{
ns: "Test.ISBN",
expected: "ISBNは正しいISBN番号でなければなりません",

@ -1173,6 +1173,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
translation: "{0} moet een geldige versie 5 UUID zijn",
override: false,
},
{
tag: "ulid",
translation: "{0} moet een geldige ULID zijn",
override: false,
},
{
tag: "ascii",
translation: "{0} mag alleen ascii karakters bevatten",

@ -4,9 +4,9 @@ import (
"testing"
"time"
. "github.com/go-playground/assert/v2"
english "github.com/go-playground/locales/en"
ut "github.com/go-playground/universal-translator"
. "github.com/go-playground/assert/v2"
"github.com/go-playground/validator/v10"
)
@ -104,6 +104,7 @@ func TestTranslations(t *testing.T) {
UUID3 string `validate:"uuid3"`
UUID4 string `validate:"uuid4"`
UUID5 string `validate:"uuid5"`
ULID string `validate:"ulid"`
ASCII string `validate:"ascii"`
PrintableASCII string `validate:"printascii"`
MultiByte string `validate:"multibyte"`
@ -306,6 +307,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.UUID5",
expected: "UUID5 moet een geldige versie 5 UUID zijn",
},
{
ns: "Test.ULID",
expected: "ULID moet een geldige ULID zijn",
},
{
ns: "Test.ISBN",
expected: "ISBN moet een geldig ISBN nummer zijn",

@ -1178,6 +1178,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
translation: "{0} deve ser um UUID versão 5 válido",
override: false,
},
{
tag: "ulid",
translation: "{0} deve ser um ULID válido",
override: false,
},
{
tag: "ascii",
translation: "{0} deve conter apenas caracteres ascii",

@ -105,6 +105,7 @@ func TestTranslations(t *testing.T) {
UUID3 string `validate:"uuid3"`
UUID4 string `validate:"uuid4"`
UUID5 string `validate:"uuid5"`
ULID string `validate:"ulid"`
ASCII string `validate:"ascii"`
PrintableASCII string `validate:"printascii"`
MultiByte string `validate:"multibyte"`
@ -321,6 +322,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.UUID5",
expected: "UUID5 deve ser um UUID versão 5 válido",
},
{
ns: "Test.ULID",
expected: "ULID deve ser um ULID válido",
},
{
ns: "Test.ISBN",
expected: "ISBN deve ser um número de ISBN válido",

@ -1173,6 +1173,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
translation: "{0} deve ser um UUID versão 5 válido",
override: false,
},
{
tag: "ulid",
translation: "{0} deve ser uma ULID válida",
override: false,
},
{
tag: "ascii",
translation: "{0} deve conter apenas caracteres ascii",

@ -4,10 +4,10 @@ import (
"testing"
"time"
. "github.com/go-playground/assert/v2"
brazilian_portuguese "github.com/go-playground/locales/pt_BR"
ut "github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
. "github.com/go-playground/assert/v2"
)
func TestTranslations(t *testing.T) {
@ -104,6 +104,7 @@ func TestTranslations(t *testing.T) {
UUID3 string `validate:"uuid3"`
UUID4 string `validate:"uuid4"`
UUID5 string `validate:"uuid5"`
ULID string `validate:"ulid"`
ASCII string `validate:"ascii"`
PrintableASCII string `validate:"printascii"`
MultiByte string `validate:"multibyte"`
@ -306,6 +307,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.UUID5",
expected: "UUID5 deve ser um UUID versão 5 válido",
},
{
ns: "Test.ULID",
expected: "ULID deve ser uma ULID válida",
},
{
ns: "Test.ISBN",
expected: "ISBN deve ser um número ISBN válido",

@ -1291,6 +1291,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
translation: "{0} должен быть UUID 5 версии",
override: false,
},
{
tag: "ulid",
translation: "{0} должен быть ULID",
override: false,
},
{
tag: "ascii",
translation: "{0} должен содержать только ascii символы",

@ -120,6 +120,7 @@ func TestTranslations(t *testing.T) {
UUID3 string `validate:"uuid3"`
UUID4 string `validate:"uuid4"`
UUID5 string `validate:"uuid5"`
ULID string `validate:"ulid"`
ASCII string `validate:"ascii"`
PrintableASCII string `validate:"printascii"`
MultiByte string `validate:"multibyte"`
@ -340,6 +341,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.UUID5",
expected: "UUID5 должен быть UUID 5 версии",
},
{
ns: "Test.ULID",
expected: "ULID должен быть ULID",
},
{
ns: "Test.ISBN",
expected: "ISBN должен быть ISBN номером",

@ -1173,6 +1173,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
translation: "{0} geçerli bir sürüm 5 UUID olmalıdır",
override: false,
},
{
tag: "ulid",
translation: "{0} geçerli bir ULID olmalıdır",
override: false,
},
{
tag: "ascii",
translation: "{0} yalnızca ascii karakterler içermelidir",

@ -4,9 +4,9 @@ import (
"testing"
"time"
. "github.com/go-playground/assert/v2"
turkish "github.com/go-playground/locales/tr"
ut "github.com/go-playground/universal-translator"
. "github.com/go-playground/assert/v2"
"github.com/go-playground/validator/v10"
)
@ -104,6 +104,7 @@ func TestTranslations(t *testing.T) {
UUID3 string `validate:"uuid3"`
UUID4 string `validate:"uuid4"`
UUID5 string `validate:"uuid5"`
ULID string `validate:"ulid"`
ASCII string `validate:"ascii"`
PrintableASCII string `validate:"printascii"`
MultiByte string `validate:"multibyte"`
@ -312,6 +313,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.UUID5",
expected: "UUID5 geçerli bir sürüm 5 UUID olmalıdır",
},
{
ns: "Test.ULID",
expected: "ULID geçerli bir ULID olmalıdır",
},
{
ns: "Test.ISBN",
expected: "ISBN geçerli bir ISBN numarası olmalıdır",

@ -1225,6 +1225,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
translation: "{0}必须是一个有效的V5 UUID",
override: false,
},
{
tag: "ulid",
translation: "{0}必须是一个有效的ULID",
override: false,
},
{
tag: "ascii",
translation: "{0}必须只包含ascii字符",

@ -109,6 +109,7 @@ func TestTranslations(t *testing.T) {
UUID3 string `validate:"uuid3"`
UUID4 string `validate:"uuid4"`
UUID5 string `validate:"uuid5"`
ULID string `validate:"ulid"`
ASCII string `validate:"ascii"`
PrintableASCII string `validate:"printascii"`
MultiByte string `validate:"multibyte"`
@ -327,6 +328,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.UUID5",
expected: "UUID5必须是一个有效的V5 UUID",
},
{
ns: "Test.ULID",
expected: "ULID必须是一个有效的ULID",
},
{
ns: "Test.ISBN",
expected: "ISBN必须是一个有效的ISBN编号",

@ -1166,6 +1166,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
translation: "{0}必須是一個有效的V5 UUID",
override: false,
},
{
tag: "ulid",
translation: "{0}必須是一個有效的ULID",
override: false,
},
{
tag: "ascii",
translation: "{0}必須只包含ascii字元",

@ -104,6 +104,7 @@ func TestTranslations(t *testing.T) {
UUID3 string `validate:"uuid3"`
UUID4 string `validate:"uuid4"`
UUID5 string `validate:"uuid5"`
ULID string `validate:"ulid"`
ASCII string `validate:"ascii"`
PrintableASCII string `validate:"printascii"`
MultiByte string `validate:"multibyte"`
@ -309,6 +310,10 @@ func TestTranslations(t *testing.T) {
ns: "Test.UUID5",
expected: "UUID5必須是一個有效的V5 UUID",
},
{
ns: "Test.ULID",
expected: "ULID必須是一個有效的ULID",
},
{
ns: "Test.ISBN",
expected: "ISBN必須是一個有效的ISBN編號",

@ -4269,6 +4269,46 @@ func TestUUIDRFC4122Validation(t *testing.T) {
}
}
func TestULIDValidation(t *testing.T) {
tests := []struct {
param string
expected bool
}{
{"", false},
{"01BX5ZZKBKACT-V9WEVGEMMVRZ", false},
{"01bx5zzkbkactav9wevgemmvrz", false},
{"a987Fbc9-4bed-3078-cf07-9141ba07c9f3xxx", false},
{"01BX5ZZKBKACTAV9WEVGEMMVRZABC", false},
{"01BX5ZZKBKACTAV9WEVGEMMVRZABC", false},
{"0IBX5ZZKBKACTAV9WEVGEMMVRZ", false},
{"O1BX5ZZKBKACTAV9WEVGEMMVRZ", false},
{"01BX5ZZKBKACTAVLWEVGEMMVRZ", false},
{"01BX5ZZKBKACTAV9WEVGEMMVRZ", true},
}
validate := New()
for i, test := range tests {
errs := validate.Var(test.param, "ulid")
if test.expected {
if !IsEqual(errs, nil) {
t.Fatalf("Index: %d ULID failed Error: %s", i, errs)
}
} else {
if IsEqual(errs, nil) {
t.Fatalf("Index: %d ULID failed Error: %s", i, errs)
} else {
val := getError(errs, "", "")
if val.Tag() != "ulid" {
t.Fatalf("Index: %d ULID failed Error: %s", i, errs)
}
}
}
}
}
func TestISBNValidation(t *testing.T) {
tests := []struct {
param string

Loading…
Cancel
Save