6

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?

ganqqwerty
  • 1,894
  • 2
  • 23
  • 36
  • django-rest-framework serializers already validate the incoming data via their `.is_valid()` method. Why do you need json-schema? – Todor Jan 27 '15 at 20:10
  • 1
    Let's say that it's just my preference. Json-schema can be published and used as an unambiguios standard of the data exchange format that we can give to the data producers whereas writing validators in the code – ganqqwerty Jan 28 '15 at 10:13
  • Maybe [Json Schema Toolkit](https://json-schema-toolkit.readthedocs.org/en/latest/#) might help? – Leonardo Feb 24 '16 at 18:04

1 Answers1

4

[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.

Community
  • 1
  • 1
jfunk
  • 7,176
  • 4
  • 37
  • 38