question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid Typescript schema requested for POST

See original GitHub issue

Hi, love this initiative. It’s allowed me to remove hundreds of lines of code.

I’ve got a strange problem trying to wire up a POST request using the standard approach:

export const submitForm = fetcher.path('/form/{formId}/submit').method('post').create();

The Swagger API definition requests formId as a parameter and formFields as the post body, but the returned function requests the following input, a intersection between an object and an array, which is impossible to provide:

image

I might be doing something wrong, but it seems like a potential bug. Here’s the type for the request:

"/form/{formId}/submit": {
    post: {
      parameters: {
        path: {
          formId: string;
        };
      };
      responses: {
        /** Success */
        200: {
          content: {
            "text/plain": components["schemas"]["FormModel"];
            "application/json": components["schemas"]["FormModel"];
            "text/json": components["schemas"]["FormModel"];
          };
        };
      };
      requestBody: {
        content: {
          "application/json":
            | components["schemas"]["FormFieldInputModel"][]
            | null;
          "text/json": components["schemas"]["FormFieldInputModel"][] | null;
          "application/*+json":
            | components["schemas"]["FormFieldInputModel"][]
            | null;
        };
      };
    };
  };


Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ajaishankarcommented, Oct 27, 2021

@bnhovde as a workaround you could try

submitForm(
  Object.assign(
    [ { identifier: "id", value: 1, observedAt: "now" } ], // body array first
    { formId: "form1" } // param next
  )
)

You still get type safety on the required properties.

0reactions
kiryptocommented, Oct 25, 2022

Hate to say it, but I think I might have evidence that top level arrays in a body is a fairly common usecase: RFC 6902 - Section 3.

That being said, it does look like the workaround and the utility function arrayRequestBody you provided is doing the trick! Just made use of it, and as with the rest of the library, it trivialized the problem. Really glad I ran across this library. Honestly this is the first time I’ve had an API service actually backed directly by the openapi spec with type validation and it’s fantastic.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript request-body types validation always pass for ...
Typescript never gives me error for any request body schema. It always gives success response. tsconfig.json { "compilerOptions": { "target": " ...
Read more >
Using with TypeScript - Ajv JSON schema validator
The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
Read more >
fluent-json-schema - npm
fluent-json-schema. A fluent API to generate JSON schemas (draft-07) for Node.js and browser. Framework agnostic.
Read more >
17.7.0 API Reference - joi.dev
If the input is invalid, error is assigned a ValidationError object providing more information. The schema can be a plain JavaScript object where...
Read more >
Chapter 6: Validations with Joi | Hapi With Typescript - Softcover
In this chapter, we will learn about: schema validations with joi plugin;; validating requests and responses with joi ;; customizing validation errors; ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found