Getting Started

Learn how to get started with DontTrust


Basic example

Let's look at a really simple example using the Schema() class.

from donttrust import Schema

# "username" is the name of the field.
# It is used to format error messages.
# This is optional, but recommended.
schema = Schema("username").string().required()

try:
    schema.validate("abcdefg")
except Exception as e:
    print(e)
    # Other error handling

Analysing the above example, we can see that we called the Schema() class with the field name as username.

This Schema is then made into a StringSchema and is then made required.

Finally, we validate the schema with the validate(value) function.

We put the validation logic in a try...except block because the validate function will throw an error if there's an error in validation. Use the validate_without_exception function to not throw an exception and just return False if there's an error in validation.

Types of Schemas

Currently, DontTrust supports these schemas:

  • string
  • number including int, float and complex
  • boolean
  • date
  • email

Use a specific schema

To use a schema of a certain datatype,

from dontrust import Schema

schema = Schema("field").string()
schema = Schema("field").number()
# ...

The general syntax is Schema().DATATYPE() where DATATYPE can be any of the types listed above.

Alternative method

You can directly import a Schema class from donttrust.schema without using Schema().DATATYPE().

from donttrust.schema import StringSchema
from donttrust.schema import BooleanSchema
from donttrust.schema import NumberSchema
from donttrust.schema import EmailSchema
from donttrust.schema import DateSchema
In these schemas, the field parameter (Schema(field)) is required

Edit this page on Github