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

Unable to assign null to properties defined as number | null | undefined after updating to version 6.2.0 #1612

Closed
maximebroubrou opened this issue Apr 15, 2024 · 3 comments
Labels

Comments

@maximebroubrou
Copy link

Description:

After updating TSOA from version 6.0.0 to 6.2.0, we encountered a validation error when attempting to PATCH properties that were previously defined using TypeScript utility types (e.g., Partial, Pick) and are explicitly allowed to be null (defined as number | null). We resolved this issue by changing the type definitions to explicitly define each property without embedded utility types, but it raises concerns about TSOA's handling of such TypeScript constructs in the latest version.

To Reproduce:

Steps to reproduce the behavior:

Original type definition using embedded utility types:

  1. Set up anEntity with the columns:
  @Column({ type: 'boolean', nullable: false, default: false })
  aProperty1: boolean;

  @Column({ type: 'text', nullable: false, array: true, default: [] })
  aProperty2: string[];

  @Column({ type: 'float', nullable: true, default: null })
  aProperty3: number | null;
  1. Set up a body update type:
export type aBodyUpdateArgsType = Partial<
  Pick<
    UpdateArgs<anEntity>,
    'aProperty1' | 'aProperty2' | 'aProperty3'
  >
>;
  1. Set up a TSOA route to update this entity, using a PATCH method:
@Patch()
@Body() body: aBodyUpdateArgsType[],
...
  1. Launch a server API and:
    Attempt to PATCH the aProperty1 property with a value of null : it works
    Attempt to PATCH the aProperty3 property with a value of null : Validation Failed with the following error:
{
    "message": "Validation Failed",
    "details": [
        "Field \"body.$0.aProperty3\" has invalid value: Could not match the intersection against every type. Issues: [{\"body.$0.aProperty3\":{\"message\":\"invalid float number\",\"value\":null}}]"
    ]
}
  1. Changing the type definitions to explicitly define each field, as shown below, resolves the issue:
export type aBodyUpdateArgsType = {
  aProperty1?: boolean | null;
  aProperty2?: string[];
  aProperty3?: number | null;
};

Additional Context:

This issue is particularly concerning because it affects our ability to use TypeScript utility types effectively with TSOA, limiting our approach to defining flexible and maintainable APIs. It also suggests a potential regression or change in how TSOA handles TypeScript type resolution in its latest version? 🤔

Copy link

Hello there maximebroubrou 👋

Thank you for opening your very first issue in this project.

We will try to get back to you as soon as we can.👀

@jchadwick
Copy link

Related to #1515?

Copy link

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

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

No branches or pull requests

2 participants