Package that allows for validate and sanitize of string values.

Overview

py.validator

A library of string validators and sanitizers

Insipired by validator.js

Strings only

This library validates and sanitizes strings only.

Passing anything other than a string will result in an error.

Instalation

You can install it by running: pip install py.validator

Validators

Here is a list of the validators currently available.

Validator Description
is_after(str [, date]) check if the string is a date that's after the specified date (defaults to now).
is_alpha(str [, locale, options]) check if the string contains only letters (a-zA-Z).

Locale is one of ['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']) and defaults to en-US. Locale list is validator.isAlphaLocales. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.
is_alphanumeric(str [, locale, options]) check if the string contains only letters and numbers (a-zA-Z0-9).

Locale is one of ['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']) and defaults to en-US. Locale list is validator.isAlphanumericLocales. options is an optional object that can be supplied with the following key(s): ignore which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.
is_ascii(str) check if the string contains ASCII chars only.
is_base32(str) check if a string is base32 encoded.
is_base58(str) check if a string is base58 encoded.
is_base64(str [, options]) check if a string is base64 encoded. options is optional and defaults to {urlSafe: false}
when urlSafe is true it tests the given base64 encoded string is url safe
is_before(str [, date]) check if the string is a date that's before the specified date.
is_bic(str) check if a string is a BIC (Bank Identification Code) or SWIFT code.
is_boolean(str [, options]) check if a string is a boolean.
options is an object which defaults to { loose: false }. If loose is is set to false, the validator will strictly match ['true', 'false', '0', '1']. If loose is set to true, the validator will also match 'yes', 'no', and will match a valid boolean string of any case. (eg: ['true', 'True', 'TRUE']).
is_btc_address(str) check if the string is a valid BTC address.
is_byte_length(str [, options]) check if the string's length (in UTF-8 bytes) falls in a range.

options is an object which defaults to {min:0, max: undefined}.
is_credit_card(str) check if the string is a credit card.
is_currency(str [, options]) check if the string is a valid currency amount.

options is an object which defaults to {symbol: '$', require_symbol: false, allow_space_after_symbol: false, symbol_after_digits: false, allow_negatives: true, parens_for_negatives: false, negative_sign_before_digits: false, negative_sign_after_digits: false, allow_negative_sign_placeholder: false, thousands_separator: ',', decimal_separator: '.', allow_decimal: true, require_decimal: false, digits_after_decimal: [2], allow_space_after_digits: false}.
Note: The array digits_after_decimal is filled with the exact number of digits allowed not a range, for example a range 1 to 3 will be given as [1, 2, 3].
is_data_uri(str) check if the string is a data uri format.
is_date(str [, options]) Check if the input is a valid date. e.g. [2002-07-15, new Date()].

options is an object which can contain the keys format, strictMode and/or delimiters

format is a string and defaults to YYYY/MM/DD.

strictMode is a boolean and defaults to false. If strictMode is set to true, the validator will reject inputs different from format.

delimiters is an array of allowed date delimiters and defaults to ['/', '-'].
is_decimal(str [, options]) check if the string represents a decimal number, such as 0.1, .3, 1.1, 1.00003, 4.0, etc.

options is an object which defaults to {force_decimal: false, decimal_digits: '1,', locale: 'en-US'}

locale determine the decimal separator and is one of ['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fa', 'fa-AF', 'fa-IR', 'fr-FR', 'fr-CA', 'hu-HU', 'id-ID', 'it-IT', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pl-Pl', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA', 'vi-VN'].
Note: decimal_digits is given as a range like '1,3', a specific value like '3' or min like '1,'.
is_divisible_by(str, number) check if the string is a number that's divisible by another.
is_ean(str) check if the string is an EAN (European Article Number).
is_float(str [, options]) check if the string is a float.

options is an object which can contain the keys min, max, gt, and/or lt to validate the float is within boundaries (e.g. { min: 7.22, max: 9.55 }) it also has locale as an option.

min and max are equivalent to 'greater or equal' and 'less or equal', respectively while gt and lt are their strict counterparts.

