Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tools to convert jsonschema into Django REST serializisers?

I've written a json-schema to validate the json that comes with the POST request to my API. No I need to deserialize this json.

When I started builing the nested serializers structure I noticed that the process is very similar. So my question is: can my already written json-schema help with deserilization process?

like image 628
ganqqwerty Avatar asked Jan 27 '15 19:01

ganqqwerty


People also ask

Do we need Serializers in Django REST framework?

Serializers in Django REST Framework are responsible for converting objects into data types understandable by javascript and front-end frameworks. Serializers also provide deserialization, allowing parsed data to be converted back into complex types, after first validating the incoming data.

How do you pass extra context data to Serializers in Django REST framework?

In function-based views, we can pass extra context to serializer with “context” parameter with a dictionary. To access the extra context data inside the serializer we can simply access it with “self. context”. From example, to get “exclude_email_list” we just used code 'exclude_email_list = self.

What is serializer Is_valid?

The .is_valid() method takes an optional raise_exception flag that will cause it to raise a serializers.ValidationError exception if there are validation errors.

What are model Serializers in Django?

The ModelSerializer class provides a shortcut that lets you automatically create a Serializer class with fields that correspond to the Model fields. The ModelSerializer class is the same as a regular Serializer class, except that: It will automatically generate a set of fields for you, based on the model.


1 Answers

[UPDATE] The blog post I linked below is quite outdated. Instead, I've gone and implemented a modern example of using JSONSchema validation and DRF together which you can see on this answer here.

Before I stumbled across your question I found this article which demonstrates how (and potentially when) to use a JSONSchema with Django REST Framework.

Django Rest Framework integrates well with models to provide out of the box validation, and ModelSerializers allow futher fine-grained custom validation. However, if you’re not using a model as the resource of your endpoint then the code required for custom validation of complex data structure can get hairy.

If there is a heavily nested data structure then a serializer can be used that has a nested serializer, which also has a nested serializer, and so on – or a JSON schema and custom JSON parser can be employed.

Using a JSON schema generation tool also provides a quick win: generate the canonical “pattern” of valid JSON structure using data known to be of the correct structure. This post will go through doing this JSON schema validation using Django Rest Framework.

like image 73
jfunk Avatar answered Oct 01 '22 20:10

jfunk