validate.py¶
Validators for indata.
Indata can be sent to validate_field
, which will use the corresponding
functions to check each field.
- validate.validate_datasets(data: list, db=None) → bool[source]¶
Validate input for the
datasets
field.It must be a list of uuids. Validate that the datasets exist in the db.
- Parameters
data (str) – The data to be validated.
db – The database to use. Defaults to
flask.g.db
.
- Returns
Validation passed.
- Return type
bool
- Raises
ValueError – Validation failed.
- validate.validate_email(data) → bool[source]¶
Validate input for the
email
field.It must be a string.
- Parameters
data – The data to be validated.
- Returns
Validation passed.
- Return type
bool
- Raises
ValueError – Validation failed.
- validate.validate_field(field_key: str, field_value: Any, testing=False) → bool[source]¶
Validate that the input data matches expectations.
Will check the data based on the key.
The validation is only done at the technical level, e.g. a check that input is of the correct type.
Checks for e.g. permissions and that the correct fields are provided for the entry must be performed separately.
- Parameters
field_key (str) – The field to validate.
field_value (Any) – The value to validate.
testing (bool) – Whether the function is used for testing.
- Returns
Whether validation passed.
- Return type
bool
- validate.validate_list_of_strings(data: list) → bool[source]¶
Validate that input is a list of strings.
- Parameters
data (list) – The data to be validated.
- Returns
Validation passed.
- Return type
bool
- Raises
ValueError – Validation failed.
- validate.validate_orcid(data: str) → bool[source]¶
Validate input for the
orcid
field.Must be a str
Must math xxxx-xxxx-xxxx-xxxx
- Parameters
data (str) – The data to be validated.
- Returns
Validation passed.
- Return type
bool
- Raises
ValueError – Validation failed.
- validate.validate_permissions(data: list) → bool[source]¶
Validate input for the
permissions
field.Must be a list containing permissions found in
PERMISSIONS
Repeats are not allowed
- Parameters
data (list) – The data to be validated.
- Returns
Validation passed.
- Return type
bool
- Raises
ValueError – Validation failed.
- validate.validate_properties(data: dict) → bool[source]¶
Validate input for the
properties
field.Must be a dict
Keys and values must be strings
Keys and values must be at least 3 characters
Keys and values may not end nor start with whitespace
- Parameters
data (dict) – The data to be validated.
- Returns
Validation passed.
- Return type
bool
- Raises
ValueError – Validation failed.
- validate.validate_string(data: str) → bool[source]¶
Validate input for a field that must have a
str
value.- Parameters
data (str) – The data to be validated.
- Returns
Validation passed.
- Return type
bool
- Raises
ValueError – Validation failed.
- validate.validate_string_non_empty(data: str) → bool[source]¶
Validate input for string fields that may not be empty.
It must be a non-empty string.
- Parameters
data (str) – The data to be validated.
- Returns
Validation passed.
- Return type
bool
- Raises
ValueError – Validation failed.
- validate.validate_tags(data: list) → bool[source]¶
Validate input for the
tags
field.It must be a list
Must be at least 3 characters
May not end nor start with whitespace
- Parameters
data (list) – The data to be validated.
- Returns
Validation passed.
- Return type
bool
- Raises
ValueError – Validation failed.
- validate.validate_url(data: str) → bool[source]¶
Validate input for a url intended for browsers.
It must start with
http(s)://
.- Parameters
data (str) – The data to be validated.
- Returns
Validation passed.
- Return type
bool
- Raises
ValueError – Validation failed.
- validate.validate_user(data: str, db=None) → bool[source]¶
Validate input for a field containing a single user uuid string.
All users must exist in the database.
- Parameters
data (str) – The data to be validated.
db – The database to use. Defaults to
flask.g.db
.
- Returns
Validation passed.
- Return type
bool
- Raises
ValueError – Validation failed.
- validate.validate_user_list(data: list, db=None) → bool[source]¶
Validate input for a field containing a list of user uuid(s).
For compatibility, the input may be UUIDs as either string (single user) or a list (single or multiple users).
All users must exist in the database.
- Parameters
data (list) – The data to be validated.
db – The database to use. Defaults to
flask.g.db
.
- Returns
Validation passed.
- Return type
bool
- Raises
ValueError – Validation failed.