form.validators

Module Contents

Classes

If

Wraps a single validator or a list of validators, which will

Stdnum

Validates a string using any python-stdnum format.

FileSizeLimit

Makes sure an uploaded file is not bigger than the given number of

WhitelistedMimeType

Makes sure an uploaded file is in a whitelist of allowed mimetypes.

ExpectedExtensions

Makes sure an uploaded file has one of the expected extensions. Since

ValidFormDefinition

Makes sure the given text is a valid onegov.form definition.

ValidFilterFormDefinition

Makes sure the given text is a valid onegov.form definition.

LaxDataRequired

A copy of wtform's DataRequired validator, but with a more lax approach

StrictOptional

A copy of wtform's Optional validator, but with a more strict approach

ValidPhoneNumber

Makes sure the given input is valid phone number.

ValidSwissSocialSecurityNumber

Makes sure the given input is a valid swiss social security number.

UniqueColumnValue

Test if the given table does not already have a value in the column

InputRequiredIf

Validator which makes a field required if another field is set and has

ValidDateRange

Makes sure the selected date is in a valid range.

Attributes

swiss_ssn_rgxp

class form.validators.If(condition: FieldCondition[_BaseFormT, _FieldT], *validators: BaseValidator[_BaseFormT, _FieldT])[source]

Wraps a single validator or a list of validators, which will only be executed if the supplied condition callback returns True.

__call__(form: onegov.form.types._BaseFormT, field: onegov.form.types._FieldT) None[source]
class form.validators.Stdnum(format: str)[source]

Validates a string using any python-stdnum format.

See https://github.com/arthurdejong/python-stdnum.

__call__(form: onegov.form.Form, field: wtforms.Field) None[source]
class form.validators.FileSizeLimit(max_bytes: int)[source]

Makes sure an uploaded file is not bigger than the given number of bytes.

Expects an onegov.form.fields.UploadField or onegov.form.fields.UploadMultipleField instance.

message[source]
__call__(form: onegov.form.Form, field: wtforms.Field) None[source]
class form.validators.WhitelistedMimeType(whitelist: Collection[str] | None = None)[source]

Makes sure an uploaded file is in a whitelist of allowed mimetypes.

Expects an onegov.form.fields.UploadField or onegov.form.fields.UploadMultipleField instance.

whitelist: Collection[str][source]
message[source]
__call__(form: onegov.form.Form, field: wtforms.Field) None[source]
class form.validators.ExpectedExtensions(extensions: Sequence[str])[source]

Bases: WhitelistedMimeType

Makes sure an uploaded file has one of the expected extensions. Since extensions are not something we can count on we look up the mimetype of the extension and use that to check.

Expects an onegov.form.fields.UploadField instance.

Usage:

ExpectedExtensions(['*'])  # default whitelist
ExpectedExtensions(['pdf'])  # makes sure the given file is a pdf
class form.validators.ValidFormDefinition(require_email_field: bool = True, reserved_fields: Collection[str] | None = None, require_title_fields: bool = False, validate_prices: bool = True)[source]

Makes sure the given text is a valid onegov.form definition.

message[source]
email[source]
syntax[source]
indent[source]
duplicate[source]
reserved[source]
required[source]
payment_method[source]
minimum_price[source]
__call__(form: onegov.form.Form, field: wtforms.Field) Form | None[source]
_parse_form(field: wtforms.Field, enable_indent_check: bool = True) onegov.form.Form[source]
class form.validators.ValidFilterFormDefinition(require_email_field: bool = True, reserved_fields: Collection[str] | None = None, require_title_fields: bool = False, validate_prices: bool = True)[source]

Bases: ValidFormDefinition

Makes sure the given text is a valid onegov.form definition.

invalid_field_type[source]
__call__(form: onegov.form.Form, field: wtforms.Field) Form | None[source]
class form.validators.LaxDataRequired(message=None)[source]

Bases: wtforms.validators.DataRequired

A copy of wtform’s DataRequired validator, but with a more lax approach to required validation checking. It accepts some specific falsy values, such as numeric falsy values, that would otherwise fail DataRequired.

This is necessary in order for us to validate stored submissions, which get validated after the initial submission in order to avoid losing file uploads.

__call__(form: wtforms.form.BaseForm, field: wtforms.Field) None[source]
class form.validators.StrictOptional(strip_whitespace=True)[source]

Bases: wtforms.validators.Optional

A copy of wtform’s Optional validator, but with a more strict approach to optional validation checking.

See https://github.com/wtforms/wtforms/issues/350

is_missing(value: object) bool[source]
__call__(form: wtforms.form.BaseForm, field: wtforms.Field) None[source]
class form.validators.ValidPhoneNumber(country: str = 'CH', country_whitelist: Collection[str] | None = None)[source]

Makes sure the given input is valid phone number.

Expects an wtforms.StringField instance.

message[source]
__call__(form: onegov.form.Form, field: wtforms.Field) None[source]
form.validators.swiss_ssn_rgxp[source]
class form.validators.ValidSwissSocialSecurityNumber[source]

Makes sure the given input is a valid swiss social security number.

Expects an wtforms.StringField instance.

message[source]
__call__(form: onegov.form.Form, field: wtforms.Field) None[source]
class form.validators.UniqueColumnValue(table: type[onegov.core.orm.Base])[source]

Test if the given table does not already have a value in the column (identified by the field name).

If the form provides a model with such an attribute, we allow this value, too.

Usage:

username = StringField(validators=[UniqueColumnValue(User)])
__call__(form: onegov.form.Form, field: wtforms.Field) None[source]
class form.validators.InputRequiredIf(field_name: str, field_data: object, message: str | None = None)[source]

Bases: wtforms.validators.InputRequired

Validator which makes a field required if another field is set and has the given value.

__call__(form: wtforms.form.BaseForm, field: wtforms.Field) None[source]
class form.validators.ValidDateRange(min: datetime.date | dateutil.relativedelta.relativedelta | None = None, max: datetime.date | dateutil.relativedelta.relativedelta | None = None, message: str | None = None)[source]

Makes sure the selected date is in a valid range.

The default error message can be overriden and be parametrized with min_date and max_date if both are supplied or just with date if only one of them is specified.

property min_date: datetime.date | None[source]
property max_date: datetime.date | None[source]
between_message[source]
after_message[source]
before_message[source]
__call__(form: onegov.form.Form, field: wtforms.Field) None[source]