From 269c02fbbb93122d88136478599b75df7e000f4b Mon Sep 17 00:00:00 2001 From: peefy Date: Tue, 7 Nov 2023 17:54:46 +0800 Subject: [PATCH] feat: publish the validator module Signed-off-by: peefy --- validator/README.md | 19 +++++++ validator/alpha/alpha.k | 108 ++++++++++++++++++++++++++++++++++++++++ validator/kcl.mod | 5 ++ validator/kcl.mod.lock | 0 validator/main.k | 3 ++ 5 files changed, 135 insertions(+) create mode 100644 validator/README.md create mode 100644 validator/alpha/alpha.k create mode 100644 validator/kcl.mod create mode 100644 validator/kcl.mod.lock create mode 100644 validator/main.k diff --git a/validator/README.md b/validator/README.md new file mode 100644 index 00000000..220eeb14 --- /dev/null +++ b/validator/README.md @@ -0,0 +1,19 @@ +## Introduction + +A library of string validators and sanitizers like [validator.js](https://github.com/validatorjs/validator.js) + +## How to Use + +```shell +kcl mod add validator +``` + +```python +import validator + +is_alpha_str = validator.is_alpha("ccc", locale='en-US', options=None) +``` + +## Resource + +The Code source and documents are [here](https://github.com/kcl-lang/artifacthub/tree/main/validator) diff --git a/validator/alpha/alpha.k b/validator/alpha/alpha.k new file mode 100644 index 00000000..62242fb3 --- /dev/null +++ b/validator/alpha/alpha.k @@ -0,0 +1,108 @@ +import regex + +englishLocales = ['AU', 'GB', 'HK', 'IN', 'NZ', 'ZA', 'ZM'] +arabicLocales = ['AE', 'BH', 'DZ', 'EG', 'IQ', 'JO', 'KW', 'LB', 'LY', 'MA', 'QM', 'QA', 'SA', 'SD', 'SY', 'TN', 'YE'] +farsiLocales = ['IR', 'AF'] +bengaliLocales = ['BD', 'IN'] +dotDecimal = ['ar-EG', 'ar-LB', 'ar-LY'] +commaDecimal = [ + 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-ZM', 'es-ES', 'fr-CA', 'fr-FR', + 'id-ID', 'it-IT', 'ku-IQ', 'hi-IN', 'hu-HU', 'nb-NO', 'nn-NO', 'nl-NL', 'pl-PL', 'pt-PT', + 'ru-RU', 'kk-KZ', 'si-LK', 'sl-SI', 'sr-RS@latin', 'sr-RS', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN', +] + +alpha = { + 'en-US': r"^[A-Za-z]+$", + 'az-AZ': r"^[A-VXYZÇƏĞİıÖŞÜ]+$", + 'bg-BG': r"^[А-Я]+$", + 'cs-CZ': r"^[A-Za-zÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$", + 'da-DK': r"^[A-Za-zÆØÅ]+$", + 'de-DE': r"^[A-Za-zÄÖÜß]+$", + 'el-GR': r"^[Α-ώ]+$", + 'es-ES': r"^[A-Za-zÁÉÍÑÓÚÜ]+$", + 'fa-IR': r"^[ابپتثجچحخدذرزژسشصضطظعغفقکگلمنوهی]+$", + 'fi-FI': r"^[A-Za-zÅÄÖ]+$", + 'fr-FR': r"^[A-Za-zÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$", + 'it-IT': r"^[A-Za-zÀÉÈÌÎÓÒÙ]+$", + 'ja-JP': r"^[ぁ-んァ-ヶヲ-゚一-龠ー・。、]+$", + 'nb-NO': r"^[A-Za-zÆØÅ]+$", + 'nl-NL': r"^[A-Za-zÁÉËÏÓÖÜÚ]+$", + 'nn-NO': r"^[A-Za-zÆØÅ]+$", + 'hu-HU': r"^[A-Za-zÁÉÍÓÖŐÚÜŰ]+$", + 'pl-PL': r"^[A-Za-zĄĆĘŚŁŃÓŻŹ]+$", + 'pt-PT': r"^[A-Za-zÃÁÀÂÄÇÉÊËÍÏÕÓÔÖÚÜ]+$", + 'ru-RU': r"^[А-ЯЁ]+$", + 'kk-KZ': r"^[А-ЯЁ\u04D8\u04B0\u0406\u04A2\u0492\u04AE\u049A\u04E8\u04BA]+$", + 'sl-SI': r"^[A-Za-zČĆĐŠŽ]+$", + 'sk-SK': r"^[A-Za-zÁČĎÉÍŇÓŠŤÚÝŽĹŔĽÄÔ]+$", + 'sr-RS@latin': r"^[A-Za-zČĆŽŠĐ]+$", + 'sr-RS': r"^[А-ЯЂЈЉЊЋЏ]+$", + 'sv-SE': r"^[A-Za-zÅÄÖ]+$", + 'th-TH': r"^[ก-๐\s]+$", + 'tr-TR': r"^[A-Za-zÇĞİıÖŞÜ]+$", + 'uk-UA': r"^[А-ЩЬЮЯЄIЇҐі]+$", + 'vi-VN': r"^[A-Za-zÀÁẠẢÃÂẦẤẬẨẪĂẰẮẶẲẴĐÈÉẸẺẼÊỀẾỆỂỄÌÍỊỈĨÒÓỌỎÕÔỒỐỘỔỖƠỜỚỢỞỠÙÚỤỦŨƯỪỨỰỬỮỲÝỴỶỸ]+$", + 'ko-KR': r"^[ㄱ-ㅎㅏ-ㅣ가-힣]*$" + 'ku-IQ': r"^[ئابپتجچحخدرڕزژسشعغفڤقکگلڵمنوۆھەیێيطؤثآإأكضصةظذ]+$", + ar: r"^[ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$" + he: r"^[א-ת]+$" + fa: r"^['آاءأؤئبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهةی']+$", + bn: r"^['ঀঁংঃঅআইঈউঊঋঌএঐওঔকখগঘঙচছজঝঞটঠডঢণতথদধনপফবভমযরলশষসহ়ঽািীুূৃৄেৈোৌ্ৎৗড়ঢ়য়ৠৡৢৣৰৱ৲৳৴৵৶৷৸৹৺৻']+$" + 'hi-IN': r"^[\u0900-\u0961]+[\u0972-\u097F]*$", + 'si-LK': r"^[\u0D80-\u0DFF]+$" +} | {"${e}": r"^[A-Za-z]+$", for e in englishLocales} + +alphanumeric = { + 'en-US': r"^[0-9A-Za-z]+$", + 'az-AZ': r"^[0-9A-VXYZÇƏĞİıÖŞÜ]+$", + 'bg-BG': r"^[0-9А-Я]+$", + 'cs-CZ': r"^[0-9A-Za-zÁČĎÉĚÍŇÓŘŠŤÚŮÝŽ]+$", + 'da-DK': r"^[0-9A-Za-zÆØÅ]+$", + 'de-DE': r"^[0-9A-Za-zÄÖÜß]+$", + 'el-GR': r"^[0-9Α-ω]+$", + 'es-ES': r"^[0-9A-Za-zÁÉÍÑÓÚÜ]+$", + 'fi-FI': r"^[0-9A-Za-zÅÄÖ]+$", + 'fr-FR': r"^[0-9A-Za-zÀÂÆÇÉÈÊËÏÎÔŒÙÛÜŸ]+$", + 'it-IT': r"^[0-9A-Za-zÀÉÈÌÎÓÒÙ]+$", + 'ja-JP': r"^[0-90-9ぁ-んァ-ヶヲ-゚一-龠ー・。、]+$", + 'hu-HU': r"^[0-9A-Za-zÁÉÍÓÖŐÚÜŰ]+$", + 'nb-NO': r"^[0-9A-Za-zÆØÅ]+$", + 'nl-NL': r"^[0-9A-Za-zÁÉËÏÓÖÜÚ]+$", + 'nn-NO': r"^[0-9A-Za-zÆØÅ]+$", + 'pl-PL': r"^[0-9A-Za-zĄĆĘŚŁŃÓŻŹ]+$", + 'pt-PT': r"^[0-9A-Za-zÃÁÀÂÄÇÉÊËÍÏÕÓÔÖÚÜ]+$", + 'ru-RU': r"^[0-9А-ЯЁ]+$", + 'kk-KZ': r"^[0-9А-ЯЁ\u04D8\u04B0\u0406\u04A2\u0492\u04AE\u049A\u04E8\u04BA]+$", + 'sl-SI': r"^[0-9A-Za-zČĆĐŠŽ]+$", + 'sk-SK': r"^[0-9A-Za-zÁČĎÉÍŇÓŠŤÚÝŽĹŔĽÄÔ]+$", + 'sr-RS@latin': r"^[0-9A-Za-zČĆŽŠĐ]+$", + 'sr-RS': r"^[0-9А-ЯЂЈЉЊЋЏ]+$", + 'sv-SE': r"^[0-9A-Za-zÅÄÖ]+$", + 'th-TH': r"^[ก-๙\s]+$", + 'tr-TR': r"^[0-9A-Za-zÇĞİıÖŞÜ]+$", + 'uk-UA': r"^[0-9А-ЩЬЮЯЄIЇҐі]+$", + 'ko-KR': r"^[0-9ㄱ-ㅎㅏ-ㅣ가-힣]*$" + 'ku-IQ': r"^[٠١٢٣٤٥٦٧٨٩0-9ئابپتجچحخدرڕزژسشعغفڤقکگلڵمنوۆھەیێيطؤثآإأكضصةظذ]+$", + 'vi-VN': r"^[0-9A-Za-zÀÁẠẢÃÂẦẤẬẨẪĂẰẮẶẲẴĐÈÉẸẺẼÊỀẾỆỂỄÌÍỊỈĨÒÓỌỎÕÔỒỐỘỔỖƠỜỚỢỞỠÙÚỤỦŨƯỪỨỰỬỮỲÝỴỶỸ]+$", + ar: r"^[٠١٢٣٤٥٦٧٨٩0-9ءآأؤإئابةتثجحخدذرزسشصضطظعغفقكلمنهوىيًٌٍَُِّْٰ]+$" + he: r"^[0-9א-ת]+$" + fa: r"^['0-9آاءأؤئبپتثجچحخدذرزژسشصضطظعغفقکگلمنوهةی۱۲۳۴۵۶۷۸۹۰']+$", + bn: r"^['ঀঁংঃঅআইঈউঊঋঌএঐওঔকখগঘঙচছজঝঞটঠডঢণতথদধনপফবভমযরলশষসহ়ঽািীুূৃৄেৈোৌ্ৎৗড়ঢ়য়ৠৡৢৣ০১২৩৪৫৬৭৮৯ৰৱ৲৳৴৵৶৷৸৹৺৻']+$" + 'hi-IN': r"^[\u0900-\u0963]+[\u0966-\u097F]*$", + 'si-LK': r"^[0-9\u0D80-\u0DFF]+$" +} | {"${e}": r"^[0-9A-Za-z]+$" for e in englishLocales} + +decimal = { + 'en-US': '.', + ar: '٫', +} | {"${e}": "" for e in englishLocales} + +is_alpha = lambda s: str, locale: str = 'en-US', options: {str:} = {} -> bool { + result = False + assert locale in alpha, "Invalid locale ${locale}" + # TODO: ignore options + ignore = options?.ignore + if s: + result = regex.match(s, alpha[locale]) + result +} diff --git a/validator/kcl.mod b/validator/kcl.mod new file mode 100644 index 00000000..e70e3ff6 --- /dev/null +++ b/validator/kcl.mod @@ -0,0 +1,5 @@ +[package] +name = "validator" +version = "0.1.0" +description = "A library of string validators and sanitizers." + diff --git a/validator/kcl.mod.lock b/validator/kcl.mod.lock new file mode 100644 index 00000000..e69de29b diff --git a/validator/main.k b/validator/main.k new file mode 100644 index 00000000..330c082d --- /dev/null +++ b/validator/main.k @@ -0,0 +1,3 @@ +import alpha + +is_alpha = alpha.is_alpha