Validation JSON schema with a Graphql response

Hi,

I’m having a issue with validating a json response from a graphql relay endpoint.

I’m trying to put additional Properties to false. But once I add that validation the tests fails. Even if specified all fields.

Here is my body response :

{
    "data": {
        "posts": {
            "edges": [
                {
                    "node": {
                        "title": "Pénitencier de Saint-Jérôme: un gardien meurt de la COVID-19",
                        "slug": "penitencier-de-saint-jerome-un-gardien-meurt-de-la-covid-19",
                        "id": "cG9zdDo4OTQ0"
                    }
                },
                {
                    "node": {
                        "title": "Une excellente 20e position pour Cendrine Browne en skiathlon",
                        "slug": "une-excellente-20e-position-pour-cendrine-browne-en-skiathlon",
                        "id": "cG9zdDo4OTQw"
                    }
                },
                {
                    "node": {
                        "title": "Le transport collectif mis à mal par le variant Omicron",
                        "slug": "le-transport-collectif-mis-a-mal-par-le-variant-omicron",
                        "id": "cG9zdDo4OTE0"
                    }
                },
                {
                    "node": {
                        "title": "Paul Germain à la présidence : Bernard Pilon devient directeur du P’tit Train du Nord",
                        "slug": "paul-germain-a-la-presidence-bernard-pilon-devient-directeur-du-ptit-train-du-nord",
                        "id": "cG9zdDo4OTMz"
                    }
                },
                {
                    "node": {
                        "title": "Une nouvelle convention collective est signée : l’APTS souhaite « un réseau public accessible et humain  »",
                        "slug": "une-nouvelle-convention-collective-est-signee-lapts-souhaite-un-reseau-public-accessible-et-humain ",
                        "id": "cG9zdDo4OTMw"
                    }
                },
                {
                    "node": {
                        "title": "Canadian Tire de Saint-Jérôme : l’heure de la retraite a sonné pour Jean-Guy Poulin",
                        "slug": "canadian-tire-de-saint-jerome-lheure-de-la-retraite-a-sonne-pour-jean-guy-poulin",
                        "id": "cG9zdDo4OTI2"
                    }
                },
                {
                    "node": {
                        "title": "Malgré la taxe spéciale : Saint-Colomban enregistre une hausse de la construction",
                        "slug": "malgre-la-taxe-speciale-saint-colomban-enregistre-une-hausse-de-la-construction",
                        "id": "cG9zdDo4OTI0"
                    }
                },
                {
                    "node": {
                        "title": "Saint-Jérôme : quel avenir pour le BIPA ?",
                        "slug": "saint-jerome-quel-avenir-pour-le-bipa",
                        "id": "cG9zdDo4OTIx"
                    }
                },
                {
                    "node": {
                        "title": "Convoi de la liberté vers Ottawa : quelques centaines de personnes répondent à l’appel",
                        "slug": "convoi-de-la-liberte-vers-ottawa-quelques-centaines-de-personnes-repondent-a-lappel",
                        "id": "cG9zdDo4OTE3"
                    }
                },
                {
                    "node": {
                        "title": "Le convoi de camions est en route pour Ottawa",
                        "slug": "le-convoi-de-camions-est-en-route-pour-ottawa",
                        "id": "cG9zdDo4OTA3"
                    }
                }
            ]
        }
    },
    "extensions": {
        "debug": [
            {
                "type": "DEBUG_LOGS_INACTIVE",
                "message": "GraphQL Debug logging is not active. To see debug logs, GRAPHQL_DEBUG must be enabled."
            }
        ]
    }
}

And here is my first validation test :

const schema = {
    type: 'object',
    proporties: {
        data: {
            type: 'object'
        },
        extensions: {
            type: 'object'
        }
    },
    required: ['data', 'extensions'],
    additionalProperties: false
}

pm.test('validate JSON Schema', function() {
    console.log('schema ', schema)
    // console.log('schema ', pm.response.to.have.jsonSchema(schema))
    pm.response.to.have.jsonSchema(schema);
});

