I need to validate the following JSON:
{
"monday": [
//List of MyClass object
],
"tuesday": [
//List of MyClass Object
],
"wednesday": [
//List of MyClass Object
],
......
}
I need to check if it contains any key that is not a day of the week (mondai, thudai for example). Also MyClass
is defined as:
class MyClass(models.Model):
type = models.CharField(unique=True, max_length=20)
value = models.IntegerField()
The fieldvalue
must be a positive integer and cannot exceed 10000. Also, type
must be either cost
or benefit
In Scala I would use the following:
object MyClass {
val ALLOWED_TYPES = Seq[String]("cost", "benefit")
implicit val openingHourReads: Reads[OpeningHour] = (
(JsPath \ "type").read[String].filter(JsonValidationError("Type must be cost or benefit"))(ALLOWED_TYPES.contains(_)) and
(JsPath \ "value").read[Int](min(1).keepAnd(max(10000)))
)(MyClass.apply _)
Is there an equivalent approach in Django?