def test_input_schema_clean(self): self.assertEqual( input_schema_clean( {'publishing': {'publish_date': '2012-12-12'}}, { 'type': "object", 'properties': { 'publishing': { 'type': 'object', 'properties': { 'publish_date': { 'type': 'string', }, 'published': { 'default': True, 'type': 'boolean', }, }, }, } } ), { 'publishing': { 'published': True, 'publish_date': '2012-12-12', }, } )
def test_input_schema_clean(self): self.assertEqual( input_schema_clean({'publishing': { 'publish_date': '2012-12-12' }}, { 'type': "object", 'properties': { 'publishing': { 'type': 'object', 'properties': { 'publish_date': { 'type': 'string', }, 'published': { 'default': True, 'type': 'boolean', }, }, }, } }), { 'publishing': { 'published': True, 'publish_date': '2012-12-12', }, })
def _wrapper(self, *args, **kwargs): validation_ruleset = getattr(self, "validation_ruleset") input_schema = create_object_schema_from_validation_ruleset(validation_ruleset) if input_schema is not None: try: encoding = "UTF-8" input_ = json.loads(self.request.body.decode(encoding)) except ValueError: raise jsonschema.ValidationError( "Input is malformed; could not decode JSON object." ) if use_defaults: input_ = input_schema_clean(input_, input_schema) jsonschema.validate( input_, input_schema, cls=validator_cls, format_checker=format_checker ) else: input_ = None setattr(self, "body", input_) output = rh_method(self, *args, **kwargs) if is_future(output): output = yield output if not output and on_empty_404: raise APIError(404, "Resource not found.") if output_schema is not None: try: jsonschema.validate( {"result": output}, { "type": "object", "properties": { "result": output_schema }, "required": ["result"] } ) except jsonschema.ValidationError as error: raise TypeError(str(error)) self.success(output)
def test_input_schema_clean_ignore_other_types(self): self.assertEqual(input_schema_clean('ABC-123', {'type': "string"}), "ABC-123")
def test_input_schema_clean_no_defaults(self): self.assertEqual(input_schema_clean({}, {'type': "object"}), {})