Basic Schema

Get to know the base schema of DontTrust


The Basic Schema

The Basic Schema is the main Schema for form validation. It isn't restricted to any type. All other schemas inherit this schema.

Usage

Import:

from donttrust import Schema
# OR
from donttrust.schema import Schema

Create:

schema1 = Schema("username").required().disallow("admin")

schema2 = Schema().required()

schema3 = Schema()

Validate:

try:
  schema1.validate("admin")
except:
  # Catch exceptions here

# Without an exception:
v = schema1.validate_without_exception("admin")
print(v)  # False

v = schema2.validate(10)
print(v)  # 10

Methods

required()

It makes the field required. If this method is called and the field is None and NOT any another falsy value (like False or 0).

try:
  v = Schema().required().validate(None)
  print(v)
except:
  # Exception is thrown

default(value)

This value is returned if the validated value is None. Does NOT work with required.

try:
  v = Schema().default("test").validate(None)
  print(v)  # test
except:
  # No exception thrown

allow(value1, value2, ...)

Only value1, value2, etc will be allowed as valid values for the schema, i.e. they will be whitelisted.

try:
  v = Schema().allow("test").validate("invalid value")
  print(v)
except:
  # Exception is thrown

disallow(value1, value2, ...)

Don't allow value1, value2, etc to be valid values of the Schema, i.e. they will be blacklisted.

try:
  v = Schema().disallow("test", "a", "b").validate("test")
  print(v)
except:
  # Exception is thrown
Warning: If you use both allow() and disallow(), disallow() will be preferred.

reset_allow() and reset_disallow()

Resets the whitelist and blacklist respectively.

try:
  v = Schema().disallow("test").reset_disallow().validate("test")
  print(v)  # test
except:
  # No exception thrown

validate(value)

The function we've been seeing so much! Validates the schema with the value and raises an exception which will inherit DontTrustBaseException if validation fails.

import donttrust

try:
  print(donttrust.Schema().required().validate(None))
except donttrust.exceptions.DontTrustBaseException as e:
  # Handles all exceptions raised by DontTrust (except ValidationError, but more on that later)
  print(e.message)

validate_without_exception(value)

Same as validate, but returns False instead of raising exception when validation fails.

schema = Schema("field").required()

print(schema.validate_without_exception("abc"))  # abc
print(schema.validate_without_exception(None))  # False
ATTENTION!
schema = Schema().required()

print(schema.validate_without_exception(None))  # False
print(schema.validate_without_exception(False))  # False

Edit this page on Github