I even tried a more complex validation but still got a error in my test:

const schema = {
    type: 'object',
    proporties: {
        data: {
            type: 'object',
            required: ['posts'],
            additionalProperties: false,
            proporties: {
                posts: {
                    type: 'object',
                    required: ['edges'],
                    additionalProperties: false,
                    proporties: {
                        edges: {
                            type: 'array',
                            "minItems": 1,
                            "items": {
                                type: 'object',
                                required: ['node'],
                                additionalProperties: false,
                                proporties: {
                                    node: {
                                        type: 'object',
                                        required: ['title', 'slug'],
                                        additionalProperties: false,
                                        proporties: {
                                            title : {
                                                type: 'string'
                                            },
                                            slug: {
                                                type: 'string'
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        },
        extensions: {
            type: 'object',
            required: ['debug'],
            additionalProperties: false,
            proporties: {
                debug: {
                    type: 'array',
                    "items": {
                        type: 'object',
                        required: ['type', 'message', 'id'],
                        additionalProperties: false,
                        proporties: {
                            type: {
                                type: 'string'
                            },
                            message: {
                                type: 'message'
                            },
                            id: {
                                type: 'string'
                            }
                        }
                    }
                }
            }
        }

    },
    required: ['data', 'extensions'],
    additionalProperties: false
}

pm.test('validate JSON Schema', function() {
    console.log('schema ', schema)
    // console.log('schema ', pm.response.to.have.jsonSchema(schema))
    pm.response.to.have.jsonSchema(schema);
});

When I remove the first additionalProperties . The test pass but if more fields gets applied It won’t fail.

Use this website to generate the schema.

JSON Schema Tool

You can continue as a guest (you don’t have to sign up).

This will generate a large schema including examples. You can go into the settings and turn off the examples and other elements that you don’t want included.

Remove the $schema and $id that it includes as that will cause the test to fail.

From there, you can then start removing the elements you don’t want if needed (or remove them via the settings and re-generate).

This is what it produced for the schema. I changed the first additionalProperties to false to test it and it appear to work as intended.

const schema =  {
    "type": "object",
    "required": [
        "data",
        "extensions"
    ],
    "additionalProperties": false,
    "properties": {
        "data": {
            "type": "object",
            "required": [
                "posts"
            ],
            "additionalProperties": true,
            "properties": {
                "posts": {
                    "type": "object",
                    "required": [
                        "edges"
                    ],
                    "additionalProperties": true,
                    "properties": {
                        "edges": {
                            "type": "array",
                            "additionalItems": true,
                            "items": {
                                "type": "object",
                                "required": [
                                    "node"
                                ],
                                "additionalProperties": true,
                                "properties": {
                                    "node": {
                                        "type": "object",
                                        "required": [
                                            "title",
                                            "slug",
                                            "id"
                                        ],
                                        "additionalProperties": true,
                                        "properties": {
                                            "title": {
                                                "type": "string"
                                            },
                                            "slug": {
                                                "type": "string"
                                            },
                                            "id": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        },
        "extensions": {
            "type": "object",
            "required": [
                "debug"
            ],
            "additionalProperties": true,
            "properties": {
                "debug": {
                    "type": "array",
                    "additionalItems": true,
                    "items": {
                        "type": "object",
                        "required": [
                            "type",
                            "message"
                        ],
                        "additionalProperties": true,
                        "properties": {
                            "type": {
                                "type": "string"
                            },
                            "message": {
                                "type": "string"
                            }
                        }
                    }
                }
            }
        }
    }
}

Thanks @michaelderekjones for the answer.

I did try it the site before. But It was not giving me the additionalProperties.

I compare your response and mine. And I found out my error. I used ‘proporties’ instead of ‘properties’.

Everything works now.

Thanks

Glad its working. In that site, when you go into the settings on the left hand pane, it has a lot of options including turning additionalProperties on or off.

It’s pretty cool, and has saved me a lot of grief in the past.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.