Pyjoyment.Validator.Validation - Perform validations¶
import Pyjoyment.Validator
from Pyjoyment.Validator.Validation import Validation
validator = Pyjoyment.Validator.new()
v = Validation(validator=validator)
v.input = {'foo': 'bar'}
v.required('foo').one_of('bar', 'baz')
print(v.param('foo'))
# Or import the module, as in Google Python Style Guide
import Pyjoyment.Validator
import Pyjoyment.Validator.Validation
validator = Pyjoyment.Validator.new()
v = Pyjoyment.Validator.Validation.new(validator=validator)
v.input = {'foo': 'bar'}
v.required('foo').one_of('bar', 'baz')
print(v.param('foo'))
Pyjoyment.Validator.Validation performs Pyjoyment.Validator
validation checks.
Checks¶
In addition to the attributes and methods below, you can also call validation
checks provided by validator on
Pyjoyment.Validator.Validation objects, similar to
check().
# Call validation checks
v.required('foo').size(2, 5).like(re.compile(r'^[A-Z]'))
v.optional('bar').equal_to('foo')
v.optional('baz').one_of('test', '123')
# Longer version
v.required('foo').check('size', 2, 5).check('like', re.compile(r'^[A-Z]'))
A check with the name of a keyword of Python, as in, is also a method
with the name followed by an underscore, as in_.
See also¶
Pyjoyment, https://mojolicious.org.
Ported from Mojolicious::Validator::Validation of Mojolicious 9.49.
Classes¶
- class Pyjoyment.Validator.Validation.Pyjoyment_Validator_Validation(*, csrf_token: str | None = None, input: dict[str, Any] | None = None, output: dict[str, Any] | None = None, topic: str | None = None, validator: Pyjoyment.Validator.object | None = None)[source]¶
Pyjoyment.Validator.Validationinherits all methods fromPyjo.Baseand implements the following new ones.- check(check: str, /, *args: Any) Self[source]¶
v = v.check('size', 2, 7)
Perform validation check on all values of the current
topic, no more checks will be performed on them after the first one failed. All checks fromPyjoyment.Validatorare supported.
- csrf_protect() Self[source]¶
v = v.csrf_protect()
Validate
csrf_tokenand protect from cross-site request forgery. Tokens are masked with a random value to protect against BREACH attacks.
- csrf_token¶
token = v.csrf_token v.csrf_token = 'fa6a08...'
CSRF token.
- error(name: str, /) list[Any] | None[source]¶
- error(name: str, value: list[Any], /) Self
err = v.error('foo') v = v.error('foo', ['custom_check']) v = v.error('foo', [check, result, *args])
Get or set details for failed validation check, at any given time there can only be one per field.
# Details about failed validation check, result, *args = v.error('foo') # Force validation to fail for a field without performing a check v.error('foo', ['some_made_up_check_name'])
- every_param(name: str | None = None) list[Any][source]¶
values = v.every_param() values = v.every_param('foo')
Similar to
param(), but returns all values sharing the same name as a list.# Get first value first = v.every_param('foo')[0]
- property failed: list[str][source]¶
names = v.failed
Return a list with all names for values that failed validation.
# Names of all values that failed for name in v.failed: print(name)
- has_error(name: str | None = None) bool[source]¶
boolean = v.has_error() boolean = v.has_error('foo')
Check if validation resulted in errors, defaults to checking all fields.
- input: dict[str, Any]¶
input = v.input v.input = {'foo': 'bar', 'baz': [123, 'yada']}
Data to be validated.
- is_valid(name: str | None = None) bool[source]¶
boolean = v.is_valid() boolean = v.is_valid('foo')
Check if validation was successful and field has a value, defaults to checking the current
topic.
- optional(name: str, /, *filters: str) Self[source]¶
v = v.optional('foo') v = v.optional('foo', *filters)
Change validation
topicand apply filters. All filters fromPyjoyment.Validatorare supported.# Trim value and check size v.optional('user', 'trim').size(1, 15)
- output: dict[str, Any]¶
output = v.output v.output = {'foo': 'bar', 'baz': [123, 'yada']}
Validated data.
- param(name: str | None = None) Any[source]¶
value = v.param() value = v.param('foo')
Access validated values, defaults to the current
topic. If there are multiple values sharing the same name, and you want to access more than just the last one, you can useevery_param().# Get value right away user = v.optional('user').size(1, 15).param()
- property passed: list[str][source]¶
names = v.passed
Return a list with all names for values that passed validation.
# Names of all values that passed for name in v.passed: print(name)
- required(name: str, /, *filters: str) Self[source]¶
v = v.required('foo') v = v.required('foo', *filters)
Change validation
topic, apply filters, and make sure a value is present. All filters fromPyjoyment.Validatorare supported.# Trim value and check size v.required('user', 'trim').size(1, 15)
- topic¶
topic = v.topic v.topic = 'foo'
Name of field currently being validated.
- validator¶
validator = v.validator v.validator = Pyjoyment.Validator.new()
Pyjoyment.Validatorobject this validation belongs to.