Skip to content

JSONSchemaType not matching Typescript Type on oneOf with objects #2567

@ldrick

Description

@ldrick

What version of Ajv are you using? Does the issue happen if you use the latest version?
8.17.1 - currently latest available version

Ajv options object

// not needed, just Typescript Types

JSON Schema

{
    "type": "object",
    "discriminator": {
        "propertyName": "animal"
    },
    "required": ["animal"],
    "oneOf": [
        {
            "title": "Cat",
            "properties": {
                "animal": {
                    "const": "Cat"
                },
                "food": {
                    "enum": ["meat", "grass", "fish"],
                    "type": "string"
                }
            },
            "required": ["food"]
        },
        {
            "title": "Fish",
            "properties": {
                "animal": {
                    "const": "Fish"
                },
                "food": {
                    "enum": ["insect", "worms"],
                    "type": "string"
                },
                "water": {
                    "enum": ["lake", "sea"],
                    "type": "string"
                }
            },
            "required": ["food", "water"]
        }
    ]
}

Sample data

// not needed, just Typescript Types

Your code

type AnimalExampleType =
    | {
          animal: "Cat";
          ears: number; // missing in schema intentionally
          food: "fish" | "grass" | "meat";
      }
    | {
          animal: "Fish";
          food: "insect" | "worms";
          water: "lake" | "sea";
      };

// should report error in tsc, because 'ears' are missing in 'Cat'
const AnimalExampleSchema_OneOf: JSONSchemaType<AnimalExampleType> = {
    type: "object",
    discriminator: {
        propertyName: "animal",
    },
    required: ["animal"],
    oneOf: [
        {
            title: "Cat",
            properties: {
                animal: {
                    const: "Cat",
                },
                food: {
                    enum: ["meat", "grass", "fish"],
                    type: "string",
                },
            },
            required: ["food"],
        },
        {
            title: "Fish",
            properties: {
                animal: {
                    const: "Fish",
                },
                food: {
                    enum: ["insect", "worms"],
                    type: "string",
                },
                water: {
                    enum: ["lake", "sea"],
                    type: "string",
                },
            },
            required: ["food", "water"],
        },
    ],
};

Validation result, data AFTER validation, error messages

// no tsc errors appear, even the 'ears' for 'Cat' are missing

What results did you expect?
tsc to report Type mismatch

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions