Tuesday 6 June 2017

AJV schema validation for nested object

Functions returns object which looks something like this:

    {
        "answer": {
           "vehicle_type": 1,
           "message": "Car"
        },
        "model": "VW",
        "color": "red"
    }

'Answer' object is always there. Other fields are there based on 'vehicle_type'.

E.g.

if vehicle_type = 1 there are 'model' and 'color'.

if vehicle_type = 2 there are 'engine_count', 'seat_count' and 'wing_count'.

I am trying to write JSON-schema which I will use to validate returned object.

I would like to set 'model' and 'color' as required properties if 'vehicle_type' is 1. And if 'vehicle_type' is 2, then 'engine_count', 'seat_count' and 'wing_count' are required.

I am using AJV (https://github.com/epoberezkin/ajv) schema validator.

For me, it's problematic because vehicle_type is nested inside 'answer', and properties which I want to mark as required are on the parent object. In other words, 'validation_type' is not on the same level as 'model' or 'engine_count'.

I already several different approached... I also tried with ajv-keywords (switch, if/else/then) but i didn't have any luck

Any ideas?



via Klik Kliković

No comments:

Post a Comment