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

When I use discriminator, I am getting an error where it can't find the specific field #1358

Open
davidli108 opened this issue Jul 2, 2024 · 1 comment
Assignees

Comments

@davidli108
Copy link

Versions:
uniforms: ^3.10.1
uniforms-mui: ^3.10.1

type A1 = {
  id_type: 'a1';
  id: string;
};
type A2 = {
  id_type: 'a2';
  id: string;
};
type C = {
  name: string;
  address: string;
}

type A1c = A1 & C
type A2c = A2 & C

/**
 * @discriminator id_type
 */
export type A = A1c | A2c

When i generate the json schema from this typescript, the json is

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$ref": "#/definitions/A",
  "definitions": {
    "A": {
      "type": "object",
      "discriminator": {
        "propertyName": "id_type"
      },
      "required": [
        "id_type"
      ],
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "name": {
              "type": "string"
            },
            "address": {
              "type": "string"
            },
            "id_type": {
              "type": "string",
              "const": "a1"
            },
            "id": {
              "type": "string"
            }
          },
          "required": [
            "address",
            "id",
            "id_type",
            "name"
          ]
        },
        {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "name": {
              "type": "string"
            },
            "address": {
              "type": "string"
            },
            "id_type": {
              "type": "string",
              "const": "a2"
            },
            "id": {
              "type": "string"
            }
          },
          "required": [
            "address",
            "id",
            "id_type",
            "name"
          ]
        }
      ]
    }
  }
}

In this case, I am getting an error and it says it cannot find the "name" or "address" field in A
As you can see, A has "type": "object" inside of it and oneOf property also has "type": "object" in each property.
When i deleted the first "type": "object", I didn't see the error.

Can you please check?

@zendranm
Copy link
Member

zendranm commented Jul 5, 2024

Hi @davidli108 Seem that the problem is related specifically to the uniforms-bridge-json-schema. Out of curiosity what did you use to generate this schema from types? In general, the handling of allOf/anyOf/oneOf is not complete as mentioned here.

I've tried to reproduce this error using the schema you shared but I've encountered a different error: Error: strict mode: unknown keyword: "discriminator". Perhaps a complete reproduction of your issue could shed some more light on it.

It's possible that the type: "object" at the top level of the schema conflicts with the oneOf keyword, as the validator might be expecting an additional level of nesting in the structure.

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

No branches or pull requests

2 participants