Confused by JSON-schema format

998 views
Skip to first unread message

cuthbert

unread,
Mar 19, 2015, 5:34:49 PM3/19/15
to json-...@googlegroups.com
Hi, I'm wondering if anyone could clarify something i'm stuck on.

i have some json i want to validate. it is in the following format :

{
  "fixedKey": {
    "1": {}
  }
}

In the object the key "fixedKey" will never change, it is hard coded in the structure. 

The key "1" I wish to validate, but it could be any numerical value so i've written the json schema below to check it's of the pattern [0-9]*

{
    "type": "object",
    "properties": {
        "fixedKey": {
            "type": "object",
            "patternProperties": {
                "[0-9]*": {}
            }
        }
    }
}

my problem is when testing here http://jsonschemalint.com/draft4/#

the json always passes even if the second key is not numeric. 

could someone point out where i've gone wrong please
thanks :-)

Mike Lorbetske

unread,
Mar 19, 2015, 6:04:04 PM3/19/15
to json-...@googlegroups.com
The schema you're using specifies that properties that match that regex must validate against the empty schema but does not say those are the only properties allowed. Add "additionalProperties": false as a peer to your patternProperties member to disallow undeclared property names.

Thanks,
Mike
Sent from my Windows Phone

From: cuthbert
Sent: ‎3/‎19/‎2015 2:34 PM
To: json-...@googlegroups.com
Subject: [json-schema] Confused by JSON-schema format

--
You received this message because you are subscribed to the Google Groups "JSON Schema" group.
To unsubscribe from this group and stop receiving emails from it, send an email to json-schema...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Geraint

unread,
Mar 20, 2015, 9:36:32 AM3/20/15
to json-...@googlegroups.com
In addition to "additionalProperties": false as Mike said, you should also fix your regular expression.

Firstly, you've used the asterisk "*", which means "zero or more".  You probably want plus "+", which means "one or more".

You should also put the start/end in there, because by default regular expressions are not anchored.

So: "^[0-9]+$" would match a series of decimal characters.  If you don't want leading zeros, you could use "^(0|[1-9][0-9]*)$".

cuthbert

unread,
Mar 21, 2015, 6:25:52 AM3/21/15
to json-...@googlegroups.com
thanks for your help guys but i stil can't get this to work...

i'm using this helper here: http://www.jsonschemavalidator.net/

using this schema it fails to compile

{
    "type": "object",
    "properties": {
        "fixedKey": {
            "type": "object",
            "properties": {
                "patternProperties": {
                    "^(0|[1-9][0-9]*)$": {}
                },
                "additionalProperties": false
            }
        }
    }
}

Unexpected token encountered when reading value for 'additionalProperties'. Expected StartObject, got Boolean. Path 'properties.fixedKey.properties.additionalProperties', line 10, position 46.

running the code against this https://github.com/mafintosh/is-my-json-valid doesn't complain but thinks my test json is ok when it should fail for not matching the regex

if i move out the "additionalProperties":false out to the next level that looks wrong as it rejects any value that is present

{
    "type": "object",
    "properties": {
        "fixedKey": {
            "type": "object",
            "properties": {
                "patternProperties": {
                    "^(0|[1-9][0-9]*)$": {}
                }
            },
            "additionalProperties": false
        }
    }
}

still confused...
Message has been deleted

Sebastian Lasse

unread,
Mar 21, 2015, 12:05:29 PM3/21/15
to json-...@googlegroups.com
Two bugs in your Schema !

  • The "additionalProperties" must be on the same 'level' than "patternProperties".
  • "patternProperties" is a replacement for "properties" (properties that are defined as patterns)

It should just read:

...

"patternProperties": {
    "^(0|[1-9][0-9]*)$": {}
},
"additionalProperties": false

...

Sebastian Lasse

unread,
Mar 21, 2015, 12:07:58 PM3/21/15
to json-...@googlegroups.com
Be sure to read the official Specifications ...

cuthbert

unread,
Mar 21, 2015, 12:47:20 PM3/21/15
to json-...@googlegroups.com
yes, I've tried that, it doesn't behave as expected in the tools indicated

xmlbuddy

unread,
Mar 22, 2015, 3:14:57 PM3/22/15
to json-...@googlegroups.com

Hi,

the following JSON schema works for your example validated with the standard Java JSON schema validator version 2.2.5 (Java lib available here):

Regards
Clemens
Reply all
Reply to author
Forward
0 new messages