Skip to content

Commit 8fe074c

Browse files
authored
Added ulid validation (#826)
1 parent ec2071b commit 8fe074c

31 files changed

+191
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ Baked-in Validations
189189
| uuid5 | Universally Unique Identifier UUID v5 |
190190
| uuid5_rfc4122 | Universally Unique Identifier UUID v5 RFC4122 |
191191
| uuid_rfc4122 | Universally Unique Identifier UUID RFC4122 |
192+
| ulid | Universally Unique Lexicographically Sortable Identifier ULID |
192193

193194
### Comparisons:
194195
| Tag | Description |

baked_in.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ var (
148148
"uuid3_rfc4122": isUUID3RFC4122,
149149
"uuid4_rfc4122": isUUID4RFC4122,
150150
"uuid5_rfc4122": isUUID5RFC4122,
151+
"ulid": isULID,
151152
"ascii": isASCII,
152153
"printascii": isPrintableASCII,
153154
"multibyte": hasMultiByteCharacter,
@@ -499,6 +500,11 @@ func isUUIDRFC4122(fl FieldLevel) bool {
499500
return uUIDRFC4122Regex.MatchString(fl.Field().String())
500501
}
501502

503+
// isULID is the validation function for validating if the field's value is a valid ULID.
504+
func isULID(fl FieldLevel) bool {
505+
return uLIDRegex.MatchString(fl.Field().String())
506+
}
507+
502508
// isISBN is the validation function for validating if the field's value is a valid v10 or v13 ISBN.
503509
func isISBN(fl FieldLevel) bool {
504510
return isISBN10(fl) || isISBN13(fl)

doc.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,12 @@ This validates that a string value contains a valid version 5 UUID. Uppercase U
10071007
10081008
Usage: uuid5
10091009
1010+
Universally Unique Lexicographically Sortable Identifier ULID
1011+
1012+
This validates that a string value contains a valid ULID value.
1013+
1014+
Usage: ulid
1015+
10101016
ASCII
10111017
10121018
This validates that a string value contains only ASCII characters.

regexes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const (
2929
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}$"
3030
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}$"
3131
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}$"
32+
uLIDRegexString = "^[A-HJKMNP-TV-Z0-9]{26}$"
3233
aSCIIRegexString = "^[\x00-\x7F]*$"
3334
printableASCIIRegexString = "^[\x20-\x7E]*$"
3435
multibyteRegexString = "[^\x00-\x7F]"
@@ -81,6 +82,7 @@ var (
8182
uUID4RFC4122Regex = regexp.MustCompile(uUID4RFC4122RegexString)
8283
uUID5RFC4122Regex = regexp.MustCompile(uUID5RFC4122RegexString)
8384
uUIDRFC4122Regex = regexp.MustCompile(uUIDRFC4122RegexString)
85+
uLIDRegex = regexp.MustCompile(uLIDRegexString)
8486
aSCIIRegex = regexp.MustCompile(aSCIIRegexString)
8587
printableASCIIRegex = regexp.MustCompile(printableASCIIRegexString)
8688
multibyteRegex = regexp.MustCompile(multibyteRegexString)

translations/en/en.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
11361136
translation: "{0} must be a valid version 5 UUID",
11371137
override: false,
11381138
},
1139+
{
1140+
tag: "ulid",
1141+
translation: "{0} must be a valid ULID",
1142+
override: false,
1143+
},
11391144
{
11401145
tag: "ascii",
11411146
translation: "{0} must contain only ascii characters",

translations/en/en_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ func TestTranslations(t *testing.T) {
103103
UUID3 string `validate:"uuid3"`
104104
UUID4 string `validate:"uuid4"`
105105
UUID5 string `validate:"uuid5"`
106+
ULID string `validate:"ulid"`
106107
ASCII string `validate:"ascii"`
107108
PrintableASCII string `validate:"printascii"`
108109
MultiByte string `validate:"multibyte"`
@@ -323,6 +324,10 @@ func TestTranslations(t *testing.T) {
323324
ns: "Test.UUID5",
324325
expected: "UUID5 must be a valid version 5 UUID",
325326
},
327+
{
328+
ns: "Test.ULID",
329+
expected: "ULID must be a valid ULID",
330+
},
326331
{
327332
ns: "Test.ISBN",
328333
expected: "ISBN must be a valid ISBN number",

translations/es/es.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
11781178
translation: "{0} debe ser una versión válida 5 UUID",
11791179
override: false,
11801180
},
1181+
{
1182+
tag: "ulid",
1183+
translation: "{0} debe ser un ULID válido",
1184+
override: false,
1185+
},
11811186
{
11821187
tag: "ascii",
11831188
translation: "{0} debe contener sólo caracteres ascii",

translations/es/es_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import (
44
"testing"
55
"time"
66

7+
. "github.com/go-playground/assert/v2"
78
spanish "github.com/go-playground/locales/es"
89
ut "github.com/go-playground/universal-translator"
9-
. "github.com/go-playground/assert/v2"
1010
"github.com/go-playground/validator/v10"
1111
)
1212

@@ -104,6 +104,7 @@ func TestTranslations(t *testing.T) {
104104
UUID3 string `validate:"uuid3"`
105105
UUID4 string `validate:"uuid4"`
106106
UUID5 string `validate:"uuid5"`
107+
ULID string `validate:"ulid"`
107108
ASCII string `validate:"ascii"`
108109
PrintableASCII string `validate:"printascii"`
109110
MultiByte string `validate:"multibyte"`
@@ -312,6 +313,10 @@ func TestTranslations(t *testing.T) {
312313
ns: "Test.UUID5",
313314
expected: "UUID5 debe ser una versión válida 5 UUID",
314315
},
316+
{
317+
ns: "Test.ULID",
318+
expected: "ULID debe ser un ULID válido",
319+
},
315320
{
316321
ns: "Test.ISBN",
317322
expected: "ISBN debe ser un número ISBN válido",

translations/fa/fa.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er
11361136
translation: "{0} باید یک UUID نسخه 5 معتبر باشد",
11371137
override: false,
11381138
},
1139+
{
1140+
tag: "ulid",
1141+
translation: "{0} باید یک ULID معتبر باشد",
1142+
override: false,
1143+
},
11391144
{
11401145
tag: "ascii",
11411146
translation: "{0} باید فقط شامل کاراکترهای اسکی باشد",

translations/fa/fa_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ func TestTranslations(t *testing.T) {
103103
UUID3 string `validate:"uuid3"`
104104
UUID4 string `validate:"uuid4"`
105105
UUID5 string `validate:"uuid5"`
106+
ULID string `validate:"ulid"`
106107
ASCII string `validate:"ascii"`
107108
PrintableASCII string `validate:"printascii"`
108109
MultiByte string `validate:"multibyte"`
@@ -322,6 +323,10 @@ func TestTranslations(t *testing.T) {
322323
ns: "Test.UUID5",
323324
expected: "UUID5 باید یک UUID نسخه 5 معتبر باشد",
324325
},
326+
{
327+
ns: "Test.ULID",
328+
expected: "ULID باید یک ULID معتبر باشد",
329+
},
325330
{
326331
ns: "Test.ISBN",
327332
expected: "ISBN باید یک شابک معتبر باشد",

0 commit comments

Comments
 (0)