diff --git a/README.md b/README.md index 8e80d52a5..700c995d3 100644 --- a/README.md +++ b/README.md @@ -178,6 +178,7 @@ validate := validator.New(validator.WithRequiredStructEnabled()) | isbn | International Standard Book Number | | isbn10 | International Standard Book Number 10 | | isbn13 | International Standard Book Number 13 | +| issn | International Standard Serial Number | | iso3166_1_alpha2 | Two-letter country code (ISO 3166-1 alpha-2) | | iso3166_1_alpha3 | Three-letter country code (ISO 3166-1 alpha-3) | | iso3166_1_alpha_numeric | Numeric country code (ISO 3166-1 numeric) | diff --git a/baked_in.go b/baked_in.go index 7d993aa51..95f56e008 100644 --- a/baked_in.go +++ b/baked_in.go @@ -150,6 +150,7 @@ var ( "isbn": isISBN, "isbn10": isISBN10, "isbn13": isISBN13, + "issn": isISSN, "eth_addr": isEthereumAddress, "eth_addr_checksum": isEthereumAddressChecksum, "btc_addr": isBitcoinAddress, @@ -651,6 +652,32 @@ func isISBN10(fl FieldLevel) bool { return checksum%11 == 0 } +// isISSN is the validation function for validating if the field's value is a valid ISSN. +func isISSN(fl FieldLevel) bool { + s := fl.Field().String() + + if !iSSNRegex.MatchString(s) { + return false + } + s = strings.ReplaceAll(s, "-", "") + + pos := 8 + checksum := 0 + + for i := 0; i < 7; i++ { + checksum += pos * int(s[i]-'0') + pos-- + } + + if s[7] == 'X' { + checksum += 10 + } else { + checksum += int(s[7] - '0') + } + + return checksum%11 == 0 +} + // isEthereumAddress is the validation function for validating if the field's value is a valid Ethereum address. func isEthereumAddress(fl FieldLevel) bool { address := fl.Field().String() diff --git a/regexes.go b/regexes.go index 6c8f98560..af98d8daa 100644 --- a/regexes.go +++ b/regexes.go @@ -22,6 +22,7 @@ const ( base64RawURLRegexString = "^(?:[A-Za-z0-9-_]{4})*(?:[A-Za-z0-9-_]{2,4})$" iSBN10RegexString = "^(?:[0-9]{9}X|[0-9]{10})$" iSBN13RegexString = "^(?:(?:97(?:8|9))[0-9]{10})$" + iSSNRegexString = "^(?:[0-9]{4}-[0-9]{3}[0-9X])$" uUID3RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-3[0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$" uUID4RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$" uUID5RegexString = "^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$" @@ -93,6 +94,7 @@ var ( base64RawURLRegex = regexp.MustCompile(base64RawURLRegexString) iSBN10Regex = regexp.MustCompile(iSBN10RegexString) iSBN13Regex = regexp.MustCompile(iSBN13RegexString) + iSSNRegex = regexp.MustCompile(iSSNRegexString) uUID3Regex = regexp.MustCompile(uUID3RegexString) uUID4Regex = regexp.MustCompile(uUID4RegexString) uUID5Regex = regexp.MustCompile(uUID5RegexString) diff --git a/translations/ar/ar.go b/translations/ar/ar.go index 038635a58..85ec5ea13 100644 --- a/translations/ar/ar.go +++ b/translations/ar/ar.go @@ -1116,6 +1116,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er translation: "يجب أن يكون {0} رقم ISBN-13 صالح", override: false, }, + { + tag: "issn", + translation: "يجب أن يكون {0} رقم ISSN صالح", + override: false, + }, { tag: "uuid", translation: "يجب أن يكون {0} UUID صالح", diff --git a/translations/ar/ar_test.go b/translations/ar/ar_test.go index 6e2a4184d..122739b37 100644 --- a/translations/ar/ar_test.go +++ b/translations/ar/ar_test.go @@ -99,6 +99,7 @@ func TestTranslations(t *testing.T) { ISBN string `validate:"isbn"` ISBN10 string `validate:"isbn10"` ISBN13 string `validate:"isbn13"` + ISSN string `validate:"issn"` UUID string `validate:"uuid"` UUID3 string `validate:"uuid3"` UUID4 string `validate:"uuid4"` @@ -341,6 +342,10 @@ func TestTranslations(t *testing.T) { ns: "Test.ISBN13", expected: "يجب أن يكون ISBN13 رقم ISBN-13 صالح", }, + { + ns: "Test.ISSN", + expected: "يجب أن يكون ISSN رقم ISSN صالح", + }, { ns: "Test.Excludes", expected: "لا يمكن أن يحتوي Excludes على النص 'text'", diff --git a/translations/en/en.go b/translations/en/en.go index 6cb6d7e0d..4c34dedbf 100644 --- a/translations/en/en.go +++ b/translations/en/en.go @@ -10,6 +10,7 @@ import ( "github.com/go-playground/locales" ut "github.com/go-playground/universal-translator" + "github.com/go-playground/validator/v10" ) @@ -1121,6 +1122,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er translation: "{0} must be a valid ISBN-13 number", override: false, }, + { + tag: "issn", + translation: "{0} must be a valid ISSN number", + override: false, + }, { tag: "uuid", translation: "{0} must be a valid UUID", diff --git a/translations/en/en_test.go b/translations/en/en_test.go index fb4486389..6782f3bd6 100644 --- a/translations/en/en_test.go +++ b/translations/en/en_test.go @@ -101,6 +101,7 @@ func TestTranslations(t *testing.T) { ISBN string `validate:"isbn"` ISBN10 string `validate:"isbn10"` ISBN13 string `validate:"isbn13"` + ISSN string `validate:"issn"` UUID string `validate:"uuid"` UUID3 string `validate:"uuid3"` UUID4 string `validate:"uuid4"` @@ -354,6 +355,10 @@ func TestTranslations(t *testing.T) { ns: "Test.ISBN13", expected: "ISBN13 must be a valid ISBN-13 number", }, + { + ns: "Test.ISSN", + expected: "ISSN must be a valid ISSN number", + }, { ns: "Test.Excludes", expected: "Excludes cannot contain the text 'text'", diff --git a/translations/es/es.go b/translations/es/es.go index 155e80f9e..0be3c5a12 100644 --- a/translations/es/es.go +++ b/translations/es/es.go @@ -1163,6 +1163,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er translation: "{0} debe ser un número ISBN-13 válido", override: false, }, + { + tag: "issn", + translation: "{0} debe ser un número ISSN válido", + override: false, + }, { tag: "uuid", translation: "{0} debe ser un UUID válido", diff --git a/translations/es/es_test.go b/translations/es/es_test.go index 4f986553e..557002b5a 100644 --- a/translations/es/es_test.go +++ b/translations/es/es_test.go @@ -101,6 +101,7 @@ func TestTranslations(t *testing.T) { ISBN string `validate:"isbn"` ISBN10 string `validate:"isbn10"` ISBN13 string `validate:"isbn13"` + ISSN string `validate:"issn"` UUID string `validate:"uuid"` UUID3 string `validate:"uuid3"` UUID4 string `validate:"uuid4"` @@ -331,6 +332,10 @@ func TestTranslations(t *testing.T) { ns: "Test.ISBN13", expected: "ISBN13 debe ser un número ISBN-13 válido", }, + { + ns: "Test.ISSN", + expected: "ISSN debe ser un número ISSN válido", + }, { ns: "Test.Excludes", expected: "Excludes no puede contener el texto 'text'", diff --git a/translations/fa/fa.go b/translations/fa/fa.go index 32fc69e9e..44cf32a1d 100644 --- a/translations/fa/fa.go +++ b/translations/fa/fa.go @@ -1116,6 +1116,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er translation: "{0} باید یک شابک(ISBN-13) معتبر باشد", override: false, }, + { + tag: "issn", + translation: "{0} باید یک شابک(ISSN) معتبر باشد", + override: false, + }, { tag: "uuid", translation: "{0} باید یک UUID معتبر باشد", diff --git a/translations/fa/fa_test.go b/translations/fa/fa_test.go index 456ebe46d..0886b4fe7 100644 --- a/translations/fa/fa_test.go +++ b/translations/fa/fa_test.go @@ -99,6 +99,7 @@ func TestTranslations(t *testing.T) { ISBN string `validate:"isbn"` ISBN10 string `validate:"isbn10"` ISBN13 string `validate:"isbn13"` + ISSN string `validate:"issn"` UUID string `validate:"uuid"` UUID3 string `validate:"uuid3"` UUID4 string `validate:"uuid4"` @@ -340,6 +341,10 @@ func TestTranslations(t *testing.T) { ns: "Test.ISBN13", expected: "ISBN13 باید یک شابک(ISBN-13) معتبر باشد", }, + { + ns: "Test.ISSN", + expected: "ISSN باید یک شابک(ISSN) معتبر باشد", + }, { ns: "Test.Excludes", expected: "Excludes نمیتواند شامل 'text' باشد", diff --git a/translations/fr/fr.go b/translations/fr/fr.go index eb40e8178..9dfafe5b4 100644 --- a/translations/fr/fr.go +++ b/translations/fr/fr.go @@ -1153,6 +1153,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er translation: "{0} doit être un numéro ISBN-13 valid", override: false, }, + { + tag: "issn", + translation: "{0} doit être un numéro ISSN valid", + override: false, + }, { tag: "uuid", translation: "{0} doit être un UUID valid", diff --git a/translations/fr/fr_test.go b/translations/fr/fr_test.go index 73a1849a1..497255a3d 100644 --- a/translations/fr/fr_test.go +++ b/translations/fr/fr_test.go @@ -100,6 +100,7 @@ func TestTranslations(t *testing.T) { ISBN string `validate:"isbn"` ISBN10 string `validate:"isbn10"` ISBN13 string `validate:"isbn13"` + ISSN string `validate:"issn"` UUID string `validate:"uuid"` UUID3 string `validate:"uuid3"` UUID4 string `validate:"uuid4"` @@ -324,6 +325,10 @@ func TestTranslations(t *testing.T) { ns: "Test.ISBN13", expected: "ISBN13 doit être un numéro ISBN-13 valid", }, + { + ns: "Test.ISSN", + expected: "ISSN doit être un numéro ISSN valid", + }, { ns: "Test.Excludes", expected: "Excludes ne doit pas contenir le texte 'text'", diff --git a/translations/id/id.go b/translations/id/id.go index f881a5395..96cd8fc16 100644 --- a/translations/id/id.go +++ b/translations/id/id.go @@ -1153,6 +1153,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er translation: "{0} harus berupa nomor ISBN-13 yang valid", override: false, }, + { + tag: "issn", + translation: "{0} harus berupa nomor ISSN yang valid", + override: false, + }, { tag: "uuid", translation: "{0} harus berupa UUID yang valid", diff --git a/translations/id/id_test.go b/translations/id/id_test.go index 6806e1028..4a5b70f7a 100644 --- a/translations/id/id_test.go +++ b/translations/id/id_test.go @@ -100,6 +100,7 @@ func TestTranslations(t *testing.T) { ISBN string `validate:"isbn"` ISBN10 string `validate:"isbn10"` ISBN13 string `validate:"isbn13"` + ISSN string `validate:"issn"` UUID string `validate:"uuid"` UUID3 string `validate:"uuid3"` UUID4 string `validate:"uuid4"` @@ -324,6 +325,10 @@ func TestTranslations(t *testing.T) { ns: "Test.ISBN13", expected: "ISBN13 harus berupa nomor ISBN-13 yang valid", }, + { + ns: "Test.ISSN", + expected: "ISSN harus berupa nomor ISSN yang valid", + }, { ns: "Test.Excludes", expected: "Excludes tidak boleh berisi teks 'text'", diff --git a/translations/it/it.go b/translations/it/it.go index 3f9030cb6..5c0a426fe 100644 --- a/translations/it/it.go +++ b/translations/it/it.go @@ -977,6 +977,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er translation: "{0} deve essere un numero ISBN-13 valido", override: false, }, + { + tag: "issn", + translation: "{0} deve essere un numero ISSN valido", + override: false, + }, { tag: "uuid", translation: "{0} deve essere un UUID valido", diff --git a/translations/it/it_test.go b/translations/it/it_test.go index 12fe78cef..bd449c540 100644 --- a/translations/it/it_test.go +++ b/translations/it/it_test.go @@ -99,6 +99,7 @@ func TestTranslations(t *testing.T) { ISBN string `validate:"isbn"` ISBN10 string `validate:"isbn10"` ISBN13 string `validate:"isbn13"` + ISSN string `validate:"issn"` UUID string `validate:"uuid"` UUID3 string `validate:"uuid3"` UUID4 string `validate:"uuid4"` @@ -351,6 +352,10 @@ func TestTranslations(t *testing.T) { ns: "Test.ISBN13", expected: "ISBN13 deve essere un numero ISBN-13 valido", }, + { + ns: "Test.ISSN", + expected: "ISSN deve essere un numero ISSN valido", + }, { ns: "Test.Excludes", expected: "Excludes non deve contenere il testo 'text'", diff --git a/translations/ja/ja.go b/translations/ja/ja.go index f106ff868..dea083ac6 100644 --- a/translations/ja/ja.go +++ b/translations/ja/ja.go @@ -1186,6 +1186,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er translation: "{0}は正しいISBN-13番号でなければなりません", override: false, }, + { + tag: "issn", + translation: "{0}は正しいISSN番号でなければなりません", + override: false, + }, { tag: "uuid", translation: "{0}は正しいUUIDでなければなりません", diff --git a/translations/ja/ja_test.go b/translations/ja/ja_test.go index d236e6817..afb507a21 100644 --- a/translations/ja/ja_test.go +++ b/translations/ja/ja_test.go @@ -101,6 +101,7 @@ func TestTranslations(t *testing.T) { ISBN string `validate:"isbn"` ISBN10 string `validate:"isbn10"` ISBN13 string `validate:"isbn13"` + ISSN string `validate:"issn"` UUID string `validate:"uuid"` UUID3 string `validate:"uuid3"` UUID4 string `validate:"uuid4"` @@ -347,6 +348,10 @@ func TestTranslations(t *testing.T) { ns: "Test.ISBN13", expected: "ISBN13は正しいISBN-13番号でなければなりません", }, + { + ns: "Test.ISSN", + expected: "ISSNは正しいISSN番号でなければなりません", + }, { ns: "Test.Excludes", expected: "Excludesには'text'というテキストを含むことはできません", diff --git a/translations/lv/lv.go b/translations/lv/lv.go index 0804ff381..ee64afa46 100644 --- a/translations/lv/lv.go +++ b/translations/lv/lv.go @@ -1121,6 +1121,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er translation: "{0} jābūt derīgam ISBN-13 numuram", override: false, }, + { + tag: "issn", + translation: "{0} jābūt derīgam ISSN numuram", + override: false, + }, { tag: "uuid", translation: "{0} jābūt derīgam UUID", diff --git a/translations/lv/lv_test.go b/translations/lv/lv_test.go index ca99784a0..99b0f9f25 100644 --- a/translations/lv/lv_test.go +++ b/translations/lv/lv_test.go @@ -101,6 +101,7 @@ func TestTranslations(t *testing.T) { ISBN string `validate:"isbn"` ISBN10 string `validate:"isbn10"` ISBN13 string `validate:"isbn13"` + ISSN string `validate:"issn"` UUID string `validate:"uuid"` UUID3 string `validate:"uuid3"` UUID4 string `validate:"uuid4"` @@ -346,6 +347,10 @@ func TestTranslations(t *testing.T) { ns: "Test.ISBN13", expected: "ISBN13 jābūt derīgam ISBN-13 numuram", }, + { + ns: "Test.ISSN", + expected: "ISSN jābūt derīgam ISSN numuram", + }, { ns: "Test.Excludes", expected: "Excludes nedrīkst saturēt tekstu 'text'", diff --git a/translations/nl/nl.go b/translations/nl/nl.go index a49f185e3..43b3067da 100644 --- a/translations/nl/nl.go +++ b/translations/nl/nl.go @@ -1153,6 +1153,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er translation: "{0} moet een geldig ISBN-13 nummer zijn", override: false, }, + { + tag: "issn", + translation: "{0} moet een geldig ISSN nummer zijn", + override: false, + }, { tag: "uuid", translation: "{0} moet een geldige UUID zijn", diff --git a/translations/nl/nl_test.go b/translations/nl/nl_test.go index 9cfeb5b57..e8f90e27a 100644 --- a/translations/nl/nl_test.go +++ b/translations/nl/nl_test.go @@ -100,6 +100,7 @@ func TestTranslations(t *testing.T) { ISBN string `validate:"isbn"` ISBN10 string `validate:"isbn10"` ISBN13 string `validate:"isbn13"` + ISSN string `validate:"issn"` UUID string `validate:"uuid"` UUID3 string `validate:"uuid3"` UUID4 string `validate:"uuid4"` @@ -324,6 +325,10 @@ func TestTranslations(t *testing.T) { ns: "Test.ISBN13", expected: "ISBN13 moet een geldig ISBN-13 nummer zijn", }, + { + ns: "Test.ISSN", + expected: "ISSN moet een geldig ISSN nummer zijn", + }, { ns: "Test.Excludes", expected: "Excludes mag niet de tekst 'text' bevatten", diff --git a/translations/pt/pt.go b/translations/pt/pt.go index 31fb1a244..8dca56228 100644 --- a/translations/pt/pt.go +++ b/translations/pt/pt.go @@ -1158,6 +1158,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er translation: "{0} deve ser um número ISBN-13 válido", override: false, }, + { + tag: "issn", + translation: "{0} deve ser um número ISSN válido", + override: false, + }, { tag: "uuid", translation: "{0} deve ser um UUID válido", diff --git a/translations/pt/pt_test.go b/translations/pt/pt_test.go index 96c831898..4620d2582 100644 --- a/translations/pt/pt_test.go +++ b/translations/pt/pt_test.go @@ -101,6 +101,7 @@ func TestTranslations(t *testing.T) { ISBN string `validate:"isbn"` ISBN10 string `validate:"isbn10"` ISBN13 string `validate:"isbn13"` + ISSN string `validate:"issn"` UUID string `validate:"uuid"` UUID3 string `validate:"uuid3"` UUID4 string `validate:"uuid4"` @@ -339,6 +340,10 @@ func TestTranslations(t *testing.T) { ns: "Test.ISBN13", expected: "ISBN13 deve ser um número ISBN-13 válido", }, + { + ns: "Test.ISSN", + expected: "ISSN deve ser um número ISSN válido", + }, { ns: "Test.Excludes", expected: "Excludes não deve conter o texto 'text'", diff --git a/translations/pt_BR/pt_BR.go b/translations/pt_BR/pt_BR.go index e22aeae18..079ec5e1a 100644 --- a/translations/pt_BR/pt_BR.go +++ b/translations/pt_BR/pt_BR.go @@ -1153,6 +1153,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er translation: "{0} deve ser um número ISBN-13 válido", override: false, }, + { + tag: "issn", + translation: "{0} deve ser um número ISSN válido", + override: false, + }, { tag: "uuid", translation: "{0} deve ser um UUID válido", diff --git a/translations/pt_BR/pt_BR_test.go b/translations/pt_BR/pt_BR_test.go index a0384cf64..7e5565a3b 100644 --- a/translations/pt_BR/pt_BR_test.go +++ b/translations/pt_BR/pt_BR_test.go @@ -100,6 +100,7 @@ func TestTranslations(t *testing.T) { ISBN string `validate:"isbn"` ISBN10 string `validate:"isbn10"` ISBN13 string `validate:"isbn13"` + ISSN string `validate:"issn"` UUID string `validate:"uuid"` UUID3 string `validate:"uuid3"` UUID4 string `validate:"uuid4"` @@ -328,6 +329,10 @@ func TestTranslations(t *testing.T) { ns: "Test.ISBN13", expected: "ISBN13 deve ser um número ISBN-13 válido", }, + { + ns: "Test.ISSN", + expected: "ISSN deve ser um número ISSN válido", + }, { ns: "Test.Excludes", expected: "Excludes não deve conter o texto 'text'", diff --git a/translations/ru/ru.go b/translations/ru/ru.go index 5cafb08f9..c34ad2899 100644 --- a/translations/ru/ru.go +++ b/translations/ru/ru.go @@ -1271,6 +1271,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er translation: "{0} должен быть ISBN-13 номером", override: false, }, + { + tag: "issn", + translation: "{0} должен быть ISSN номером", + override: false, + }, { tag: "uuid", translation: "{0} должен быть UUID", diff --git a/translations/ru/ru_test.go b/translations/ru/ru_test.go index 223966f17..c0ff6f02c 100644 --- a/translations/ru/ru_test.go +++ b/translations/ru/ru_test.go @@ -116,6 +116,7 @@ func TestTranslations(t *testing.T) { ISBN string `validate:"isbn"` ISBN10 string `validate:"isbn10"` ISBN13 string `validate:"isbn13"` + ISSN string `validate:"issn"` UUID string `validate:"uuid"` UUID3 string `validate:"uuid3"` UUID4 string `validate:"uuid4"` @@ -358,6 +359,10 @@ func TestTranslations(t *testing.T) { ns: "Test.ISBN13", expected: "ISBN13 должен быть ISBN-13 номером", }, + { + ns: "Test.ISSN", + expected: "ISSN должен быть ISSN номером", + }, { ns: "Test.Excludes", expected: "Excludes не должен содержать текст 'text'", diff --git a/translations/tr/tr.go b/translations/tr/tr.go index 7c86258af..4edcfb074 100644 --- a/translations/tr/tr.go +++ b/translations/tr/tr.go @@ -1153,6 +1153,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er translation: "{0} geçerli bir ISBN-13 numarası olmalıdır", override: false, }, + { + tag: "issn", + translation: "{0} geçerli bir ISSN numarası olmalıdır", + override: false, + }, { tag: "uuid", translation: "{0} geçerli bir UUID olmalıdır", diff --git a/translations/tr/tr_test.go b/translations/tr/tr_test.go index e18ac6a41..f21e32bd3 100644 --- a/translations/tr/tr_test.go +++ b/translations/tr/tr_test.go @@ -100,6 +100,7 @@ func TestTranslations(t *testing.T) { ISBN string `validate:"isbn"` ISBN10 string `validate:"isbn10"` ISBN13 string `validate:"isbn13"` + ISSN string `validate:"issn"` UUID string `validate:"uuid"` UUID3 string `validate:"uuid3"` UUID4 string `validate:"uuid4"` @@ -330,6 +331,10 @@ func TestTranslations(t *testing.T) { ns: "Test.ISBN13", expected: "ISBN13 geçerli bir ISBN-13 numarası olmalıdır", }, + { + ns: "Test.ISSN", + expected: "ISSN geçerli bir ISSN numarası olmalıdır", + }, { ns: "Test.Excludes", expected: "Excludes, 'text' metnini içeremez", diff --git a/translations/vi/vi.go b/translations/vi/vi.go index 170938db0..0b11407ac 100644 --- a/translations/vi/vi.go +++ b/translations/vi/vi.go @@ -1116,6 +1116,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er translation: "{0} phải là số ISBN-13", override: false, }, + { + tag: "issn", + translation: "{0} phải là số ISSN", + override: false, + }, { tag: "uuid", translation: "{0} phải là giá trị UUID", diff --git a/translations/vi/vi_test.go b/translations/vi/vi_test.go index d26c42dd2..a767fc319 100644 --- a/translations/vi/vi_test.go +++ b/translations/vi/vi_test.go @@ -99,6 +99,7 @@ func TestTranslations(t *testing.T) { ISBN string `validate:"isbn"` ISBN10 string `validate:"isbn10"` ISBN13 string `validate:"isbn13"` + ISSN string `validate:"issn"` UUID string `validate:"uuid"` UUID3 string `validate:"uuid3"` UUID4 string `validate:"uuid4"` @@ -336,6 +337,10 @@ func TestTranslations(t *testing.T) { ns: "Test.ISBN13", expected: "ISBN13 phải là số ISBN-13", }, + { + ns: "Test.ISSN", + expected: "ISSN phải là số ISSN", + }, { ns: "Test.Excludes", expected: "Excludes không được chứa chuỗi 'text'", diff --git a/translations/zh/zh.go b/translations/zh/zh.go index 7f45681f4..0b084e55a 100644 --- a/translations/zh/zh.go +++ b/translations/zh/zh.go @@ -1235,6 +1235,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er translation: "{0}必须是一个有效的ISBN-13编号", override: false, }, + { + tag: "issn", + translation: "{0}必须是一个有效的ISSN编号", + override: false, + }, { tag: "uuid", translation: "{0}必须是一个有效的UUID", diff --git a/translations/zh/zh_test.go b/translations/zh/zh_test.go index aaf512d5e..2b8ee050c 100644 --- a/translations/zh/zh_test.go +++ b/translations/zh/zh_test.go @@ -105,6 +105,7 @@ func TestTranslations(t *testing.T) { ISBN string `validate:"isbn"` ISBN10 string `validate:"isbn10"` ISBN13 string `validate:"isbn13"` + ISSN string `validate:"issn"` UUID string `validate:"uuid"` UUID3 string `validate:"uuid3"` UUID4 string `validate:"uuid4"` @@ -345,6 +346,10 @@ func TestTranslations(t *testing.T) { ns: "Test.ISBN13", expected: "ISBN13必须是一个有效的ISBN-13编号", }, + { + ns: "Test.ISSN", + expected: "ISSN必须是一个有效的ISSN编号", + }, { ns: "Test.EndsWith", expected: "EndsWith必须以文本'end'结尾", diff --git a/translations/zh_tw/zh_tw.go b/translations/zh_tw/zh_tw.go index 40cbec228..e465eb63c 100644 --- a/translations/zh_tw/zh_tw.go +++ b/translations/zh_tw/zh_tw.go @@ -1146,6 +1146,11 @@ func RegisterDefaultTranslations(v *validator.Validate, trans ut.Translator) (er translation: "{0}必須是一個有效的ISBN-13編號", override: false, }, + { + tag: "issn", + translation: "{0}必須是一個有效的ISSN編號", + override: false, + }, { tag: "uuid", translation: "{0}必須是一個有效的UUID", diff --git a/translations/zh_tw/zh_tw_test.go b/translations/zh_tw/zh_tw_test.go index f15f9f29e..e106363f1 100644 --- a/translations/zh_tw/zh_tw_test.go +++ b/translations/zh_tw/zh_tw_test.go @@ -100,6 +100,7 @@ func TestTranslations(t *testing.T) { ISBN string `validate:"isbn"` ISBN10 string `validate:"isbn10"` ISBN13 string `validate:"isbn13"` + ISSN string `validate:"issn"` UUID string `validate:"uuid"` UUID3 string `validate:"uuid3"` UUID4 string `validate:"uuid4"` @@ -327,6 +328,10 @@ func TestTranslations(t *testing.T) { ns: "Test.ISBN13", expected: "ISBN13必須是一個有效的ISBN-13編號", }, + { + ns: "Test.ISSN", + expected: "ISSN必須是一個有效的ISSN編號", + }, { ns: "Test.Excludes", expected: "Excludes不能包含文字'text'", diff --git a/validator_test.go b/validator_test.go index 4a08d011a..2826ae70e 100644 --- a/validator_test.go +++ b/validator_test.go @@ -4849,6 +4849,42 @@ func TestISBN10Validation(t *testing.T) { } } +func TestISSNValidation(t *testing.T) { + tests := []struct { + param string + expected bool + }{ + {"", false}, + {"foo", false}, + {"20519990", false}, + {"2051-9991", false}, + {"2051-999X", false}, + {"1050-124X", true}, + {"0317-8471", true}, + } + + validate := New() + + for i, test := range tests { + errs := validate.Var(test.param, "issn") + + if test.expected { + if !IsEqual(errs, nil) { + t.Fatalf("Index: %d ISSN failed Error: %s", i, errs) + } + } else { + if IsEqual(errs, nil) { + t.Fatalf("Index: %d ISSN failed Error: %s", i, errs) + } else { + val := getError(errs, "", "") + if val.Tag() != "issn" { + t.Fatalf("Index: %d ISSN failed Error: %s", i, errs) + } + } + } + } +} + func TestExcludesRuneValidation(t *testing.T) { tests := []struct { Value string `validate:"excludesrune=☻"`