Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Supported discriminator in oneOf with nested allOf #56

Open
mefellows opened this issue Aug 13, 2024 · 0 comments
Open

Supported discriminator in oneOf with nested allOf #56

mefellows opened this issue Aug 13, 2024 · 0 comments
Labels
bug Something isn't working enhancement New feature or request

Comments

@mefellows
Copy link
Contributor

Background

Ajv does not support oneOf(allOf(...), allOf(...))

See ajv/lib/vocabularies/discriminator/index.ts

The documented limitation is here: https://ajv.js.org/json-schema.html#discriminator:

each oneOf subschema must have properties keyword with discriminator property. The subschemas should be either inlined or included as direct references (only $ref keyword without any extra keywords is allowed).

Workaround

A workaround is to flatten the inner allOf()s.

Repro

Below is effectively the code that gets executed. You can run this file directly in Node.js.

import Ajv from "ajv/dist/2019.js";

const payload = {
  type: "ADD_EVENT",
  payload: {
    events: [
      {
        type: "EVENT_TYPE_A",
        payload: {
          originator: "ORIGINATOR_A",
        },
      },
    ],
  },
};

const originalSchema = {
  type: "object",
  properties: {
    type: {
      const: "ADD_EVENT",
    },
    payload: {
      type: "object",
      properties: {
        events: {
          type: "array",
          items: {
            type: "object",
            properties: {
              type: {
                const: "EVENT_TYPE_A",
              },
              payload: {
                type: "object",
                oneOf: [
                  {
                    type: "object",
                    allOf: [
                      {
                        type: "object",
                        properties: {
                          originator: {
                            const: "ORIGINATOR_A",
                          },
                        },
                        required: ["originator"],
                      },
                    ],
                  },
                  {
                    type: "object",
                    allOf: [
                      {
                        type: "object",
                        properties: {
                          originator: {
                            const: "ORIGINATOR_B",
                          },
                        },
                        required: ["originator"],
                      },
                    ],
                  },
                ],
                discriminator: {
                  propertyName: "originator",
                },
              },
            },
            required: ["type", "payload"],
          },
        },
      },
    },
  },
};

const modifiedSchema = {
  type: "object",
  properties: {
    type: {
      const: "ADD_EVENT",
    },
    payload: {
      type: "object",
      properties: {
        events: {
          type: "array",
          items: {
            type: "object",
            properties: {
              type: {
                const: "EVENT_TYPE_A",
              },
              payload: {
                type: "object",
                oneOf: [
                  {
                    type: "object",
                    properties: {
                      originator: {
                        const: "ORIGINATOR_A",
                      },
                    },
                    required: ["originator"],
                  },
                  {
                    type: "object",
                    properties: {
                      originator: {
                        const: "ORIGINATOR_B",
                      },
                    },
                    required: ["originator"],
                  },
                ],
                discriminator: {
                  propertyName: "originator",
                },
              },
            },
            required: ["type", "payload"],
          },
        },
      },
    },
  },
};

const ajv = new Ajv({
  allErrors: true,
  discriminator: true,
  strictSchema: false,
});
ajv.validate(modifiedSchema, payload); // this is ok
ajv.validate(originalSchema, payload); // this throws
@mefellows mefellows added bug Something isn't working enhancement New feature or request labels Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant