jazzband / jsonmodels

jsonmodels is library to make it easier for you to deal with structures that are converted to, or read from JSON.
http://jsonmodels.readthedocs.org/en/latest/
BSD 3-Clause "New" or "Revised" License
334 stars 51 forks source link

add is_valid logic to base model #173

Open mojtabaakbari221b opened 1 year ago

mojtabaakbari221b commented 1 year ago

Suppose you want to validate a schema, in the current state, you should call the validate function and see if it is none, that means the data is valid. The first problem is that we don't have a clean code of true or false, and the second is that we don't have a list of fields that are invalid, and we only know about 1 field being invalid. The second problem: suppose we want to return a list of invalid fields to the user, how?

This is how you can do these things with the new codes : And imagine that we use drf in our api development.

from rest_framework.response import Response

person = Person(name='Chuck', surname='Norris')

if person.is_valid() 
   return Reponse(status=201)
else:
   return Response(data=person.errors, status=401)

In general, this implementation is inspired by the implementation of isـvalid in drf serializer