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

properties + anyOf generates incorrect output #567

Open
gborobio73 opened this issue Jan 11, 2024 · 3 comments
Open

properties + anyOf generates incorrect output #567

gborobio73 opened this issue Jan 11, 2024 · 3 comments

Comments

@gborobio73
Copy link

Given this schema:

{
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "id": {
      "type": "string"
    },
    "validFrom": {
      "type": "string"
    },
    "validTo": {
      "type": "string"
    }
  },
  "anyOf": [
    {
      "required": ["id", "validFrom"]
    },
    {
      "required": ["id", "validTo"]
    }
  ],
  "title": "Assortment"
}

This is the type generated:

export type Assortment = Assortment1 & Assortment2;
export type Assortment1 = {
  [k: string]: unknown;
};

export interface Assortment2 {
  id?: string;
  validFrom?: string;
  validTo?: string;
}

And I was hoping it would generate something like this:

export type Assortment =
  | {
      id: string;
      validFrom: string;
      validTo?: string;
    }
  | {
      id: string;
      validFrom?: string;
      validTo: string;
    };

I might be doing something wrong, so my apologies in advance.

Thank you so much!

@bcherny bcherny changed the title Conditionally required fields generates a "dictionary" type properties + anyOf generates incorrect output Jan 14, 2024
@LukasFritzeDev
Copy link

This technique is called Factoring Schemas in the documentation and should work.

A simpler sample would be

{
  "type": "string",
  "oneOf": [
    {
      "description": "Name of resource.",
      "pattern": "^d[0-9]+$"
    },
    {
      "description": "Id (uuid) of resource.",
      "format": "uuid"
    }
  ]
}

produces this type:

{
  [k: string]: unknown;
} & string;

when I’d expect

string | string

or just

string

@petrosv91
Copy link

Any update on this? Or a workaround?

@drewhoener
Copy link

oh good this is an actual issue, I thought I just didn't understand json schema lol
+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants