Page 1 of 1
Json validation with unevaluatedProperties
Posted: Fri Oct 18, 2024 12:54 am
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
Re: Json validation with unevaluatedProperties
Posted: Fri Oct 18, 2024 2:46 pm
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
This way, you can explicitly allow the
$schema property, which will help bypass the validation error.
Regards,
Florin
Re: Json validation with unevaluatedProperties
Posted: Tue Oct 22, 2024 1:50 am
by scottbdr
Not ideal, but it works! Thank you!
Re: Json validation with unevaluatedProperties
Posted: Wed Nov 20, 2024 11:46 pm
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.
Re: Json validation with unevaluatedProperties
Posted: Thu Nov 21, 2024 3:47 pm
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