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

Nullable doesn't cover unions with null in it #2310

Closed
SMJSGaming opened this issue Jul 24, 2023 · 4 comments
Closed

Nullable doesn't cover unions with null in it #2310

SMJSGaming opened this issue Jul 24, 2023 · 4 comments

Comments

@SMJSGaming
Copy link

What version of Ajv are you using? Does the issue happen if you use the latest version?

8.12.0

Your typescript code

export const UUID_PARAM: JSONSchemaType<{ uuid: string | null }> = {
    type: "object",
    additionalProperties: false,
    properties: {
        uuid: {
            nullable: true,
            type: "string",
            format: "uuid"
        }
    },
    required: [ "uuid" ]
};

Typescript compiler error messages

Type '{ type: "object"; additionalProperties: false; properties: { uuid: { nullable: true; type: "string"; format: string; }; }; required: "uuid"[]; }' is not assignable to type 'JSONSchemaType<{ uuid: string | null; }>'.
  The types of 'properties.uuid' are incompatible between these types.
    Type '{ nullable: true; type: "string"; format: string; }' is not assignable to type '{ $ref: string; } | ({ anyOf: readonly UncheckedJSONSchemaType<string | null, false>[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record<...> | undefined; definitions?: Record<...> | undefined; } & { ...; }) | ({ ...; } & ... 1 more ... & { ...; }) | ({ ...; } & ... 2...'.
      Types of property 'nullable' are incompatible.
        Type 'true' is not assignable to type 'false'.

Describe the change that should be made to address the issue?

Nullable should cover unions which include null.

Are you going to resolve the issue?

I would love for this to be resolved but I don't have enough experience with the internals of AJV to solve this type error myself. I would prefer it if someone who maintains this library would take a look at this.

@oyamauchi
Copy link

I believe this is the same as: #1664

This is the workaround I'm using:

type MyClass = {
  field: number | null;
}

const nullable = <T>(input: T): T => {
  return { anyOf: [input, { type: 'null' }] } as T;
};

const schema: JSONSchemaType<MyClass> = {
  // ...
  properties: {
    field: nullable({ type: 'number' })
  }
}

The nullable function adds the right element to the schema, so that the right thing happens in runtime validation, but hides it from tsc.

@SMJSGaming
Copy link
Author

The story you mentioned isn't exactly the same. Also I get that you can trick TS into accepting the values, but my point is that null unions and nullable should be considered the same thing rather than being a type error (especially considering it doesn't handle unions properly in genera making it impossible to properly validate them without tricking TS).

@Renegade334
Copy link

Duplicate: #2163

@jasoniangreen
Copy link
Collaborator

Closing as it is a duplicate of #2163

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

No branches or pull requests

4 participants