"""JSON Draft3 Schema Validator Wrapper. ==================================== A wrapper module for a JSON Draft3 Schema validator. JSON Draft3 Schema: https://tools.ietf.org/html/draft-zyp-json-schema-03. This wrapper module allows: - fail_fast=False: do not exit on first validation error. - validating any data dict against any JSON Draft 3 validation schema. - error response returns with body containing: | errors: { field_name1: error_message, field_name2: error_message } (Taken from https://github.com/theorchard/ows-project-manager/) """ from territories import response VALIDATION_ERROR = 'validation_error' def filter_input(data): """Filter function that cleans the input data from the user. Args: data (dict): the user input to be filtered. Returns: (dict): the filtered dict """ for key, value in data.items(): if isinstance(value, str): data[key] = value.strip() return data def validate(data, validator): """Validate JSON dota. Args: data (dict): the JSON to be validated. validator (Draft3Validator): the JSON Draft3 Schema to validate against Returns: Response: the response of the create operation. """ errors = {} data = filter_input(data) for e in sorted(validator.iter_errors(data), key=str): if e.relative_path: errors[e.relative_path.pop()] = e.message elif e.absolute_schema_path: errors[e.absolute_schema_path.pop()] = e.message if errors: return response.create_error_response( code=VALIDATION_ERROR, message=errors, status=400) return response.Response(message={'status': 'ok'}, status=200)