Skip to content

Commit

Permalink
Added ulid validation (go-playground#826)
Browse files Browse the repository at this point in the history
  • Loading branch information
uberswe authored Jan 2, 2022
1 parent ec2071b commit 8fe074c
Show file tree
Hide file tree
Showing 31 changed files with 191 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
6 changes: 6 additions & 0 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ var (
"uuid3_rfc4122": isUUID3RFC4122,
"uuid4_rfc4122": isUUID4RFC4122,
"uuid5_rfc4122": isUUID5RFC4122,
"ulid": isULID,
"ascii": isASCII,
"printascii": isPrintableASCII,
"multibyte": hasMultiByteCharacter,
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions regexes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions translations/en/en.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions translations/en/en_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions translations/es/es.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 6 additions & 1 deletion translations/es/es_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions translations/fa/fa.go
Original file line number Diff line number Diff line change
Expand Up @@ -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} باید فقط شامل کاراکترهای اسکی باشد",
Expand Down
5 changes: 5 additions & 0 deletions translations/fa/fa_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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 باید یک شابک معتبر باشد",
Expand Down
5 changes: 5 additions & 0 deletions translations/fr/fr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions translations/fr/fr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions translations/id/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 6 additions & 1 deletion translations/id/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions translations/ja/ja.go
Original file line number Diff line number Diff line change
Expand Up @@ -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文字のみを含まなければなりません",
Expand Down
7 changes: 6 additions & 1 deletion translations/ja/ja_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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番号でなければなりません",
Expand Down
5 changes: 5 additions & 0 deletions translations/nl/nl.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 6 additions & 1 deletion translations/nl/nl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions translations/pt/pt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions translations/pt/pt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions translations/pt_BR/pt_BR.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 6 additions & 1 deletion translations/pt_BR/pt_BR_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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"`
Expand Down Expand Up @@ -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",
Expand Down
Loading

0 comments on commit 8fe074c

Please sign in to comment.