Validation¶
flarchitect ships with a suite of field validators that hook directly into
Marshmallow. Validators can be attached to a model column via the SQLAlchemy
info mapping or inferred automatically from column names and formats.
For a runnable example demonstrating email and URL validation see the validators demo.
Basic usage¶
class Author(db.Model):
email = db.Column(
db.String,
info={"validate": "email"},
)
website = db.Column(
db.String,
info={"format": "uri"}, # auto adds URL validation
)
When invalid data is sent to the API a 400 response is returned:
{
"errors": {"email": ["Email address is not valid."]},
"status_code": 400,
"value": null
}
Field validation¶
flarchitect inspects the info mapping and the optional format
attribute on SQLAlchemy columns to determine which validators to apply. When
API_AUTO_VALIDATE is enabled, common formats such as email or date
are added automatically based on column names. Any validator supported by
validate_by_type can also be assigned manually:
class Payment(db.Model):
account = db.Column(db.String, info={"validate": "iban"})
cron = db.Column(db.String, info={"format": "cron"})
This associates the iban and cron validators with the account and
cron columns respectively. Invalid values cause Marshmallow to raise a
ValidationError and the API will respond with 400.
Available validators¶
validate_by_type supports the following names:
emailurlipv4ipv6macsluguuidcardcountry_codedomainmd5sha1sha224sha256sha384sha512hostnameibancronbase64currencyphonepostal_codedatedatetimetimebooleandecimal