Skip to content

Commit

Permalink
[openownership/cove-bods#16] Improve some validation messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Bjwebb committed Feb 19, 2019
1 parent b3b53b9 commit bf659ff
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions libcove/lib/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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': '<code>{}</code> is not a string. Check that the value {} has quotes at the start and end. Escape any quotes in the value with <code>\</code>', # noqa
'integer': '<code>{}</code> 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': '<code>{}</code> is not a number. Check that the value {} doesn’t contain any characters other than 0-9 and dot (<code>.</code>). Number values should not be in quotes. ', # noqa
'object': '<code>{}</code> is not a JSON object',
'array': '<code>{}</code> is not a JSON array'}
'string': '<code>{}</code> should be a string. Check that the value {} has quotes at the start and end. Escape any quotes in the value with <code>\</code>', # noqa
'integer': '<code>{}</code> 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': '<code>{}</code> should be a number. Check that the value {} doesn’t contain any characters other than 0-9 and dot (<code>.</code>). Number values should not be in quotes. ', # noqa
'boolean': '<code>{}</code> should be a JSON boolean, <code>true</code> or <code>false</code>.', # noqa
'object': '<code>{}</code> should be a JSON object',
'array': '<code>{}</code> should be a JSON array. Check that value(s) appear within square brackets, [...]'}


def unique_ids(validator, ui, instance, schema):
Expand Down Expand Up @@ -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("<code>{}</code> is missing but required within <code>{}</code>", 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("<code>{}</code> is missing but required within <code>{}</code>. 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("<code>{}</code> 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("<code>{}</code> 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 <code>{}</code>", header)
message = "'{}' contains an unrecognised value. Check the related codelist for allowed code values.".format(header)
message_safe = format_html("<code>{}</code> contains an unrecognised value. Check the related codelist for allowed code values.", header)

if e.validator == 'pattern':
message_safe = format_html('<code>{}</code> does not match the regex <code>{}</code>', header, e.validator_value) # noqa
Expand Down

0 comments on commit bf659ff

Please sign in to comment.