validators
Validate Anything!
Validate that a number is between minimum and/or maximum value.
This will work with any comparable type, such as floats, decimals and dates not just integers. This validator is originally based on WTForms-NumberRange-Validator.
Examples:
>>> from datetime import datetime >>> between(5, min_val=2) True >>> between(13.2, min_val=13, max_val=14) True >>> between(500, max_val=400) ValidationError(func=between, args={'value': 500, 'max_val': 400}) >>> between( ... datetime(2000, 11, 11), ... min_val=datetime(1999, 11, 11) ... ) True
Arguments:
- value: Value which is to be compared.
- min_val: The minimum required value of the number. If not provided, minimum value will not be checked.
- max_val: The maximum value of the number. If not provided, maximum value will not be checked.
Returns:
(Literal[True]): If
value
is in between the given conditions. (ValidationError): Ifvalue
is not in between the given conditions.
Raises:
- (ValueError): If
min_val
is greater thanmax_val
. - (TypeError): If there's a type mismatch during comparison.
Note:
PossibleValueTypes
=TypeVar("PossibleValueTypes", int, float, str, datetime)
- If neither
min_val
normax_val
is provided, result will always beTrue
.
Return whether or not given value is a valid binance smart chain address.
Full validation is implemented for BSC addresses.
Examples:
>>> bsc_address('0x4e5acf9684652BEa56F2f01b7101a225Ee33d23f') True >>> bsc_address('0x4g5acf9684652BEa56F2f01b7101a225Eh33d23z') ValidationError(func=bsc_address, args={'value': '0x4g5acf9684652BEa56F2f01b7101a225Eh33d23z'})
Arguments:
- value: BSC address string to validate.
Returns:
(Literal[True]): If
value
is a valid bsc address. (ValidationError): Ifvalue
is an invalid bsc address.
Return whether or not given value is a valid bitcoin address.
Full validation is implemented for P2PKH and P2SH addresses. For segwit addresses a regexp is used to provide a reasonable estimate on whether the address is valid.
Examples:
>>> btc_address('3Cwgr2g7vsi1bXDUkpEnVoRLA9w4FZfC69') True >>> btc_address('1BvBMsEYstWetqTFn5Au4m4GFg7xJaNVN2') ValidationError(func=btc_address, args={'value': '1BvBMsEYstWetqTFn5Au4m4GFg7xJaNVN2'})
Arguments:
- value: Bitcoin address string to validate.
Returns:
(Literal[True]): If
value
is a valid bitcoin address. (ValidationError): Ifvalue
is an invalid bitcoin address.
Return whether or not given value is a valid ethereum address.
Full validation is implemented for ERC20 addresses.
Examples:
>>> eth_address('0x9cc14ba4f9f68ca159ea4ebf2c292a808aaeb598') True >>> eth_address('0x8Ba1f109551bD432803012645Ac136ddd64DBa72') ValidationError(func=eth_address, args={'value': '0x8Ba1f109551bD432803012645Ac136ddd64DBa72'})
Arguments:
- value: Ethereum address string to validate.
Returns:
(Literal[True]): If
value
is a valid ethereum address. (ValidationError): Ifvalue
is an invalid ethereum address.
Return whether or not given value is a valid tron address.
Full validation is implemented for TRC20 tron addresses.
Examples:
>>> trx_address('TLjfbTbpZYDQ4EoA4N5CLNgGjfbF8ZWz38') True >>> trx_address('TR2G7Rm4vFqF8EpY4U5xdLdQ7XgJ2U8Vd') ValidationError(func=trx_address, args={'value': 'TR2G7Rm4vFqF8EpY4U5xdLdQ7XgJ2U8Vd'})
Arguments:
- value: Tron address string to validate.
Returns:
(Literal[True]): If
value
is a valid tron address. (ValidationError): Ifvalue
is an invalid tron address.
Return whether or not given value is a valid American Express card number.
Examples:
>>> amex('378282246310005') True >>> amex('4242424242424242') ValidationError(func=amex, args={'value': '4242424242424242'})
Arguments:
- value: American Express card number string to validate
Returns:
(Literal[True]): If
value
is a valid American Express card number. (ValidationError): Ifvalue
is an invalid American Express card number.
Return whether or not given value is a valid generic card number.
This validator is based on Luhn's algorithm.
Examples:
>>> card_number('4242424242424242') True >>> card_number('4242424242424241') ValidationError(func=card_number, args={'value': '4242424242424241'})
Arguments:
- value: Generic card number string to validate
Returns:
(Literal[True]): If
value
is a valid generic card number. (ValidationError): Ifvalue
is an invalid generic card number.
Return whether or not given value is a valid Diners Club card number.
Examples:
>>> diners('3056930009020004') True >>> diners('4242424242424242') ValidationError(func=diners, args={'value': '4242424242424242'})
Arguments:
- value: Diners Club card number string to validate
Returns:
(Literal[True]): If
value
is a valid Diners Club card number. (ValidationError): Ifvalue
is an invalid Diners Club card number.
Return whether or not given value is a valid Discover card number.
Examples:
>>> discover('6011111111111117') True >>> discover('4242424242424242') ValidationError(func=discover, args={'value': '4242424242424242'})
Arguments:
- value: Discover card number string to validate
Returns:
(Literal[True]): If
value
is a valid Discover card number. (ValidationError): Ifvalue
is an invalid Discover card number.
Return whether or not given value is a valid JCB card number.
Examples:
>>> jcb('3566002020360505') True >>> jcb('4242424242424242') ValidationError(func=jcb, args={'value': '4242424242424242'})
Arguments:
- value: JCB card number string to validate
Returns:
(Literal[True]): If
value
is a valid JCB card number. (ValidationError): Ifvalue
is an invalid JCB card number.
Return whether or not given value is a valid Mastercard card number.
Examples:
>>> mastercard('5555555555554444') True >>> mastercard('4242424242424242') ValidationError(func=mastercard, args={'value': '4242424242424242'})
Arguments:
- value: Mastercard card number string to validate
Returns:
(Literal[True]): If
value
is a valid Mastercard card number. (ValidationError): Ifvalue
is an invalid Mastercard card number.
Return whether or not given value is a valid UnionPay card number.
Examples:
>>> unionpay('6200000000000005') True >>> unionpay('4242424242424242') ValidationError(func=unionpay, args={'value': '4242424242424242'})
Arguments:
- value: UnionPay card number string to validate
Returns:
(Literal[True]): If
value
is a valid UnionPay card number. (ValidationError): Ifvalue
is an invalid UnionPay card number.
Return whether or not given value is a valid Visa card number.
Examples:
>>> visa('4242424242424242') True >>> visa('2223003122003222') ValidationError(func=visa, args={'value': '2223003122003222'})
Arguments:
- value: Visa card number string to validate
Returns:
(Literal[True]): If
value
is a valid Visa card number. (ValidationError): Ifvalue
is an invalid Visa card number.
Return whether or not given value is a valid Mir card number.
Examples:
>>> mir('2200123456789019') True >>> mir('4242424242424242') ValidationError(func=mir, args={'value': '4242424242424242'})
Arguments:
- value: Mir card number string to validate.
Returns:
(Literal[True]): If
value
is a valid Mir card number. (ValidationError): Ifvalue
is an invalid Mir card number.
Validates given calling code.
This performs country's calling code validation.
Examples:
>>> calling_code('+91') True >>> calling_code('-31') ValidationError(func=calling_code, args={'value': '-31'})
Arguments:
- value: Country's calling code string to validate.
Returns:
(Literal[True]): If
value
is a valid calling code. (ValidationError): Ifvalue
is an invalid calling code.
Validates given country code.
This performs a case-sensitive ISO 3166 country code validation.
Examples:
>>> country_code('GB', iso_format='alpha3') ValidationError(func=country_code, args={'value': 'GB', 'iso_format': 'alpha3'}) >>> country_code('USA') True >>> country_code('840', iso_format='numeric') True >>> country_code('iN', iso_format='alpha2') ValidationError(func=country_code, args={'value': 'iN', 'iso_format': 'alpha2'}) >>> country_code('ZWE', iso_format='alpha3') True
Arguments:
- value: Country code string to validate.
- iso_format: ISO format to be used. Available options are:
auto
,alpha2
,alpha3
andnumeric
. - ignore_case: Enable/Disable case-sensitive matching.
Returns:
(Literal[True]): If
value
is a valid country code. (ValidationError): Ifvalue
is an invalid country code.
Validates given currency code.
This performs ISO 4217 currency code/symbol validation.
Examples:
>>> currency('USD') True >>> currency('ZWX') ValidationError(func=currency, args={'value': 'ZWX'})
Arguments:
- value: Currency code/symbol string to validate.
- skip_symbols: Skip currency symbol validation.
- ignore_case: Enable/Disable case-sensitive matching.
Returns:
(Literal[True]): If
value
is a valid currency code. (ValidationError): Ifvalue
is an invalid currency code.
Return whether or not given value is a valid cron string.
Examples:
>>> cron('*/5 * * * *') True >>> cron('30-20 * * * *') ValidationError(func=cron, args={'value': '30-20 * * * *'})
Arguments:
- value: Cron string to validate.
Returns:
(Literal[True]): If
value
is a valid cron string. (ValidationError): Ifvalue
is an invalid cron string.
Return whether or not given value is a valid domain.
Examples:
>>> domain('example.com') True >>> domain('example.com/') ValidationError(func=domain, args={'value': 'example.com/'}) >>> # Supports IDN domains as well:: >>> domain('xn----gtbspbbmkef.xn--p1ai') True
Arguments:
- value: Domain string to validate.
- consider_tld: Restrict domain to TLDs allowed by IANA.
- rfc_1034: Allows optional trailing dot in the domain name. Ref: RFC 1034.
- rfc_2782: Domain name is of type service record. Allows optional underscores in the domain name. Ref: RFC 2782.
Returns:
(Literal[True]): If
value
is a valid domain name. (ValidationError): Ifvalue
is an invalid domain name.
Raises:
- (UnicodeError): If
value
cannot be encoded intoidna
or decoded intoutf-8
.
Validate an email address.
This was inspired from Django's email validator. Also ref: RFC 1034, RFC 5321 and RFC 5322.
Examples:
>>> email('someone@example.com') True >>> email('bogus@@') ValidationError(func=email, args={'value': 'bogus@@'})
Arguments:
- value: eMail string to validate.
- ipv6_address: When the domain part is an IPv6 address.
- ipv4_address: When the domain part is an IPv4 address.
- simple_host: When the domain part is a simple hostname.
- rfc_1034: Allow trailing dot in domain name. Ref: RFC 1034.
- rfc_2782: Domain name is of type service record. Ref: RFC 2782.
Returns:
(Literal[True]): If
value
is a valid eMail. (ValidationError): Ifvalue
is an invalid eMail.
Return whether or not given value is a valid base16 encoding.
Examples:
>>> base16('a3f4b2') True >>> base16('a3f4Z1') ValidationError(func=base16, args={'value': 'a3f4Z1'})
Arguments:
- value: base16 string to validate.
Returns:
(Literal[True]): If
value
is a valid base16 encoding. (ValidationError): Ifvalue
is an invalid base16 encoding.
Return whether or not given value is a valid base32 encoding.
Examples:
>>> base32('MFZWIZLTOQ======') True >>> base32('MfZW3zLT9Q======') ValidationError(func=base32, args={'value': 'MfZW3zLT9Q======'})
Arguments:
- value: base32 string to validate.
Returns:
(Literal[True]): If
value
is a valid base32 encoding. (ValidationError): Ifvalue
is an invalid base32 encoding.
Return whether or not given value is a valid base58 encoding.
Examples:
>>> base58('14pq6y9H2DLGahPsM4s7ugsNSD2uxpHsJx') True >>> base58('cUSECm5YzcXJwP') True
Arguments:
- value: base58 string to validate.
Returns:
(Literal[True]): If
value
is a valid base58 encoding. (ValidationError): Ifvalue
is an invalid base58 encoding.
Return whether or not given value is a valid base64 encoding.
Examples:
>>> base64('Y2hhcmFjdGVyIHNldA==') True >>> base64('cUSECm5YzcXJwP') ValidationError(func=base64, args={'value': 'cUSECm5YzcXJwP'})
Arguments:
- value: base64 string to validate.
Returns:
(Literal[True]): If
value
is a valid base64 encoding. (ValidationError): Ifvalue
is an invalid base64 encoding.
Return whether or not given value is a valid CUSIP.
Checks if the value is a valid CUSIP.
Examples:
>>> cusip('037833DP2') True >>> cusip('037833DP3') ValidationError(func=cusip, args={'value': '037833DP3'})
Arguments:
- value: CUSIP string to validate.
Returns:
(Literal[True]): If
value
is a valid CUSIP string. (ValidationError): Ifvalue
is an invalid CUSIP string.
Return whether or not given value is a valid ISIN.
Checks if the value is a valid ISIN.
Examples:
>>> isin('037833DP2') ValidationError(func=isin, args={'value': '037833DP2'}) >>> isin('037833DP3') ValidationError(func=isin, args={'value': '037833DP3'})
Arguments:
- value: ISIN string to validate.
Returns:
(Literal[True]): If
value
is a valid ISIN string. (ValidationError): Ifvalue
is an invalid ISIN string.
Return whether or not given value is a valid SEDOL.
Checks if the value is a valid SEDOL.
Examples:
>>> sedol('2936921') True >>> sedol('29A6922') ValidationError(func=sedol, args={'value': '29A6922'})
Arguments:
- value: SEDOL string to validate.
Returns:
(Literal[True]): If
value
is a valid SEDOL string. (ValidationError): Ifvalue
is an invalid SEDOL string.
Return whether or not given value is a valid MD5 hash.
Examples:
>>> md5('d41d8cd98f00b204e9800998ecf8427e') True >>> md5('900zz11') ValidationError(func=md5, args={'value': '900zz11'})
Arguments:
- value: MD5 string to validate.
Returns:
(Literal[True]): If
value
is a valid MD5 hash. (ValidationError): Ifvalue
is an invalid MD5 hash.
Return whether or not given value is a valid SHA1 hash.
Examples:
>>> sha1('da39a3ee5e6b4b0d3255bfef95601890afd80709') True >>> sha1('900zz11') ValidationError(func=sha1, args={'value': '900zz11'})
Arguments:
- value: SHA1 string to validate.
Returns:
(Literal[True]): If
value
is a valid SHA1 hash. (ValidationError): Ifvalue
is an invalid SHA1 hash.
Return whether or not given value is a valid SHA224 hash.
Examples:
>>> sha224('d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f') True >>> sha224('900zz11') ValidationError(func=sha224, args={'value': '900zz11'})
Arguments:
- value: SHA224 string to validate.
Returns:
(Literal[True]): If
value
is a valid SHA224 hash. (ValidationError): Ifvalue
is an invalid SHA224 hash.
Return whether or not given value is a valid SHA256 hash.
Examples:
>>> sha256( ... 'e3b0c44298fc1c149afbf4c8996fb924' ... '27ae41e4649b934ca495991b7852b855' ... ) True >>> sha256('900zz11') ValidationError(func=sha256, args={'value': '900zz11'})
Arguments:
- value: SHA256 string to validate.
Returns:
(Literal[True]): If
value
is a valid SHA256 hash. (ValidationError): Ifvalue
is an invalid SHA256 hash.
Return whether or not given value is a valid SHA384 hash.
Examples:
>>> sha384( ... 'cb00753f45a35e8bb5a03d699ac65007272c32ab0eded163' ... '1a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7' ... ) True >>> sha384('900zz11') ValidationError(func=sha384, args={'value': '900zz11'})
Arguments:
- value: SHA384 string to validate.
Returns:
(Literal[True]): If
value
is a valid SHA384 hash. (ValidationError): Ifvalue
is an invalid SHA384 hash.
Return whether or not given value is a valid SHA512 hash.
Examples:
>>> sha512( ... 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce' ... '9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af9' ... '27da3e' ... ) True >>> sha512('900zz11') ValidationError(func=sha512, args={'value': '900zz11'})
Arguments:
- value: SHA512 string to validate.
Returns:
(Literal[True]): If
value
is a valid SHA512 hash. (ValidationError): Ifvalue
is an invalid SHA512 hash.
Return whether or not given value is a valid hostname.
Examples:
>>> hostname("ubuntu-pc:443") True >>> hostname("this-pc") True >>> hostname("xn----gtbspbbmkef.xn--p1ai:65535") True >>> hostname("_example.com") ValidationError(func=hostname, args={'value': '_example.com'}) >>> hostname("123.5.77.88:31000") True >>> hostname("12.12.12.12") True >>> hostname("[::1]:22") True >>> hostname("dead:beef:0:0:0:0000:42:1") True >>> hostname("[0:0:0:0:0:ffff:1.2.3.4]:-65538") ValidationError(func=hostname, args={'value': '[0:0:0:0:0:ffff:1.2.3.4]:-65538'}) >>> hostname("[0:&:b:c:@:e:f::]:9999") ValidationError(func=hostname, args={'value': '[0:&:b:c:@:e:f::]:9999'})
Arguments:
- value: Hostname string to validate.
- skip_ipv6_addr: When hostname string cannot be an IPv6 address.
- skip_ipv4_addr: When hostname string cannot be an IPv4 address.
- may_have_port: Hostname string may contain port number.
- maybe_simple: Hostname string maybe only hyphens and alpha-numerals.
- consider_tld: Restrict domain to TLDs allowed by IANA.
- private: Embedded IP address is public if
False
, private/local ifTrue
. - rfc_1034: Allow trailing dot in domain/host name. Ref: RFC 1034.
- rfc_2782: Domain/Host name is of type service record. Ref: RFC 2782.
Returns:
(Literal[True]): If
value
is a valid hostname. (ValidationError): Ifvalue
is an invalid hostname.
Validate a Spanish CIF.
Each company in Spain prior to 2008 had a distinct CIF and has been discontinued. For more information see wikipedia.org/cif.
The new replacement is to use NIF for absolutely everything. The issue is that there are "types" of NIFs now: company, person [citizen or resident] all distinguished by the first character of the DOI. For this reason we will continue to call CIFs NIFs, that are used for companies.
This validator is based on generadordni.es.
Examples:
>>> es_cif('B25162520') True >>> es_cif('B25162529') ValidationError(func=es_cif, args={'value': 'B25162529'})
Arguments:
- value: DOI string which is to be validated.
Returns:
(Literal[True]): If
value
is a valid DOI string. (ValidationError): Ifvalue
is an invalid DOI string.
Validate a Spanish DOI.
A DOI in spain is all NIF / CIF / NIE / DNI -- a digital ID. For more information see wikipedia.org/doi. This validator is based on generadordni.es.
Examples:
>>> es_doi('X0095892M') True >>> es_doi('X0095892X') ValidationError(func=es_doi, args={'value': 'X0095892X'})
Arguments:
- value: DOI string which is to be validated.
Returns:
(Literal[True]): If
value
is a valid DOI string. (ValidationError): Ifvalue
is an invalid DOI string.
Validate a Spanish NIE.
The NIE is a tax identification number in Spain, known in Spanish as the NIE, or more formally the NĂºmero de identidad de extranjero. For more information see wikipedia.org/nie. This validator is based on generadordni.es.
Examples:
>>> es_nie('X0095892M') True >>> es_nie('X0095892X') ValidationError(func=es_nie, args={'value': 'X0095892X'})
Arguments:
- value: DOI string which is to be validated.
Returns:
(Literal[True]): If
value
is a valid DOI string. (ValidationError): Ifvalue
is an invalid DOI string.
Validate a Spanish NIF.
Each entity, be it person or company in Spain has a distinct NIF. Since we've designated CIF to be a company NIF, this NIF is only for person. For more information see wikipedia.org/nif. This validator is based on generadordni.es.
Examples:
>>> es_nif('26643189N') True >>> es_nif('26643189X') ValidationError(func=es_nif, args={'value': '26643189X'})
Arguments:
- value: DOI string which is to be validated.
Returns:
(Literal[True]): If
value
is a valid DOI string. (ValidationError): Ifvalue
is an invalid DOI string.
Validate a Finnish Business ID.
Each company in Finland has a distinct business id. For more information see Finnish Trade Register
Examples:
>>> fi_business_id('0112038-9') # Fast Monkeys Ltd True >>> fi_business_id('1234567-8') # Bogus ID ValidationError(func=fi_business_id, args={'value': '1234567-8'})
Arguments:
- value: Business ID string to be validated.
Returns:
(Literal[True]): If
value
is a valid finnish business id. (ValidationError): Ifvalue
is an invalid finnish business id.
Validate a Finnish Social Security Number.
This validator is based on django-localflavor-fi.
Examples:
>>> fi_ssn('010101-0101') True >>> fi_ssn('101010-0102') ValidationError(func=fi_ssn, args={'value': '101010-0102'})
Arguments:
- value: Social Security Number to be validated.
- allow_temporal_ssn: Whether to accept temporal SSN numbers. Temporal SSN numbers are the ones where the serial is in the range [900-999]. By default temporal SSN numbers are valid.
Returns:
(Literal[True]): If
value
is a valid finnish SSN. (ValidationError): Ifvalue
is an invalid finnish SSN.
Validate a french department number.
Examples:
>>> fr_department(20) # can be an integer ValidationError(func=fr_department, args={'value': 20}) >>> fr_department("20") ValidationError(func=fr_department, args={'value': '20'}) >>> fr_department("971") # Guadeloupe True >>> fr_department("00") ValidationError(func=fr_department, args={'value': '00'}) >>> fr_department('2A') # Corsica True >>> fr_department('2B') True >>> fr_department('2C') ValidationError(func=fr_department, args={'value': '2C'})
Arguments:
- value: French department number to validate.
Returns:
(Literal[True]): If
value
is a valid french department number. (ValidationError): Ifvalue
is an invalid french department number.
Validate a french Social Security Number.
Each french citizen has a distinct Social Security Number. For more information see French Social Security Number (sadly unavailable in english).
Examples:
>>> fr_ssn('1 84 12 76 451 089 46') True >>> fr_ssn('1 84 12 76 451 089') # control key is optional True >>> fr_ssn('3 84 12 76 451 089 46') # wrong gender number ValidationError(func=fr_ssn, args={'value': '3 84 12 76 451 089 46'}) >>> fr_ssn('1 84 12 76 451 089 47') # wrong control key ValidationError(func=fr_ssn, args={'value': '1 84 12 76 451 089 47'})
Arguments:
- value: French Social Security Number string to validate.
Returns:
(Literal[True]): If
value
is a valid french Social Security Number. (ValidationError): Ifvalue
is an invalid french Social Security Number.
Validate an indian aadhar card number.
Examples:
>>> ind_aadhar('3675 9834 6015') True >>> ind_aadhar('3675 ABVC 2133') ValidationError(func=ind_aadhar, args={'value': '3675 ABVC 2133'})
Arguments:
- value: Aadhar card number string to validate.
Returns:
(Literal[True]): If
value
is a valid aadhar card number. (ValidationError): Ifvalue
is an invalid aadhar card number.
Validate a pan card number.
Examples:
>>> ind_pan('ABCDE9999K') True >>> ind_pan('ABC5d7896B') ValidationError(func=ind_pan, args={'value': 'ABC5d7896B'})
Arguments:
- value: PAN card number string to validate.
Returns:
(Literal[True]): If
value
is a valid PAN card number. (ValidationError): Ifvalue
is an invalid PAN card number.
Validate a Russian INN (Taxpayer Identification Number).
The INN can be either 10 digits (for companies) or 12 digits (for individuals). The function checks both the length and the control digits according to Russian tax rules.
Examples:
>>> ru_inn('500100732259') # Valid 12-digit INN True >>> ru_inn('7830002293') # Valid 10-digit INN True >>> ru_inn('1234567890') # Invalid INN ValidationError(func=ru_inn, args={'value': '1234567890'})
Arguments:
- value: Russian INN string to validate. Can contain only digits.
Returns:
(Literal[True]): If
value
is a valid Russian INN. (ValidationError): Ifvalue
is an invalid Russian INN.
Note:
The validation follows the official algorithm:
- For 10-digit INN: checks 10th control digit
- For 12-digit INN: checks both 11th and 12th control digits
Return whether or not given value is a valid IBAN code.
Examples:
>>> iban('DE29100500001061045672') True >>> iban('123456') ValidationError(func=iban, args={'value': '123456'})
Arguments:
- value: IBAN string to validate.
Returns:
(Literal[True]): If
value
is a valid IBAN code. (ValidationError): Ifvalue
is an invalid IBAN code.
Returns whether a given value is a valid IPv4 address.
From Python version 3.9.5 leading zeros are no longer tolerated and are treated as an error. The initial version of ipv4 validator was inspired from WTForms IPAddress validator.
Examples:
>>> ipv4('123.0.0.7') True >>> ipv4('1.1.1.1/8') True >>> ipv4('900.80.70.11') ValidationError(func=ipv4, args={'value': '900.80.70.11'})
Arguments:
- value: IP address string to validate.
- cidr: IP address string may contain CIDR notation.
- strict: IP address string is strictly in CIDR notation.
- private: IP address is public if
False
, private/local/loopback/broadcast ifTrue
. - host_bit: If
False
and host bits (along with network bits) _are_ set in the supplied address, this function raises a validation error. ref IPv4Network.Returns:
(Literal[True]): If
value
is a valid IPv4 address. (ValidationError): Ifvalue
is an invalid IPv4 address.
Returns if a given value is a valid IPv6 address.
Including IPv4-mapped IPv6 addresses. The initial version of ipv6 validator was inspired from WTForms IPAddress validator.
Examples:
>>> ipv6('::ffff:192.0.2.128') True >>> ipv6('::1/128') True >>> ipv6('abc.0.0.1') ValidationError(func=ipv6, args={'value': 'abc.0.0.1'})
Arguments:
- value: IP address string to validate.
- cidr: IP address string may contain CIDR annotation.
- strict: IP address string is strictly in CIDR notation.
- host_bit: If
False
and host bits (along with network bits) _are_ set in the supplied address, this function raises a validation error. ref IPv6Network.Returns:
(Literal[True]): If
value
is a valid IPv6 address. (ValidationError): Ifvalue
is an invalid IPv6 address.
Return whether or not the length of given string is within a specified range.
Examples:
>>> length('something', min_val=2) True >>> length('something', min_val=9, max_val=9) True >>> length('something', max_val=5) ValidationError(func=length, args={'value': 'something', 'max_val': 5})
Arguments:
- value: The string to validate.
- min_val: The minimum required length of the string. If not provided, minimum length will not be checked.
- max_val: The maximum length of the string. If not provided, maximum length will not be checked.
Returns:
(Literal[True]): If
len(value)
is in between the given conditions. (ValidationError): Iflen(value)
is not in between the given conditions.
Raises:
- (ValueError): If either
min_val
ormax_val
is negative.
Return whether or not given value is a valid MAC address.
This validator is based on WTForms MacAddress validator.
Examples:
>>> mac_address('01:23:45:67:ab:CD') True >>> mac_address('00:00:00:00:00') ValidationError(func=mac_address, args={'value': '00:00:00:00:00'})
Arguments:
- value: MAC address string to validate.
Returns:
(Literal[True]): If
value
is a valid MAC address. (ValidationError): Ifvalue
is an invalid MAC address.
Validate whether or not given value is valid slug.
Valid slug can contain only lowercase alphanumeric characters and hyphens. It starts and ends with these lowercase alphanumeric characters.
Examples:
>>> slug('my-slug-2134') True >>> slug('my.slug') ValidationError(func=slug, args={'value': 'my.slug'})
Arguments:
- value: Slug string to validate.
Returns:
(Literal[True]): If
value
is a valid slug. (ValidationError): Ifvalue
is an invalid slug.
Return whether or not given value is a valid URL.
This validator was originally inspired from URL validator of dperini. The following diagram is from urlly::
foo://admin:hunter1@example.com:8042/over/there?name=ferret#nose
\_/ \___/ \_____/ \_________/ \__/\_________/ \_________/ \__/
| | | | | | | |
scheme username password hostname port path query fragment
Examples:
>>> url('http://duck.com') True >>> url('ftp://foobar.dk') True >>> url('http://10.0.0.1') True >>> url('http://example.com/">user@example.com') ValidationError(func=url, args={'value': 'http://example.com/">user@example.com'})
Arguments:
- value: URL string to validate.
- skip_ipv6_addr: When URL string cannot contain an IPv6 address.
- skip_ipv4_addr: When URL string cannot contain an IPv4 address.
- may_have_port: URL string may contain port number.
- simple_host: URL string maybe only hyphens and alpha-numerals.
- strict_query: Fail validation on query string parsing error.
- consider_tld: Restrict domain to TLDs allowed by IANA.
- private: Embedded IP address is public if
False
, private/local ifTrue
. - rfc_1034: Allow trailing dot in domain/host name. Ref: RFC 1034.
- rfc_2782: Domain/Host name is of type service record. Ref: RFC 2782.
- validate_scheme: Function that validates URL scheme.
Returns:
(Literal[True]): If
value
is a valid url. (ValidationError): Ifvalue
is an invalid url.
Return whether or not given value is a valid UUID-v4 string.
This validator is based on WTForms UUID validator.
Examples:
>>> uuid('2bc1c94f-0deb-43e9-92a1-4775189ec9f8') True >>> uuid('2bc1c94f 0deb-43e9-92a1-4775189ec9f8') ValidationError(func=uuid, args={'value': '2bc1c94f 0deb-43e9-92a1-4775189ec9f8'})
Arguments:
- value: UUID string or object to validate.
Returns:
(Literal[True]): If
value
is a valid UUID. (ValidationError): Ifvalue
is an invalid UUID.
Exception class when validation failure occurs.
A decorator that makes given function validator.
Whenever the given func
returns False
this
decorator returns ValidationError
object.
Examples:
>>> @validator ... def even(value): ... return not (value % 2) >>> even(4) True >>> even(5) ValidationError(func=even, args={'value': 5})
Arguments:
- func: Function which is to be decorated.
Returns:
(Callable[..., ValidationError | Literal[True]]): A decorator which returns either
ValidationError
orLiteral[True]
.
Raises:
- (ValidationError): If
r_ve
orRAISE_VALIDATION_ERROR
isTrue