locale determine the decimal separator and is one of ['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'es-ES', 'fr-CA', 'fr-FR', 'hu-HU', 'it-IT', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'sl-SI', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'tr-TR', 'uk-UA']. Locale list is validator.isFloatLocales.
is_hexadecimal(str) check if the string is a hexadecimal number.
is_int(str [, options]) check if the string is an integer.

options is an object which can contain the keys min and/or max to check the integer is within boundaries (e.g. { min: 10, max: 99 }). options can also contain the key allow_leading_zeroes, which when set to false will disallow integer values with leading zeroes (e.g. { allow_leading_zeroes: false }). Finally, options can contain the keys gt and/or lt which will enforce integers being greater than or less than, respectively, the value provided (e.g. {gt: 1, lt: 4} for a number between 1 and 4).
is_ip(str [, version]) check if the string is an IP (version [string
is_ISO31661_alpha2(str) check if the string is a valid ISO 3166-1 alpha-2 officially assigned country code.
is_isrc(str) check if the string is a ISRC.
is_jwt(str) check if the string is valid JWT token.
isLowercase(str) check if the string is lowercase.
is_md5(str) check if the string is a MD5 hash.

Please note that you can also use the isHash(str, 'md5') function. Keep in mind that MD5 has some collision weaknesses compared to other algorithms (e.g., SHA).
is_mongo_id(str) check if the string is a valid hex-encoded representation of a [MongoDB ObjectId][mongoid].
is_number(str [, options]) check if the string is a number (int or float)
is_octal(str) check if the string is a valid octal number.
is_passport_number(str, country_code) check if the string is a valid passport number.

(country_code is one of [ 'AM', 'AR', 'AT', 'AU', 'BA', 'BE', 'BG', 'BY', 'BR', 'CA', 'CH', 'CN', 'CY', 'CZ', 'DE', 'DK', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU', 'IE' 'IN', 'IR', 'ID', 'IS', 'IT', 'JP', 'KR', 'LT', 'LU', 'LV', 'LY', 'MT', 'MY', 'MZ', 'NL', 'PL', 'PT', 'RO', 'RS', 'RU', 'SE', 'SL', 'SK', 'TR', 'UA', 'US' ].
is_port(str) check if the string is a valid port number.
is_rgb_color(str [, include_percent_values]) check if the string is a rgb or rgba color.

includePercentValues defaults to true. If you don't want to allow to set rgb or rgba values with percents, like rgb(5%,5%,5%), or rgba(90%,90%,90%,.3), then set it to false.
is_semantic_version(str) check if the string is a Semantic Versioning Specification (SemVer).
is_uppercase(str) check if the string is uppercase.
is_slug(str) Check if the string is of type slug. Options allow a single hyphen between string. e.g. [cn-cn, cn-c-c]
is_uuid(str [, version]) check if the string is a UUID (version [string
Comments
  • `is_url` function failing when top level domain is longer than 6 chars.

    `is_url` function failing when top level domain is longer than 6 chars.

    Restricting the top level domain to a max of length of 6 chars fails when top level domain is longer. e.g.: 'www.testing.testing' //False

    due to this within is_url: top_level_domain_pattern = r"\.[a-z]{2,6}"

    Can we get the option to pass it as a param? or just make it somewhat longer?

    opened by mikeynagler 3
  • ModuleNotFoundError: No module named 'pyvalidator.utils'

    ModuleNotFoundError: No module named 'pyvalidator.utils'

    When trying to use the library, I get the following error:

    File "/usr/local/lib/python3.8/site-packages/pyvalidator/__init__.py", line 1, in <module>
        from pyvalidator.is_after import is_after
      File "/usr/local/lib/python3.8/site-packages/pyvalidator/is_after.py", line 1, in <module>
        from .utils.to_date import to_date, default_date_string
    ModuleNotFoundError: No module named 'pyvalidator.utils'
    

    Seems that utils package is not exist in the build

    opened by eyal666 3
  • chore: Updated Python version specification

    chore: Updated Python version specification

    The version specification only indicates that Python 3 is required. However, Python 3.7 is still supported, but TypedDict (first introduced in 3.8) is used in a number of the scripts.

    opened by deathbywedgie 1
  • Chore/test improvements

    Chore/test improvements

    • New function for standardizing printed output of tests
    • Cleanup, logic improvements, and standardization across all test
    • Some tests did not print the standard output printed by other tests
    • New test for utils.to_string.py
    opened by deathbywedgie 0
  • chore/numerous improvements

    chore/numerous improvements

    • various logic improvements
    • fixed various typos
    • fixed a potential bug with the to_str helper
    • fixed a potential bug with the "compile" method of the "RegEx" custom class
    • edited to comply with various PEP 8 best practices
    • removed some custom types that do not serve a purpose
    • improved some type hints
    • raise correct exceptions
    • split requirements.txt so that only true requirements for users to use the package are present, and moved all dev & test packages to requirements-dev.txt
    • set "raw" string flags for regex wherever backslashes are used
    opened by deathbywedgie 0
  • chore: Reformatting for readability and PEP 8 compliance

    chore: Reformatting for readability and PEP 8 compliance

    Maintenance/cleanup update Reformatting for readability and PEP 8 compliance: space separation, line break separation, mixed indentation, tab indentation, etc.

    opened by deathbywedgie 0
  • Support kwargs for validators

    Support kwargs for validators

    The idea would be to have the validator functions, which accept an dict options argument, to also support a variant of the methods that can accept options via kwargs.

    For example the is_email validator, accepts as a second argument an options dict, like in this case:

    is_email("[email protected]", { "allow_display_name": True })
    

    The idea would be to have this method also accept kwargs with the same option properties with a lower precedence over the dict argument.

    So the execution of the validator would end up something like this:

    is_email("[email protected]", allow_display_name=True)
    
    good first issue 
    opened by theteladras 0
Owner
Sanel Hadzini
Full Stack Engineer. TypeScript / React / React Native / Node.js / Express / Nestjs / Python / Flask / Docker / Kubernetes
Sanel Hadzini
A python module to validate input.

A python module to validate input.

Matthias 6 Sep 13, 2022
This repository contains scripts that help you validate QR codes.

Validation tools This repository contains scripts that help you validate QR codes. It's hacky, and a warning for Apple Silicon users: the dependencies

Ryan Barrett 8 Mar 1, 2022
A simple package for handling variables in string.

A simple package for handling string variables. Welcome! This is a simple package for handling variables in string, You can add or remove variables wi

null 1 Dec 31, 2021
ColorController is a Pythonic interface for managing colors by english-language name and various color values.

ColorController.py Table of Contents Encode color data in various formats. 1.1: Create a ColorController object using a familiar, english-language col

Tal Zaken 2 Feb 12, 2022
Python utility for discovering interesting CFPreferences values on iDevices

Description Simple utility to search for interesting preferences in iDevices. Installation python3 -m pip install -U --user cfprefsmon Example In this

null 12 Aug 19, 2022
Rabbito is a mini tool to find serialized objects in input values

Rabbito-ObjectFinder Rabbito is a mini tool to find serialized objects in input values What does Rabbito do Rabbito has the main object finding Serial

null 7 Dec 13, 2021
A small python tool to get relevant values from SRI invoices

SriInvoiceProcessing A small python tool to get relevant values from SRI invoices Some useful info to run the tool Login into your SRI account and ret

Wladymir Brborich 2 Jan 7, 2022
A python lib for generate random string and digits and special characters or A combination of them

A python lib for generate random string and digits and special characters or A combination of them

Torham 4 Nov 15, 2022
RapidFuzz is a fast string matching library for Python and C++

RapidFuzz is a fast string matching library for Python and C++, which is using the string similarity calculations from FuzzyWuzzy

Max Bachmann 1.7k Jan 4, 2023
glip is a module for retrieve ip address like local-ip, global-ip, external-ip as string.

gle_ip_info glip is a module for retrieve ip address like local-ip, global-ip, external-ip as string.

Fatin Shadab 3 Nov 21, 2021
Creates a C array from a hex-string or a stream of binary data.

hex2array-c Creates a C array from a hex-string. Usage Usage: python3 hex2array_c.py HEX_STRING [-h|--help] Use '-' to read the hex string from STDIN.

John Doe 3 Nov 24, 2022
A string extractor module for python

A string extractor module for python

Fayas Noushad 4 Jul 19, 2022
A hashtag from string extract python module

A hashtag from string extract python module

Fayas Noushad 3 Aug 10, 2022
A string to hashtags module

A string to hashtags module

Fayas Noushad 4 Dec 1, 2021
This is a package that allows you to create a key-value vault for storing variables in a global context

This is a package that allows you to create a key-value vault for storing variables in a global context. It allows you to set up a keyring with pre-defined constants which act as keys for the vault. These constants are then what is stored inside the vault. A key is just a string, but the value that the key is mapped to can be assigned to any type of object in Python. If the object is serializable (like a list or a dict), it can also be writen to a JSON file You can then use a decorator to annotate functions that you want to have use this vault to either store return variables in or to extract variables to be used as input for the function.

Data Ductus 2 Dec 14, 2022
Keval allows you to call arbitrary Windows kernel-mode functions from user mode, even (and primarily) on another machine.

Keval Keval allows you to call arbitrary Windows kernel-mode functions from user mode, even (and primarily) on another machine. The user mode portion

null 42 Dec 17, 2022
Utility to play with ADCS, allows to request tickets and collect information about related objects.

certi Utility to play with ADCS, allows to request tickets and collect information about related objects. Basically, it's the impacket copy of Certify

Eloy 185 Dec 29, 2022
A fixture that allows runtime xfail

pytest-runtime-xfail pytest plugin, providing a runtime_xfail fixture, which is callable as runtime_xfail(), to allow runtime decisions to mark a test

Brian Okken 4 Apr 6, 2022
Allows you to canibalize methods from classes effectively implementing trait-oriented programming

About This package enables code reuse in non-inheritance way from existing classes, effectively implementing traits-oriented programming pattern. Stor

null 1 Dec 13, 2021