Validators#

Validators take a string and returns whether this string satisfies a set of rules. The most common application of this is for data types. Validators also capture state. For example, the Integer validator keeps track of the largest value it has seen.

Text#

class baskerville.Text#

Validates values as text and will therefore always succeed, collecting information on the longest seen value.

Example

>>> text = baskerville.Text()
>>> text.validate("42")
True
>>> text.validate("Ferris")
True
max_length#

The maximum number of bytes of a value this validator has validated successfully.

Type:

int

min_length#

The minimum number of bytes of a value this validator has validated successfully.

Type:

int

class baskerville.Literal(values)#

Validates on literal values provided at creation. For example, you could match on the values True and False to implement a boolean type validator.

Parameters:

values (list[str]) -- List of literal values that will succeed.

Example

>>> boolean = baskerville.Literal(["True", "False"])
>>> boolean.validate("True")
True
>>> boolean.validate("Ferris")
False

Numeric#

class baskerville.Integer#

Represents a 128-bit signed integer and captures the largest and smallest values seen. These values can then be used to inform a more detailed inferred type. For example, if the minimum value is 0, then the field may be representable as an unsigned integer.

Example

>>> integer = baskerville.Integer()
>>> integer.validate("+42")
True
>>> integer.validate("Ferris")
False
leading_plus#

Whether this validator has seen a value with a leading '+' sign.

Type:

bool

max_value#

Maximum value that this validator has validated successfully.

Type:

int

min_value#

Minimum value that this validator has validated successfully.

Type:

int

class baskerville.Float#

Represents a 64-bit floating point number.

Example

>>> float_ = baskerville.Float()
>>> float_.validate("4.2")
True
>>> float_.validate("42")
True
>>> float_.validate("+42e-1")
True
>>> float_.validate("Ferris")
False
e_notation#

Whether this validator has seen a value written in E notation.

Example

>>> float_ = baskerville.Float()
>>> float_.e_notation
False
>>> float_.validate("42e-1")
True
>>> float_.e_notation
True
Type:

bool

leading_plus#

Whether this validator has seen a value with a leading '+' sign.

Type:

bool

max_value#

Maximum value that this validator has validated successfully.

Type:

float

min_value#

Minimum value that this validator has validated successfully.

Type:

float

Time#

class baskerville.Date(formats=None)#

Example:

>>> date = baskerville.Date()
>>> date.validate("2001-01-22")
True
>>> date.formats
['%Y-%m-%d']
formats#

List of valid date strftime-like format strings

Note

This will clone the entire list and its elements when retrieved. Consider memoizing.

Type:

list[str]

class baskerville.Time(formats=None)#

Example:

>>> time = baskerville.Time()
>>> time.validate("12:34:56")
True
>>> time.formats
['%H:%M:%S']
formats#

List of valid time strftime-like format strings

Note

This will clone the entire list and its elements when retrieved. Consider memoizing.

Type:

list[str]

class baskerville.DateTime(formats=None)#

Validates date-time values.

Parameters:

formats (Optional[list[DateTimeFormat]]) -- List of date-time formats.

Example

>>> date_time = baskerville.DateTime()
>>> date_time.validate("Mon, 22 Jan 2001 00:00:00 GMT")
True
>>> date_time.formats
[RFC2822]
>>> date_time = baskerville.DateTime(formats=[baskerville.DateTimeFormat.Unix])
>>> date_time.validate("980121600")
True
>>> date_time.validate("Ferris")
False
formats#

List of valid date-time formats

Note

This will clone the entire list and its elements when retrieved. Consider memoizing.

Type:

list[DateTimeFormat]

class baskerville.DateTimeFormat#
RFC2822 = RFC2822#
RFC3339 = RFC3339#
static Strftime(strftime)#
Parameters:

strftime (str) --

Strftime-like format string.

Unix = Unix#

Other#

class baskerville.Empty#

Validates empty values.

Example

>>> empty = baskerville.Empty()
>>> empty.validate("")
True
>>> empty.validate("Ferris")
False
class baskerville.Unique#

Validates unique values. If the validator has seen a given value before, then it will fail to validate.

Example

>>> unique = baskerville.Unique()
>>> unique.validate("Ferris")
True
>>> unique.validate("Corro")
True
>>> unique.validate("Ferris")
False