Adding ref path to required in if/then [json] - json

Please I need help, I have a large file which I need to validate some data. For fields under same properties it is working using code below. But I also have fields which are not under the same "properties".
"if": {
"properties": {
"type": { "const": "one" }
}
},
"then": { "required": ["test"] } }
So, what I'm doing is adding a reference path to the required statement. I tried this
"then": { "required": {"$ref" : "#/path/example" }}
but it didn't work. Any ideas if I can use reference path inside required and if yes how?

I think what you are trying to express is that if /type is "one" then /path/example is required.
So, these should be valid:
{
"type": "one",
"path": { "example": 42 }
}
{
"type": "two",
"path": { "foo": 42 }
}
And, this should not be valid:
{
"type": "one",
"path": { "foo": 42 }
}
Here's how we can do that:
"if": {
"properties": {
"type": { "const": "one" }
}
},
"then": {
"properties": {
"path": { "required": ["example"] }
},
"required": ["path"]
}
You need to establish the path using properties (potentially several nested properties if you need to go more than two levels) and use required at each level.
(If I'm still no understanding your question, let me know and we can try again)

Related

How to write complex conditional validation in JSON schema?

Below is the sample JSON schema.
I need to implement this validation -
If property1 has a value of "abc" and property2 has "abc111" in the list of provided values, then subProperty1 is mandatory inside property3
{
"type": "object",
"additionalProperties": false,
"properties": {
"property1": {
"type": "string"
},
"property2": {
"type": "array",
"items": {
"type": "string"
}
},
"property3": {
"type": "object",
"properties": {
"subProperty1": {
...
},
"subProperty2": {
...
},
}
}
}
}
I tried to implement it using this JSON -
"if": {
"properties": {
"property1": {
"const": "abc"
},
"property2": {
"contains": {
"properties": {
"items": {
"const": "abc111"
}
}
}
}
}
},
"then": {
"dependentSchemas": {
"property1": {
"required": [ "property3" ],
"message": "Error - Missing property3 with subProperty1",
"properties": {
"property3": {
"required": [ "subProperty1" ]
}
}
}
}
}
But this doesnt work as expected. Could someone please help me fix the conditional logic?
It's hard to say without knowing in what way things aren't working as expected. But you may be missing a required keyword in your if clause -- in order to ensure that property1 really is present. (The properties keyword evaluates to true if the property isn't present at all.) Then you would also need a required: ["property3"] in your then clause.
Also, you don't need dependentSchemas at all -- you are adding a dependency on property1, but that's already in the if clause so it's redundant.

Cannot validate recursive schema

I'm stuck on writing a correct json schema for my data.
Condition:
directory is an recursive object with optional property "$meta"
file can be string or object
if file is object it can have optional property "$meta" with oneOf "$data" or "$stringData" not both.
JSON Data:
{
"dir1": {
"dir1A": {},
"dir1B": {
"dir1B01": {
"dir1B0101": {},
"dir1B0102": {},
"dir1B0103": {},
"file1B.txt": {
"$meta": {},
"$data": "ooo",
"$stringData": "Netus et malesuada" // here should failed
}
}
}
},
"dir2": {
"file2.txt": "dolor sit amet" // here should OK but failed
},
"file1.txt": "Lorem Ipsum"
}
Schema:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/dir.schema.json",
"type": "object",
"patternProperties": {
"^[^\\/?%*:|\"<>]+$": { // failed if name consist of invalid chars
"$ref": "#/$defs/directory"
}
},
"additionalProperties": false,
"$defs": {
"file": {
"oneOf": [
{
"type": "string"
},
{
"type": "object",
"properties": {
"$data": {
"type": "string",
"description": "base64 encoded data"
},
"$stringData": {
"type": "string"
}
},
"oneOf": [
{
"not": {
"anyOf": [
{
"required": [
"$data",
"$stringData"
]
}
]
}
},
{
"allOf": [
{
"not": {
"required": [
"$data"
]
}
},
{
"not": {
"required": [
"$stringData"
]
}
}
]
}
]
}
]
},
"directory": {
"anyOf": [
{
"$ref": "#/$defs/file"
},
{
"type": "object",
"properties": {
"$meta": {
"type": "object",
"additionalProperties": true,
"properties": {
"createdAt": {
"type": "string"
},
"size": {
"type": "integer"
},
}
}
},
"patternProperties": {
"^[^\\/?%*:|\"<>]+$": {
"$ref": "#"
}
}
}
]
}
}
}
Live Demo: https://www.jsonschemavalidator.net/s/HNFtNfRw
Thank you for help
There are a few issues, so let's walk through deriving this schema one step at a time.
Let's start with the object representation of a file. Here's the easy part.
{
"type": "object",
"properties": {
"$meta": { "$ref": "#/$defs/meta" },
"$data": { "type": "string" },
"$stringData": { "type": "string" }
}
}
Then we need to add the assertion that one and only one of $data or $stringData is required. The oneOf keyword can express this cleanly.
"oneOf": [
{ "required": ["$data"] },
{ "required": ["$stringData"] }
]
Only one of the schemas can validate to true. If both properties are present, the schema will fail.
Next, let's define a file can be represented as a string or an object.
{
"anyOf": [
{ "type": "string" },
{ "$ref": "#/$defs/object-file" }
]
}
Notice that we are using anyOf instead of oneOf in this case. anyOf passes validation as soon as one of the schemas is valid. If the first one passes validation, it doesn't need to check the second one. If oneOf is used, the validator needs to check both schemas because oneOf requires that only one of the schemas passes validation and the rest fail. Because one of these schemas is a string and the other is an object, it's impossible for any JSON data to be both a string an object. Therefore, using oneOf only makes the validation less efficient.
That covers files, let's do directories next.
{
"type": "object",
"properties": {
"$meta": { "$ref": "#/$defs/meta" }
},
"patternProperties": {
"^[^\\/?%*:|\"<>]+$": { "$ref": "#/$defs/file-or-directory" }
},
"additionalProperties": false
}
The recursive reference part is pretty simple, but we have a problem with the $meta property. $meta matches the regular expression in patternProperties, so $meta will need to validate against #/$defs/meta and #/$defs/file-or-directory, which is not what you want. What you need is something like an additionalPatternProperties keyword, but since that doesn't exist, you need to change your patternProperties regex to exclude $meta. Something like this, "^[^\\/?%*:|\"<>]+(?<!\\$meta)$".
Now all that's left is a schema to express that something is a file or a directory.
"anyOf": [
{ "$ref": "#/$defs/file" },
{ "$ref": "#/$defs/directory" }
]
Again, we want to use anyOf, but there's a bit more to this than you might notice at first glance. The problem is that something that matches the file schema will also match the directory schema. So, we can't use oneOf and the file schema needs to come first. If the directory schema was first, it would incorrectly validate files as directories. When the file schema comes first, it checks if it's a file first and the false positive that we would get from the directory schema becomes irrelevant.
I'll leave it to you to put the pieces together into a full schema, but leave a comment if you have any issues.

Conditionally apply sub-schema, based on another file's property

Does json-schema allow to have conditional sub-schema based on the property value from another file?
Example:
Jsons:
another.json
{
"honey": "honey"
}
main.json
{
"hello": "abc"
}
Schema
{
"type": "object",
"properties": {
"hello": {
"type": "string"
}
},
"required": ["hello"],
"if": {
"properties": {
"another.json#/honey": { -->> is this possible
"const": "honey"
}
}
},
"then": {
"properties": {
"hi": {
"type": "string"
}
},
"required": ["hi"]
}
}
The main.json should fail validation screaming "hi" is required since, another.json has "honey" as value.
Now, changing another.json to
{
"honey": "not_honey"
}
Should pass the validation for main.json
The question is
No. JSON Schema has no notion of files, only "a JSON instance", of which only one can be validated at a time. You would need to combine them yourself in some way.

Is it possible to create a JSON Schema with allOf (multiple if and then) and $ref?

I am trying to create a complex schema that will check for the value of a property and then validate according to the value of that same property. I am wondering if it's possible to use $ref and allOf in the same schema and if so, how? I am having some trouble getting this to work. It may be important to note that I am using AJV. Please see my code below
{
"$ref": "#/definitions/Welcome",
"definitions": {
"Welcome": {
"properties": {
"auth": {
"type": "string",
"enum": ["oauth1","oauth2"]
},
"environment": {
"$ref": "#/definitions/Environment"
}
}
},
"Environment": {
"properties": {
"dev": {
"type": "object"
}
}
},
"Oauth1": {
"type": "object",
"properties": {
"temporary_credentials": {
"type": "string"
}
}
},
"Oauth2": {
"type": "object",
"properties": {
"auth_url": {
"type": "string"
}
}
}
},
"allOf": [
{
"if": {
"auth": {
"const": "oauth1"
}
},
"then": {
"environment": {
"dev": {
"$ref": "#/definitions/Oauth1
}
}
}
},
{
"if": {
"auth": {
"const": "oauth2"
}
},
"then": {
"environment": {
"dev": {
"$ref": "#/definitions/Oauth2
}
}
}
}
]
}
A sample json input to be validated against this schema would be something like this
{
"auth": "oauth1",
"environment": {
"dev": {
"temporary_credentials": "xyzzy"
}
}
}
I feel like there might be an error in my "then" statements or simply the placement of the allOf. The error I would get is something like this "$ref: keywords ignored in schema at path "#"".
In schema version up to and including draft7, once you use "$ref", all other keywords in that level of the schema are ignored. That's what the error is telling you: because you used $ref, other keywords are ignored.
If you only want to use a $ref at the root level, the trick is to wrap it in an "allOf".
But since you already have an allOf at the root level, you can just add the $ref as another branch of the allOf and it will work.
That would look like:
"allOf": [
{
"$ref": "#/definitions/Welcome",
},
{
"if": {
"auth": {
"const": "oauth1"
}
etc.
Note: in the schema you posted, you have two unclosed strings "#/definitions/Oauth1 and "#/definitions/Oauth2. If you had that in your real schema it would be invalid JSON.

Use conditional statements on json schema based on another schema object

I have a json object like:
{
"session": {
"session_id": "A",
"start_timestamp": 1535619633301
},
"sdk": {
"name": "android",
"version": "21"
}
}
The sdk name can either be android or ios. And the session_id is based on name field in sdk json. I have written a json schema using conditional statement (Using draft 7) as follows:
But it works in an unexpected manner:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "#/definitions/Base",
"definitions": {
"Base": {
"type": "object",
"additionalProperties": false,
"properties": {
"session": {
"$ref": "#/definitions/Session"
},
"sdk": {
"$ref": "#/definitions/SDK"
}
},
"title": "Base"
},
"Session": {
"type": "object",
"additionalProperties": false,
"properties": {
"start_timestamp": {
"type": "integer",
"minimum": 0
},
"session_id": {
"type": "string",
"if": {
"SDK": {
"properties": {
"name": {
"enum": "ios"
}
}
}
},
"then": {
"pattern": "A"
},
"else": {
"pattern": "B"
}
}
},
"required": [
"session_id",
"start_timestamp"
],
"title": "Session"
},
"SDK": {
"type": "object",
"additionalProperties": false,
"properties": {
"version": {
"type": "string"
},
"name": {
"type": "string",
"enum": [
"ios",
"android"
]
}
},
"required": [
"name",
"version"
],
"title": "SDK"
}
}
}
So the following JSON Passes:
{
"session": {
"session_id": "A",
"start_timestamp": 1535619633301
},
"sdk": {
"name": "ios",
"version": "21"
}
}
But this fails:
{
"session": {
"session_id": "B",
"start_timestamp": 1535619633301
},
"sdk": {
"name": "android",
"version": "21"
}
}
can someone explain y?.. Even this passes:
{
"session": {
"session_id": "A",
"start_timestamp": 1535619633301
},
"sdk": {
"name": "android",
"version": "21"
}
}
I think you're having a similar problem as in this question.
#Relequestual is right in that you need the properties keyword around your SDK callout. But for what you want to do, you need to reorganize.
Subschemas only operate on their level in the instance, not at the root.
Consider this schema for a simple JSON object instance containing a one and a two property:
{
"properties": {
"one": {
"enum": ["yes", "no", "maybe"]
},
"two": {
"if": {
"properties": {
"one": {"const": "yes"}
}
},
"then": {
... // do some assertions on the two property here
},
"else": {
...
}
}
}
}
The if keyword under the two property can only consider the portion of the instance under the two property (i.e. two's value). It's not looking at the root of the instance, so it can't see the one property at all.
To make it so that the subschema under the two property subschema can see the one property in the instance, you have to move the if outside of the properties keyword.
{
"if": {
"properties": {
"one": {"const" : "yes"}
}
},
"then": {
... // do some assertions on the two property here
},
"else": {
... // assert two here, or have another if/then/else structure to test the one property some more
}
}
For two possible values of one, this is pretty good. Even three possible values isn't bad. However, as the possible values of one increases, so does the nesting of ifs, which can make your schema horrible to read (and possibly make validation slower).
Instead of using the if/then/else construct, I suggest using an anyOf or oneOf where each subschema represents a valid state for the instance, given the varying values of one.
{
"oneOf": [
{
"properties": {
"one": {"const": "yes"},
"two": ... // do some assertions on the two property here
}
},
{
"properties": {
"one": {"const": "no"},
"two": ... // do some assertions on the two property here
}
},
{
"properties": {
"one": {"const": "maybe"},
"two": ... // do some assertions on the two property here
}
}
]
}
This is much cleaner in my opinion.
Hopefully that explanation helps you reconstruct your schema to allow those other instances to pass.
You have to move your conditional to a high enough level to be able to reference all of the the properties it needs to reference. In this case, that's the /definitions/Base schema. Then you just need to write your schemas properly as Relequestual explained.
{
"$ref": "#/definitions/Base",
"definitions": {
"Base": {
"type": "object",
"properties": {
"session": { "$ref": "#/definitions/Session" },
"sdk": { "$ref": "#/definitions/SDK" }
},
"allOf": [
{
"if": {
"properties": {
"sdk": {
"properties": {
"name": { "const": "ios" }
}
}
},
"required": ["sdk"]
},
"then": {
"properties": {
"session": {
"properties": {
"session_id": { "pattern": "A" }
}
}
}
},
"else": {
"properties": {
"session": {
"properties": {
"session_id": { "pattern": "B" }
}
}
}
}
}
]
},
...
}
The value of if must be a JSON Schema. If you were to take lines https://gist.github.com/Relequestual/f225c34f6becba09a2bcaa66205f47f3#file-schema-json-L29-L35 (29-35) and use that as a JSON Schema by itself, you would impose no validation constraints, because there are no JSON Schema key words at the top level of the object.
{
"SDK": {
"properties": {
"name": {
"enum": "ios"
}
}
}
}
This is allowed in the specification, because people may want to extend the functionality of JSON Schema by adding their own key words. So it's "valid" JSON Schema, but doesn't actually DO anything.
You Need to add properties to the schema for it to make sense.
{
"properties": {
"SDK": {
"properties": {
"name": {
"const": "ios"
}
}
}
}
}
Additionally, enum must be an array. When you only have a single item, you may use const.