donttrust package

Submodules

donttrust.exceptions module

exception donttrust.exceptions.CharacterException(field: str, charset: str)

Bases: donttrust.exceptions.DontTrustBaseException

charset: str
property message
Returns

Formatted message

exception donttrust.exceptions.DisallowedValueException(field: str, value: str)

Bases: donttrust.exceptions.DontTrustBaseException

property message
Returns

Formatted message

value: str
exception donttrust.exceptions.DontTrustBaseException(field: str, message: str)

Bases: Exception

field: str
property message
Returns

Formatted message

exception donttrust.exceptions.InvalidDateException(field: str, message: str)

Bases: donttrust.exceptions.DontTrustBaseException

field: str
property message
Returns

Formatted message

exception donttrust.exceptions.InvalidEmailException(field: str, message: str)

Bases: donttrust.exceptions.DontTrustBaseException

field: str
property message
Returns

Formatted message

exception donttrust.exceptions.IsNotBooleanException(field: str)

Bases: donttrust.exceptions.DontTrustBaseException

field: str
exception donttrust.exceptions.LengthException(field: str, length: int, min_max=True)

Bases: donttrust.exceptions.DontTrustBaseException

length: int
property message
Returns

Formatted message

min_max: bool
exception donttrust.exceptions.MultipleException(field: str, num: int)

Bases: donttrust.exceptions.DontTrustBaseException

property message
Returns

Formatted message

num: int
exception donttrust.exceptions.RegexException(field: str, charset: str)

Bases: donttrust.exceptions.CharacterException

charset: str
field: str
exception donttrust.exceptions.RequiredException(field: str)

Bases: donttrust.exceptions.DontTrustBaseException

field: str
property message
Returns

Formatted message

exception donttrust.exceptions.SizeException(field: str, length: int, min_max: bool)

Bases: donttrust.exceptions.LengthException

field: str
length: int
min_max: bool
exception donttrust.exceptions.TypeException(field: str, type_: str)

Bases: donttrust.exceptions.DontTrustBaseException

property message
Returns

Formatted message

type: str

donttrust.schema module

class donttrust.schema.BooleanSchema(id_: str)

Bases: donttrust.schema.Schema

allow(*values)

Allow (whitelist) certain values. Blacklist is preferred over this.

If the validation value is not in allowed values, validation fails.

Parameters

values – Values to allow

disallow(*values)

Disallow (blacklist) certain values. Preferred over whitelist

If the validation value is in disallowed values, validation fails.

Parameters

values – Values to disallow

disallow_all()
falsy(value)

Add a value to be validated as False

reset_allow()

Removes all items from whitelist

reset_disallow()

Removes all items from blacklist

strict()

Don’t allow any values other than True or False

truthy(value)

Add a value to be validated as True

validate(value)

Validates the Schema with value.

Parameters

value – Value to validate

Returns

Returns the validated value, or throws an exception if validation fails.

class donttrust.schema.DateSchema(id_: str = 'field')

Bases: donttrust.schema.Schema

max(date: Union[datetime.datetime, str, int])

The date should be less than this date

min(date: Union[datetime.datetime, str, int])

The date should be atleast this date

validate(value: Union[datetime.datetime, str, int])

Validates the Schema with value.

Parameters

value – Value to validate

Returns

Returns the validated value, or throws an exception if validation fails.

class donttrust.schema.EmailSchema(id_: str = 'field')

Bases: donttrust.schema.Schema

allow(*values)

Allow (whitelist) certain values. Blacklist is preferred over this.

If the validation value is not in allowed values, validation fails.

Parameters

values – Values to allow

allow_mail_providers(*mail_providers: str)

Only allow these mail providers (exluding the tlds)

allow_tlds(*tlds: str)

Only allow these top level domains (.com, .net) to be a part of the email

disallow(*values)

Disallow (blacklist) certain values. Preferred over whitelist

If the validation value is in disallowed values, validation fails.

Parameters

values – Values to disallow

disallow_all()
disallow_mail_providers(*mail_providers: str)

Don’t allow these mail providers (exluding the tlds)

disallow_tlds(*tlds: str)

Don’t allow these top level domains (.com, .net) to be a part of the email

reset_allow()

Removes all items from whitelist

reset_disallow()

Removes all items from blacklist

validate(value: str)

Validates the Schema with value.

Parameters

value – Value to validate

Returns

Returns the validated value, or throws an exception if validation fails.

class donttrust.schema.NumberSchema(id_: str = 'field')

Bases: donttrust.schema.Schema

add(num: donttrust.schema.NumberSchema.int)

