15.2.172. camcops_server.cc_modules.cc_validators¶
camcops_server/cc_modules/cc_validators.py
Copyright (C) 2012, University of Cambridge, Department of Psychiatry. Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
This file is part of CamCOPS.
CamCOPS is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
CamCOPS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with CamCOPS. If not, see <https://www.gnu.org/licenses/>.
String validators and the like.
All functions starting validate_
do nothing if the input is good, and raise
ValueError
if it’s bad, with a descriptive error (you can use str()
on the exception).
All validators take a
camcops_server.cc_modules.cc_request.CamcopsRequest
parameter, for
internationalized error messages.
WARNING: even the error messages shouldn’t contain the error-producing strings.
- camcops_server.cc_modules.cc_validators.anchor(expression: str, anchor_start: bool = True, anchor_end: bool = True) str [source]¶
Adds start/end anchors.
- camcops_server.cc_modules.cc_validators.describe_regex_permitted_char(expression: str, req: Optional[CamcopsRequest] = None, invalid_prefix: bool = True) str [source]¶
Describes the characters permitted in a regular expression character selector – as long as it’s simple! This won’t handle arbitrary regexes.
- camcops_server.cc_modules.cc_validators.describe_regex_permitted_char_length(expression: str, max_length: int, min_length: int = 1, req: Optional[CamcopsRequest] = None) str [source]¶
Describes a valid string by permitted characters and length.
- camcops_server.cc_modules.cc_validators.dummy_gettext(x: str) str [source]¶
Returns the input directly.
- camcops_server.cc_modules.cc_validators.min_max_copies(expression: str, max_count: int, min_count: int = 1) str [source]¶
Given a regex expression, permit it a minimum/maximum number of times. For example, for a regex group
x
, producex{min,max}
.Be very careful if you use
min_count == 0
– without other restrictions, your regex may match an empty string.
- camcops_server.cc_modules.cc_validators.one_or_more(expression: str) str [source]¶
Regex for one or more copies.
- camcops_server.cc_modules.cc_validators.validate_alphanum(x: str, req: Optional[CamcopsRequest] = None) None [source]¶
Validates a generic alphanumeric string.
- camcops_server.cc_modules.cc_validators.validate_alphanum_underscore(x: str, req: Optional[CamcopsRequest] = None) None [source]¶
Validates a string that can be alphanumeric or contain an underscore.
- camcops_server.cc_modules.cc_validators.validate_any_url(url: str, req: Optional[CamcopsRequest] = None) None [source]¶
Validates a URL. If valid, returns the URL; if not, returns
default
. See https://stackoverflow.com/questions/22238090/validating-urls-in-pythonHowever, avoid this one. For example, a URL such as xxhttps://127.0.0.1:8088/ can trigger Chrome to launch
xdg-open
.
- camcops_server.cc_modules.cc_validators.validate_anything(x: str, req: Optional[CamcopsRequest] = None) None [source]¶
Lets anything through. May be unwise.
- camcops_server.cc_modules.cc_validators.validate_by_char_and_length(x: str, permitted_char_expression: str, max_length: int, min_length: int = 1, req: Optional[CamcopsRequest] = None, flags: int = 0) None [source]¶
Validate a string based on permitted characters and length.
- camcops_server.cc_modules.cc_validators.validate_device_name(x: str, req: Optional[CamcopsRequest] = None) None [source]¶
Validate a client device name – the computer-oriented one, not the friendly one.
- camcops_server.cc_modules.cc_validators.validate_download_filename(x: str, req: Optional[CamcopsRequest] = None) None [source]¶
Validate a file for user download.
Permit e.g.
CamCOPS_dump_2021-06-04T100622.zip
.Prohibit silly things (like directory/drive delimiters).
- camcops_server.cc_modules.cc_validators.validate_email(email: str, req: Optional[CamcopsRequest] = None) None [source]¶
Validate an e-mail address.
Is this a valid e-mail address?
We use the same validation system as our web form (which uses Colander’s method plus a length constraint).
- camcops_server.cc_modules.cc_validators.validate_group_name(name: str, req: Optional[CamcopsRequest] = None) None [source]¶
Is the string a valid group name?
Group descriptions can be anything, but group names shouldn’t have odd characters in – this greatly facilitates config file handling etc. (for example: no spaces, no commas).
- camcops_server.cc_modules.cc_validators.validate_hl7_aa(x: str, req: Optional[CamcopsRequest] = None) None [source]¶
Validate HL7 Assigning Authority.
- camcops_server.cc_modules.cc_validators.validate_hl7_id_type(x: str, req: Optional[CamcopsRequest] = None) None [source]¶
Validate HL7 Identifier Type.
- camcops_server.cc_modules.cc_validators.validate_human_name(x: str, req: Optional[CamcopsRequest] = None, min_length: int = 0, max_length: int = 255) None [source]¶
Accepts spaces, accents, etc.
This is hard. See https://stackoverflow.com/questions/888838/regular-expression-for-validating-names-and-surnames
- camcops_server.cc_modules.cc_validators.validate_ip_address(x: str, req: Optional[CamcopsRequest] = None) None [source]¶
Validates an IP address.
- camcops_server.cc_modules.cc_validators.validate_new_password(x: str, req: Optional[CamcopsRequest] = None) None [source]¶
Validate a proposed new password. Enforce our password policy.
- camcops_server.cc_modules.cc_validators.validate_redirect_url(url: str, req: Optional[CamcopsRequest] = None) None [source]¶
Validates a URL. If valid, returns the URL; if not, returns
default
. See https://stackoverflow.com/questions/22238090/validating-urls-in-python
- camcops_server.cc_modules.cc_validators.validate_restricted_sql_search_literal(x: str, req: Optional[CamcopsRequest] = None, min_length: int = 0, max_length: int = 255) None [source]¶
Validates a string that can be fairly broad, and can do SQL finding via wildcards such as
%
and_
, but should be syntactically safe in terms of HTML etc. It does not permit arbitrary strings; it’s a subset of what might be possible in SQL.
- camcops_server.cc_modules.cc_validators.validate_task_tablename(x: str, req: Optional[CamcopsRequest] = None) None [source]¶
Validates a string that could be a task tablename.