tdegrunt / jsonschema

JSON Schema validation
Other
1.82k stars 262 forks source link

validation error reported onOf object incorrectly #382

Closed ClementMindflow closed 4 months ago

ClementMindflow commented 1 year ago

Hello !

When validating a schema containing a onOf where two of the items are similar, an error is reported even if the value match the expected format.

For example, validating the value { type: "json", value: "{\n \n}" } against this schema:

{
  title: "Test Schema",
  oneOf: [
    {
      $ref: "#/definitions/schema-A"
    },
    {
      $ref: "#/definitions/schema-B"
    }
  ],
  definitions: {
    "schema-A": {
      type: "object",
      required: ["type", "value"],
      properties: {
        type: {
          type: "string",
          default: "json"
        },
        value: {
          title: "JSON content",
          type: "string",
          default: "{\n \n}"
        }
      }
    },
    "schema-B": {
      type: "object",
      required: ["type", "value"],
      properties: {
        type: {
          type: "string",
          default: "text"
        },
        value: {
          title: "Text content",
          type: "string"
        }
      }
    }
  }
}

report the following error: is not exactly one from <#/definitions/schema-A>,<#/definitions/schema-B>

See this codepen for a reproduction scenario.

semicullen commented 1 year ago

I assume you mean oneOf, not onOff.

ninthclowd commented 7 months ago

I'm having the same issue. Getting: { "property": "instance", "message": "is not exactly one from \"TYPE A\",\"TYPE B"" } when the schema is defined as "oneOf": [...]

ClementMindflow commented 4 months ago

I came back here after a while... The issue did not came from the validation lib but from a misunderstanding about how oneOf were supposed to work. The spec say that the value should match one and exactly one schema in the list. And that care should be taken when crafting schemas inside a oneOf so they are not "structurally equivalent". Which is not the case here.

Closing this ticket.