Json validation with unevaluatedProperties

Having trouble installing Oxygen? Got a bug to report? Post it all here.
scottbdr
Posts: 57
Joined: Tue Jul 21, 2009 1:48 am

Json validation with unevaluatedProperties

Post by scottbdr »

Hi, I'm trying to create test instances for JSON schemas that specify either "unevaluatedProperties" or "additionalProperties" as false. When I attach a schema to the JSON instance being used to test the schemas, I get an error because the "$schema" property used to attach the schema is considered additional/unevaluated.

I'm sure you have run into this already - just wondering if there is any way around it - it seems a perfectly reasonable error, but one that is frustrating as well - I have many smaller schemas that build into a larger one, and I don't want to implement a validation scenario for every single one - my preference is to attach the schema to the instance.

Thanks, Scott
florin_nica
Posts: 38
Joined: Wed Sep 09, 2020 3:17 pm

Re: Json validation with unevaluatedProperties

Post by florin_nica »

Hi Scott,

I understand that this behavior can be frustrating when working with multiple schemas. I suggest adding

Code: Select all

"unevaluatedProperties": {"$schema": true}
in each schema, rather than using the boolean version

Code: Select all

"unevaluatedProperties": true
This way, you can explicitly allow the $schema property, which will help bypass the validation error.

Regards,
Florin
scottbdr
Posts: 57
Joined: Tue Jul 21, 2009 1:48 am

Re: Json validation with unevaluatedProperties

Post by scottbdr »

Not ideal, but it works! Thank you!
scottbdr
Posts: 57
Joined: Tue Jul 21, 2009 1:48 am

Re: Json validation with unevaluatedProperties

Post by scottbdr »

Actually... revisiting this, I don't think your suggestion works - I think it just sets unevaluatedProperties to "true" as it will allow $schema, but it also allows everything else. Thought I'd point this out since I saw this thread coming up in some AI results and I think folks should know it's not valid. For example, this schema:

Code: Select all

{
    "$schema": "http://json-schema.org/draft/2020-12/schema#",
    "type": "object",
    "properties": {
        "propertyName": {
            "type": "string"
        }
    },
    "unevaluatedProperties": {"$schema": true}
}
will validate this (rather than flagging "foo" and not flagging $schema):

Code: Select all

{
    "$schema": "test.jschema",
    "propertyName": "propertyName",
    "foo": "bar"
}
Not sure there is a way around this without just adding "$schema" as a property to your schemas, or Oxygen (and other validating editors) providing some software work-around.
florin_nica
Posts: 38
Joined: Wed Sep 09, 2020 3:17 pm

Re: Json validation with unevaluatedProperties

Post by florin_nica »

Hi, Scott,

You are correct, the proposed solution doesn't appear to be suitable. A potential workaround could involve using the patternProperties keyword to explicitly allow only the $schema property, as shown below:

Code: Select all

{
    "$schema": "http://json-schema.org/draft/2020-12/schema#",
    "type": "object",
    "properties": {
        "propertyName": {
            "type": "string"
        }
    },
    "patternProperties": {
        "^\\$schema$": {"type": "string"}
    },
    "unevaluatedProperties": false
}
Let me know if this approach works for you.

Regards,
Florin
Post Reply