From bf659ff2c77fff3db16080c0b89ea7457518392a Mon Sep 17 00:00:00 2001 From: Ben Webb Date: Tue, 19 Feb 2019 13:08:14 +0900 Subject: [PATCH] [openownership/cove-bods#16] Improve some validation messages These are just the easiest changes to make. https://docs.google.com/document/d/1U3e_b-ZaYnx6QEuCSz03Di3lzof7iPDTP8OpSgZyFU8/edit# --- libcove/lib/common.py | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/libcove/lib/common.py b/libcove/lib/common.py index beb22c1..189b1ea 100644 --- a/libcove/lib/common.py +++ b/libcove/lib/common.py @@ -31,21 +31,23 @@ LANGUAGE_RE = re.compile("^(.*_(((([A-Za-z]{2,3}(-([A-Za-z]{3}(-[A-Za-z]{3}){0,2}))?)|[A-Za-z]{4}|[A-Za-z]{5,8})(-([A-Za-z]{4}))?(-([A-Za-z]{2}|[0-9]{3}))?(-([A-Za-z0-9]{5,8}|[0-9][A-Za-z0-9]{3}))*(-([0-9A-WY-Za-wy-z](-[A-Za-z0-9]{2,8})+))*(-(x(-[A-Za-z0-9]{1,8})+))?)|(x(-[A-Za-z0-9]{1,8})+)))$") # noqa validation_error_template_lookup = {'date-time': 'Date is not in the correct format', 'uri': 'Invalid \'uri\' found', - 'string': '\'{}\' is not a string. Check that the value {} has quotes at the start and end. Escape any quotes in the value with \'\\\'', # noqa - 'integer': '\'{}\' is not a integer. Check that the value {} doesn’t contain decimal points or any characters other than 0-9. Integer values should not be in quotes. ', # noqa - 'number': '\'{}\' is not a number. Check that the value {} doesn’t contain any characters other than 0-9 and dot (\'.\'). Number values should not be in quotes. ', # noqa - 'object': '\'{}\' is not a JSON object', - 'array': '\'{}\' is not a JSON array'} + 'string': '\'{}\' should be a string. Check that the value {} has quotes at the start and end. Escape any quotes in the value with \'\\\'', # noqa + 'integer': '\'{}\' should be an integer. Check that the value {} doesn’t contain decimal points or any characters other than 0-9. Integer values should not be in quotes. ', # noqa + 'number': '\'{}\' should be a number. Check that the value {} doesn’t contain any characters other than 0-9 and dot (\'.\'). Number values should not be in quotes. ', # noqa + 'boolean': '\'{}\' should be a JSON boolean, \'true\' or \'false\'.', # noqa + 'object': '\'{}\' should be a JSON object', + 'array': '\'{}\' should be a JSON array. Check that value(s) appear within square brackets, [...]'} # These are "safe" html that we trust # Don't insert any values into these strings without ensuring escaping # e.g. using django's format_html function. validation_error_template_lookup_safe = {'date-time': 'Date is not in the correct format', 'uri': 'Invalid \'uri\' found', - 'string': '{} is not a string. Check that the value {} has quotes at the start and end. Escape any quotes in the value with \', # noqa - 'integer': '{} is not a integer. Check that the value {} doesn’t contain decimal points or any characters other than 0-9. Integer values should not be in quotes. ', # noqa - 'number': '{} is not a number. Check that the value {} doesn’t contain any characters other than 0-9 and dot (.). Number values should not be in quotes. ', # noqa - 'object': '{} is not a JSON object', - 'array': '{} is not a JSON array'} + 'string': '{} should be a string. Check that the value {} has quotes at the start and end. Escape any quotes in the value with \', # noqa + 'integer': '{} should be an integer. Check that the value {} doesn’t contain decimal points or any characters other than 0-9. Integer values should not be in quotes. ', # noqa + 'number': '{} should be a number. Check that the value {} doesn’t contain any characters other than 0-9 and dot (.). Number values should not be in quotes. ', # noqa + 'boolean': '{} should be a JSON boolean, true or false.', # noqa + 'object': '{} should be a JSON object', + 'array': '{} should be a JSON array. Check that value(s) appear within square brackets, [...]'} def unique_ids(validator, ui, instance, schema): @@ -562,17 +564,17 @@ def get_schema_validation_errors(json_data, schema_obj, schema_name, cell_src_ma field_name = heading[0][1] value['header'] = heading[0][1] if parent_name: - message = "'{}' is missing but required within '{}'".format(field_name, parent_name) - message_safe = format_html("{} is missing but required within {}", field_name, parent_name) # noqa + message = "'{}' is missing but required within '{}'. Check that the field is included and correctly spelled.".format(field_name, parent_name) + message_safe = format_html("{} is missing but required within {}. Check that the field is included and correctly spelled.", field_name, parent_name) # noqa else: - message = "'{}' is missing but required".format(field_name) - message_safe = format_html("{} is missing but required", field_name, parent_name) + message = "'{}' is missing but required. Check that the field is included and correctly spelled.".format(field_name) + message_safe = format_html("{} is missing but required. Check that the field is included and correctly spelled.", field_name, parent_name) if e.validator == 'enum': if "isCodelist" in e.schema: continue - message = "Invalid code found in '{}'".format(header) - message_safe = format_html("Invalid code found in {}", header) + message = "'{}' contains an unrecognised value. Check the related codelist for allowed code values.".format(header) + message_safe = format_html("{} contains an unrecognised value. Check the related codelist for allowed code values.", header) if e.validator == 'pattern': message_safe = format_html('{} does not match the regex {}', header, e.validator_value) # noqa