question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

minProperties constraint ignored when using definitions/references in JSON schema

See original GitHub issue

I have a JSON schema that defines a type language_object with some properties and then uses that on one of the fields, along with a minProperties constraint.

However, when validating against this schema, submitting an empty object in said field does not raise a ValidationException. It appears that the minProperties constraint is ignored when it is placed alongside $ref

I have prepared a schema that defines my field once as name using references and once as nameEx with explicit properties. As far as I can tell these should be equivalent:

{
    "type":"object",
    "$schema":"http://json-schema.org/draft-04/schema#",
    "definitions":{
        "language_object":{
            "type":"object",
            "additionalProperties":false,
            "patternProperties":{
                "^[a-z]{2}$":{
                    "type":"string"
                }
            }
        }
    },
    "properties":{
        "name":{
            "$ref":"#/definitions/language_object",
            "minProperties":1
        },
        "nameEx":{
            "type":"object",
            "patternProperties":{
                "^[a-z]{2}$":{
                    "type":"string"
                }
            },
            "additionalProperties":false,
            "minProperties":1
        },
        "date":{
            "type":"string",
            "format":"date-time"
        }
    }
}

When I validate the following JSON

{
   "name":{},
    "nameEx":{}
}

I only get #/nameEx: minimum size: [1], found: [0], not both. Other situations, like this disallowed key/property work fine on both:

{
   "name":{"nbb":"bla"},
   "nameEx":{"nbb":"bla"}
}

Returns

#: 2 schema violations found
#/nameEx: extraneous key [nbb] is not permitted
#/name: extraneous key [nbb] is not permitted

Thank you for your patience!

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Antares42commented, Mar 16, 2016

Thanks, I followed the discussions in the comments. You are awesome.

0reactions
erosbcommented, Mar 15, 2016

@Antares42 version 1.2.0 is available on maven central with the bugfix (also with “format” support)

Read more comments on GitHub >

github_iconTop Results From Across the Web

object — Understanding JSON Schema 2020-12 documentation
Any property that doesn't match any of the property names in the properties keyword is ignored by this keyword. Note. See Additional Properties...
Read more >
Apply addtional constraints to a refered definition in JSON ...
Is there any way I can apply the constraint directly without copy&paste the content of the validType ? Schema: { "type": "object", "definition": ......
Read more >
JSON Schema vs. OpenAPI - codeburst
The number of properties on an object can be restricted using the minProperties and maxProperties keywords. Each of these must be a non-negative ......
Read more >
Min Properties - Liquid Technologies
If the schema is not allowed to contain an object the constraint is ignored, and not shown in the property grid. The constraint...
Read more >
Object type | Opis JSON Schema
objects representing valid json schemas, and the whole object must match the entire schema. * Starting with draft 2019-09 you should use dependentSchemas...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found