1

First of all, I am not getting a proper error reponse on the web platform as well (https://jsonschemalint.com). I am using jsonschema in python, and have a proper json schema and json data that works.

The problem I'd like to solve is the following: Before we deliver JSON files with example data, we need to run them through SoapUI to test if they are proper, as we are dealing with huge files and usually our devs may make some errors in generating them, so we do the final check.

I'd like to create a script to automate this, avoiding SoapUI. So after googling, I came across jsonschema, and tried to use it. I get all the proper results,etc, I get errors when I delete certain elements as usual, but the biggest issues are the following:

Example : I have a subsubsub object in my JSON schema, let's call it Test1, which contains the following :

**Schema**
    {
   "exname":"2",
   "info":{},
   "consumes":{},
   "produces":{},
   "schemes":{},
   "tags":{},
   "parameters":{},
   "paths":{},
   "definitions":{
      "MainTest1":{
         "description":"",
         "minProperties":1,
         "properties":{
            "test1":{
               "items":{
                  "$ref":"#//Test1"
               },
               "maxItems":10,
               "minItems":1,
               "type":"array"
            },
            "test2":{
               "items":{
                  "$ref":"#//"
               },
               "maxItems":10,
               "minItems":1,
               "type":"array"
            }
         }
      },
      "Test1":{
         "description":"test1des",
         "minProperties":1,
         "properties":{
            "prop1":{
               "description":"prop1des",
               "example":"prop1exam",
               "maxLength":10,
               "minLength":2,
               "type":"string"
            },
            "prop2":{
               "description":"prop2des",
               "example":"prop2example",
               "maxLength":200,
               "minLength":2,
               "type":"string"
            },
            "prop3":{
               "enum":[
                  "enum1",
                  "enum2",
                  "enum3"
               ],
               "example":"enum1",
               "type":"string"
            }
         },
         "required":[
            "prop3"
         ],
         "type":"object"
      }
   }
}

    **Proper example for Test1** 
    {
    "Test1": [{
        "prop1": "TestStr",
        "prop2": "Test and Test",
        "prop3": "enum1"
    }]
    }

    **Improper example that still passes validation for Test1** 
    {
    "test1": [{
        "prop1": "TestStr123456", [wrong as it passes the max limit]
        "prop2": "Test and Test",
        "prop3": " enum1" [wrong as it has a whitespace char before enum1]
    }]
    }

The first issue I ran across is that enum in prop3 isn't validated correctly. So, when I use " enum1" or "enumruwehrqweur" or "literally anything", the tests pass. In addition, that min-max characters do not get checked throughout my JSON. No matter how many characters I use in any field, I do not get an error. Anyone has any idea how to fix this, or has anyone found a better workaround to do what I would like to do? Thank you in advance!

5
  • it will be great to add a code snippet with false-positive example check Commented Aug 31, 2018 at 8:29
  • @AzatIbrakov added Json examples. You can take a look directly at jsonschemalint.com which uses the same functionlity like the Python Lib I am using. It doesn't show errors as well, which makes me think that there must be a lib bug or inconsistency. Commented Aug 31, 2018 at 8:41
  • Before I answer, is this a true representation of your schema? I ask because you have a property at the root level "Test1". This does nothing, and needs to be under a "properties" keyed object, as you've done within that object. Commented Aug 31, 2018 at 8:48
  • @Relequestual Thanks for the question as that will further clarify things ( I am still pretty new to working with JSONs). I will add a more clear representation asap. Commented Aug 31, 2018 at 8:56
  • No problem. I am working on correcting your schema. There are a few issues. It's OK =] Commented Aug 31, 2018 at 8:57

1 Answer 1

1

There were a few issues with your schema. I'll address each of them.

In your schema, you have "Test1". In your JSON instance, you have "test1". Case is important. I would guess this is just an error in creating your example.

In your schema, you have "Test1" at the root level. Because this is not a schema key word, it is ignored, and has no effect on validation. You need to nest it inside a "properties" object, as you have done elsewhere.

{
  "properties": {
    "test1": {

Your validation would still not work correctly. If you want to validate each item in an array, you need to use the items keyword.

{
  "properties": {
    "test1": {
      "items": {
        "description": "test1des",

Finally, you'll need to nest the required and type key words inside the items object.

Here's the complete schema:

{
  "properties": {
    "test1": {
      "items": {
        "description": "test1des",
        "minProperties": 1,
        "properties": {
          "prop1": {
            "description": "prop1des",
            "example": "prop1exam",
            "maxLength": 10,
            "minLength": 2,
            "type": "string"
          },
          "prop2": {
            "description": "prop2des",
            "example": "prop2example",
            "maxLength": 200,
            "minLength": 2,
            "type": "string"
          },
          "prop3": {
            "enum": [
              "enum1",
              "enum2",
              "enum3"
            ],
            "example": "enum1",
            "type": "string"
          }
        },
        "required": [
          "prop3"
        ],
        "type": "object"
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your constructive comments! I have updated my schema to try to showcase what the original is. We use different schemas, with multiple thousands of lines, and are always validated, so I am pretty sure their structure is ok, it is just hard to transfer them here without compromising something, so that's why I have these errors. I am still trying to see if the enum will give error messages with pretty simple examples, but can't achieve it. What you mentioned about "items" though may be the actual issue, as it is missing in the ref fields in our schemas.
Try using jsonschemavalidator.net to validate your schema. Please modify your question to include a complete and valid schema, otherwise it's really difficult for me to guess what the issues are. Currently that schema you've provided does nothing, because you have no validation key words at the root level.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.