Add this number to the Number after validation

complex()

Number should be of complex datatype

divide(num: donttrust.schema.NumberSchema.int)

Divide the Number from this number after validation

divisible(num: donttrust.schema.NumberSchema.int)

Alias of multiple

equals(num: int)

Number should equal this number

float()

Number should be of float datatype

int()

Number should be of int datatype

max(num: int)

Number should be lesser than this value

min(num: int)

Number should be atleast this value

multiple(num: donttrust.schema.NumberSchema.int)

Number should be a multiple of this number

multiply(num: donttrust.schema.NumberSchema.int)

Multiply this number to the Number after validation

negative()

Number should be negative

port()

Number should be within the range of 0-65535

positive()

Number should be positive (includes zero)

subtract(num: donttrust.schema.NumberSchema.int)

Subtract this number from the Number after validation

validate(value: int)

Validates the Schema with value.

Parameters

value – Value to validate

Returns

Returns the validated value, or throws an exception if validation fails.

class donttrust.schema.Schema(id_: str = 'field')

Bases: object

allow(*values)

Allow (whitelist) certain values. Blacklist is preferred over this.

If the validation value is not in allowed values, validation fails.

Parameters

values – Values to allow

boolean()

Creates a boolean schema

Returns

The BooleanSchema

date()

Creates an date schema

Returns

The DateSchema

default(value: Any)

Used as default value if the value is None.

Does NOT work with required.

Parameters

value – Value used as default

disallow(*values)

Disallow (blacklist) certain values. Preferred over whitelist

If the validation value is in disallowed values, validation fails.

Parameters

values – Values to disallow

email()

Creates an email schema

Returns

The EmailSchema

field: Optional[str] = None
number()

Creates a number schema

Returns

The NumberSchema

required()

Makes the field required

reset_allow()

Removes all items from whitelist

reset_disallow()

Removes all items from blacklist

string()

Creates a string schema

Returns

The StringSchema

validate(value)

Validates the Schema with value.

Parameters

value – Value to validate

Returns

Returns the validated value, or throws an exception if validation fails.

validate_and_get_message(value)

Validates and gets the exception message (Exception is auto handled)

Parameters

value – Value to validate

Returns

Validated value or message

validate_without_exception(value)

Same as validate, but doesn’t throw an exception and just returns False if validation fails.

Parameters

value – Value to validate

Returns

Returns the validated value or returns False if validation fails

class donttrust.schema.StringSchema(id_: str = 'field')

Bases: donttrust.schema.Schema

allow_empty()

Allows empty strings

alphanum()

String should be alphanumeric

flags(flag: int)

A regex flag to use when compiling the regex defined in the regex() function

lower()

String should be lowercase

max(length: int)

Set maximum length of the string

min(length: int)

Set minimum length of the string

regex(pattern: str)

String should match with the regex pattern

strip()

Strip the string (trim its end of whitespaces) after validation

to_lower()

Make the string lowercase upon validation

to_upper()

Make the string uppercase upon validation

upper()

String should be uppercase

validate(value: str)

Validates the Schema with value.

Parameters

value – Value to validate

Returns

Returns the validated value, or throws an exception if validation fails.

Module contents

class donttrust.DontTrust(**items: donttrust.schema.Schema)

Bases: object

validate(dict_=None, **items)

Validates all schema in this object and throws a donttrust.ValidationError if validation fails. To not raise an error and return False if validation fails, use validate_without_exception.

Parameters
  • dict – The dictionary of items to validate. Use either this or kwargs

  • kwargs – The items to validate. It should be a kwargs object with the same fields as the schema.

Returns

Returns a dictionary with the validated items.

Raises

Raises donttrust.ValidationError if validation fails

validate_and_return_json_object(dict_=None, **items) → dict

Same as validate, but returns a dictionary instead of throwing errors.

Returns {"data": data} if validation succeedes, or {"error": message, "field": fieldname} if validation fails

Parameters
  • dict – The dictionary of items to validate. Use either this or kwargs

  • items – The items to validate. It should be a kwargs object with the same fields as the schema.

Returns

The dictionary

validate_without_exception(dict_=None, **items)

Same as validate, but returns False if validation fails

Parameters
  • dict – The dictionary of items to validate. Use either this or kwargs

  • items – The items to validate. It should be a kwargs object with the same fields as the schema.

Returns

Returns a dictionary with validated data if validation succeeds, else returns False

exception donttrust.ValidationError(field: str, message: str)

Bases: Exception

Error raised when validation of a DontTrust object has failed.