be4i
be4i

Reputation: 47

How to validated pointer in json.net?

Hello I have the following scenario:

JSON object:

{
    "$id": "1",
    "someProp": "123",
    "children": [{
        "$id": "2",
        "$type": "ClassB",
        "Parent": {
            "$ref": "1"
        }
    }]
}

JSON schema :

{
    "id": "ClassA",
    "required": true,
    "type": [
        "object",
        "null"
    ],
    "properties": {
        "someProp": {
            "required": true,
            "type": [
                "string",
                "null"
            ]
        },
        "children": {
            "id": "List<Child>",
            "required": true,
            "type": [
                "array",
                "null"
            ],
            "items": {
                "id": "Child",
                "type": [
                    "object",
                    "null"
                ],
                "properties": {
                    "id": {
                        "required": true,
                        "type": "integer"
                    },
                    "parent": {
                        "$ref": "ClassA"
                    }
                }
            }
        }
    }
}

I have a complex object who has reference loops, so I have configured json.net to make reference when the object is serialized. Everything is working as expected I can serialize and deserialize the object, but when I am validating the JSON object with the above schema I got the following error :

Required properties are missing from object : "someProp", path : object.Children[0].parent

And that is correct how can make the schema look at the reference JSON object ?

Upvotes: 2

Views: 244

Answers (1)

Dipen Shah
Dipen Shah

Reputation: 26075

Problem is with the "id" property of an object inside children array

"properties": {
    "id": {
             "required": true,
             "type": "integer"
           },
    "parent": {
               "$ref": "ClassA"
              }
}

You are saying id requires to have property "id" and you don't have that property inside object, either changes "id" to "$id" or remove it from property list to satisfy schema.

"properties": {
    "$id": {
             "required": true,
             "type": "integer"
           },
    "parent": {
               "$ref": "ClassA"
              }
}

Also make sure you have "id" / "$id" integer value, i.e. "$id":2 not "$id":"2"

Upvotes: 0

Related Questions