3

Hope I might find some help with this validation issue: I have a JSON array that can have multiple object types (video, image). Within that array, the objects have a rel field value. The case I'm working on is that there can only be one object with "rel": "primaryMedia" allowed — either a video or an image.

Here are the object representations for the image and video case, both showing the "rel": "primaryMedia".

{
  "media": [
    {
      "caption": "Caption goes here",
      "id": "ncim87659842",
      "rel": "primaryMedia",
      "type": "image"
    },
    {
      "description": "Shaima Swileh arrived in San Francisco after fighting for 17 months to get a waiver from the U.S. government to be allowed into the country to visit her son.",
      "headline": "Yemeni mother arrives in U.S. to be with dying 2-year-old son",
      "id": "mmvo1402810947621",
      "rel": "primaryMedia",
      "type": "video"
    }
  ]
}

Here's a stripped-down version of the schema I created to validate this using oneOf (assuming this will handle my case). It doesn't however, work as intended.

{
  "$id": "http://example.com/schema/rockcms/article.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {},
  "properties": {
    "media": {
      "items": {
        "oneOf": [
          {
            "additionalProperties": false,
            "properties": {
              "caption": {
                "type": "string"
              },
              "id": {
                "type": "string"
              },
              "rel": {
                "enum": [
                  "primaryMedia"
                ],
                "type": "string"
              },
              "type": {
                "enum": [
                  "image"
                ],
                "type": "string"
              }
            },
            "required": [
              "caption",
              "id",
              "rel",
              "type"
            ],
            "type": "object"
          },
          {
            "additionalProperties": false,
            "properties": {
              "description": {
                "type": "string"
              },
              "headline": {
                "type": "string"
              },
              "id": {
                "type": "string"
              },
              "rel": {
                "enum": [
                  "primaryMedia"
                ],
                "type": "string"
              },
              "type": {
                "enum": [
                  "video"
                ],
                "type": "string"
              }
            },
            "required": [
              "description",
              "headline",
              "id",
              "rel",
              "type"
            ],
            "type": "object"
          }
        ]
      },
      "type": "array"
    }
  }
}

Using the JSON Schema validator at https://www.jsonschemavalidator.net, the schema validates when data is correctly presented, but the doesn't work when trying to catch errors.

In the case below, headline is added for a video, and it's missing id. This should fail because headline isn't allowed on video, and id is required.

{ 
  "media": [
    {
      "headline": "Yemeni mother arrives in U.S. to be with dying 2-year-old son",
      "rel": "primaryMedia",
      "type": "video"
    }
  ]
}

The results I get from the validator, however, aren't entirely expected. Seems it's conflating the two object schemas in its response.

In addition to this, I've separately found that the schema will allow the population of BOTH a video and image object in media, which isn't expected.

Been trying to figure out what I've done wrong, but am stumped. Would very much appreciate some feedback, if anyone has to offer. Thanks in advance!


Validator Response:

Message: JSON is valid against no schemas from 'oneOf'. Schema path: #/properties/media/items/oneOf

Message: Property 'headline' has not been defined and the schema does not allow additional properties. Schema path: #/properties/media/items/oneOf/0/additionalProperties

Message: Value "video" is not defined in enum. Schema path: #/properties/media/items/oneOf/0/properties/type/enum

Message: Required properties are missing from object: description, id. Schema path: #/properties/media/items/oneOf/1/required

Message: Required properties are missing from object: caption, id. Schema path: #/properties/media/items/oneOf/0/required

9
  • Hi Anthony. I see no issues with what you've presented and explained. Can you explain what you EXPECT the error output to be? You should note that currently there's no defined standard error reporting format, so it may very well be quite different for each library you use. (In the next version of JSON Schema (draft-8) we are including a number of standard output formats.) Commented Feb 5, 2019 at 13:33
  • When you say "In addition to this, I've separately found that the schema will allow the population of BOTH a video and image object in media, which isn't expected.", do you mean, you're expecting ALL the objects to satisfy only ONE of the oneOf values? Commented Feb 5, 2019 at 13:34
  • OK, I think I've understood, you have three questions/issues here... 1. I want only one object in the array to be the primary media. 2. The validation ouput is not what I expect. 3. [whatever the outcome of the previous comment is]. Is that correct? Commented Feb 5, 2019 at 13:40
  • @Relequestual Thanks for the follow-up! Let me try and answer your questions: (1) I would expect, in the error case above, to not see the error given for the video ENUM issue — video is, in fact, an allowed enum. Agree that the other errors are likely valid otherwise. Commented Feb 5, 2019 at 13:45
  • @Relequestual What I want to allow for is that in media you can only have ONE object whose rel field value is "primaryMedia". That object can either be an image OR video. Commented Feb 5, 2019 at 13:48

1 Answer 1

1

Your question is formed of three parts, but the first two are linked, so I'll address those, although they don't have a "solution" as such.

  1. How can I validate that only one object in an array has a specific key, and the others do not.

You cannot do this with JSON Schema. The set of JSON Schema keywords which are applicable to arrays do not have a means for expressing "one of the values must match a schema", but rather are applicable to either ALL of the items in a the array, or A SPECIFIC item in the array (if items is an array as opposed to an object). https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.4

  1. The validation output is not what I expect. I expect to only see the failing branch of oneOf which relates to my object type, which is defined by the type key.

The JSON Schema specification (as of draft-7) does not specify any format for returning errors, however the error structure you get is pretty "complete" in terms of what it's telling you (and is similar to how we are specifying errors should be returned for draft-8).

Consider, the validator knows nothing about your schema or your JSON instance in terms of your business logic. When validating a JSON instance, a validator may step through all values, and test validation rules against all applicable subschemas. Looking at your errors, both of the schemas in oneOf are applicable to all items in your array, and so all are tested for validation. If one does not satisfy the condition, the others will be tested also. The validator cannot know, when using a oneOf, what your intent was.

You MAY be able to get around this issue, by using an if / then combination. If your if schema is simply a const of the type, and your then schema is the full object type, you may get a cleaner error response, but I haven't tested this theory.

  1. I want ALL items in the array to be one of the types. What's going on here?

From the spec...

items:

If "items" is a schema, validation succeeds if all elements in the array successfully validate against that schema.

oneOf:

An instance validates successfully against this keyword if it
validates successfully against exactly one schema defined by this
keyword's value.

What you have done is say: Each item in the array should be valid according to [schemaA]. SchemA: The object should be valid according to on of these schemas: [the schemas inside the oneOf].

I can see how this is confusing when you think "items must be one of the following", but items applies the value schema to each of the items in the array independantly.

To make it do what you mean, move the oneOf above items, and then refactor one of the media types to another items schema.

Here's a sudo schema.

oneOf: [items: properties: type: const: image], [items: properties: type: image]

Let me know if any of this isn't clear or you have any follow up questions.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the feedback! I'll have a go at this. Really appreciate the time putting this together.
Did this solve your problem? If so, consider marking as the accepted answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.