Week #5 | Decorator for schema Validation

karannaoh
Published: 07/01/2019

This Week:

Issue: #507 Provide decorator for validating json schema request payloads. 

Last week: added jsonschema validation to all the endpoints.

example- PR: #517  

validator = get_schema_validator('Type_Of_schema')
try:
   data = await request.json()
   validator.validate(data) 
   behavior = data.get('behavior', None)
   return await delete_behavior(context, behavior)
except jsonschema.exceptions.ValidationError as e:
 raise HTTPPreconditionFailed(content={
      'reason': 'json schema validation error',
      'message': e.message,
      'validator': e.validator,
      'validator_value': e.validator_value,
      'path': [i for i in e.path],
      'schema_path': [i for i in e.schema_path],
      "schema": app_settings['json_schema_definitions']['Behavior']
})

problem in this:- This piece of code was added to all the endpoint causing too much code redundancy

Solution:- Adding an additional argument to the decorator of every route for the validation and validating request only if it's true. 

example: - 1b8703dc28ff3487efa5ba09829af98b528740f4/guillotina/api/addons.py#L19

PR for this: -  #544

 

Next Week: 

Issue #553 Be able to subscribe to content changes.

Will be adding a new addon to guillotina which will subscribe to changes made on any object and publish it on a websocket endpoint. 

1000 characters left