Skip to content

Built in Validators

kk edited this page Dec 13, 2023 · 9 revisions

All validators support common parameters below, they are omitted in the following document:

  • default=None
  • optional=False
  • desc=None
  • invalid_to=None
  • invalid_to_default=False

All string-like validators(str, date, datetime, email...) should treat empty string as None.
All bool type params' default value is False, custom validators should follow this guideline.

# List
list(minlen=0, maxlen=1024, unique=False)
# example:
T.list(T.str).maxlen(10).unique

# Dict, slim: remove None and empty items
dict(key=None, value=None, minlen=0, maxlen=1024, slim=False)
# example:
T.dict({'key1': T.int, 'key2': T.int})
T.dict(key1=T.int, key2=T.int)
T.dict.key(T.str).value(T.int).maxlen(10)

# Enum
enum([...values])
# example:
T.enum('A B C')
T.enum('A,B,C')
T.enum(['A', 'B', 'C'])
T.enum(EnumClass)

# Union
union([...schemas])
union(xxx=schema, ...).by(field)
# example: match schemas by value type
T.union([T.str, T.list(T.str), T.dict(key=T.str)])
# example: match schemas by specified field
T.union(type1=T.dict(...), type2=T.dict(...)).by('type')

# Any
any()
# example:
T.any

# Integer
int(min=-(2**64-1), max=2**64-1)
# example:
T.int.min(1).max(12)

# Boolean
bool()
# example:
T.bool

# Float, exmin: exclusive min value, exmax: exclusive max value
float(min=-sys.float_info.max, max=sys.float_info.max,
    exmin=-sys.float_info.max, exmax=sys.float_info.max)
# example:
T.float.min(0).exmax(1)

# Bytes
bytes(minlen=0, maxlen=-1)
# example:
T.bytes.minlen(0).maxlen(1024)

# String
# escape: HTML escape or not
# strip: strip whitespace or not
# match: regex string
# accept_object: accept any type objects and convert it by str() function,
#     otherwize only accept str and int type objects.
str(minlen=0, maxlen=1024*1024, escape=False, strip=False, match=None, accept_object=False)
# example:
T.str.match('[a-zA-Z0-9]+')
T.str.minlen(255).strip

# Nullable string
# parameters same as str validator
nstr(minlen=0, maxlen=1024*1024, escape=False, strip=False, match=None, accept_object=False)
# example:
T.nstr.optional

# Date, Time and Datetime, default format is ISO8601
date(format='%Y-%m-%d', object=False)
time(format='%H:%M:%S', object=False)
datetime(format='%Y-%m-%dT%H:%M:%S.%fZ', object=False)
# example:
T.datetime         # return datetime string
T.datetime.object  # return datetime object

# Go's duration string, such as "300ms", "-1.5h" or "2h45m". 
# Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
# Extended time units are "d", "w", "mo", "y".
timedelta(min=None, max=None, extended=False, string=False, object=False)
# example:
T.timedelta.max('60s')  # return float number of seconds, eg: 30.0
T.timedelta.string      # return duration string, eg: 30s
T.timedelta.object      # return timedelta object

# Email address
email()
# example:
T.email

# IP address, object type: ipaddress.IPv4Address, ipaddress.IPv6Address
ipv4(object=False)
ipv6(object=False)
# example:
T.ipv4

# URL, object type: urllib.parse.ParseResult
url(scheme='http https', maxlen=255, object=False)
# example:
T.url.scheme('https')

# FQDN, eg: www.google.com
fqdn()
# example:
T.fqdn

# Slug Text
slug(minlen=0, maxlen=255, strip=False)
# example:
T.slug.maxlen(16)

# UUID, object type: uuid.UUID
uuid(version=None, object=False)
# example:
T.uuid
T.uuid.version(4)

# Phone number, allow `+##` country prefix, only support 11 digits phone number
phone(strip=False)
# example:
T.phone

# Chinese idcard
idcard(strip=False)