asyrjasalo / RESTinstance

Robot Framework library for RESTful JSON APIs
https://pypi.org/project/RESTinstance
GNU Lesser General Public License v3.0
205 stars 84 forks source link

Add copy paste examples for JSON schema validation #68

Closed heilaaks closed 2 years ago

heilaaks commented 5 years ago

Description

This is a proposal how to improve documentation examples. As a newbie with Robot and JSON schema validation, I was struggling a bit with the JSON schema validation with RESTinstance. I had difficulties to understand that some of the JSON schema keywords are in the Robot command and some in the schema itself. This is in the documentation, but as a newcomer I tend to read and implement first based on examples.

The second thing I am not sure if I have understood correctly is the use case for the schema building in the background by the RESTinstance. I would assume that the use case is an existing REST API server that does not have Robot tests? In this case it is possible to build tests and construct the JSON schema in the background without doing any changes to the server. It is possible to run run tests without validation since the existing functionality is recorded. Eventually there is a well defined JSON schema made by RESTinstance that allows validating the response from the server when refactoring and new features are done. But for a new implementation, there is anyways a need to build the API validation with schema validation in order to be able to develop the server. So in the later case the background schema is a bit redundant?

Feel free to modify the example anyhow you please. It is made mainly to illustrate the problem I had.

So far the library seems good. Thank you.

Example: Schema validation for JSON array

Validating JSON list against JSON schema with the RESTinstance can be done with JSON Schema validation keywords for array like items and contains. The list validation is useful for arrays of arbitrary length arrays where each item matches the same schema [1]. The JSON schema definition below, can be constructed to a Robot keyword like in the example below.

[1] https://json-schema.org/understanding-json-schema/reference/array.html

events = {
    "type": "object",
    "required": ["eventId", "name"],
    "properties": {
        "eventId": {
            "type": "string"
        },
        "name": {
            "type": "string"
        }
    }
}

# Example how the schema is built to test with JSON schema lints
schema = {
    "type": "array",
    "minItems": 0,
    "maxItems": 100,
    "items": events,
}

*** Test Cases ***
Event monitoring
    GET          ${basepath}/resources
    Array        response body data 0 attributes events  minItems=0  maxItems=100  items=${events}
Atihinen commented 3 years ago

I think we can merge this with #67

Atihinen commented 2 years ago

We've added #101 to milestone 1.3.0. I'll close this one. If you have any comments please use issue #101