nitrojs / nitrojs/nitro

defineRouteMeta needs to take a type parameter for examples value

Open
#2,490 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement v2
Dominant language
TypeScript
Stars
11.2k
Forks
899
Avg merge
2d 24m
Merged PRs (30d)
40

Description

Describe the feature

defineRouteMeta({
  openAPI: {
    tags: ["test"],
    description: "Test route description",
    requestBody: {
      required: true,
      content: {
        "application/json": {
          description: "Payload",
          summary: "Payload",
          schema: SomeInputSchemaJson,
          examples: {
            my_correct_example {
              summary: "an awesome example",
              value: {
                 yayValidField: 'nice'
              },
            },
            my_incorrect_example {
              summary: "oops",
              value: {
                 failTrain: 'lmao'
              },
            },
          },
        },
      },
    },
    responses: {
      "200": {
        summary: "Success",
        description: "Success",
        content: {
          "application/json": {
            encoding: {},
            schema: SomeOutputSchemaJson,
          },
        },
      },
    },
  },
});

Given the above route meta, we should be able to ensure that we get type safety when inserting incorrect example data structure :


defineRouteMeta<SomeInput>({
  openAPI: {
    tags: ["test"],
    description: "Test route description",
    requestBody: {
      required: true,
      content: {
        "application/json": {
          description: "Payload",
          summary: "Payload",
          schema: SomeInputSchemaJson,
          examples: {
            my_correct_example {
              summary: "an awesome example",
              value: {
                 yayValidField: 'nice'
              },
            },
            my_incorrect_example {
              summary: "oops",
              value: {
                 failTrain: 'lmao' 👈👈👈👈👈👈👈👈 should now show error
              },
            },
          },
        },
      },
    },
    responses: {
      "200": {
        summary: "Success",
        description: "Success",
        content: {
          "application/json": {
            encoding: {},
            schema: SomeOutputSchemaJson,
          },
        },
      },
    },
  },
});
Additional information
  • Would you be willing to help implement this feature?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names defineRouteMeta but no source files or tests. Start by locating its TypeScript definition and the OpenAPI requestBody content types, then inspect how examples values are typed. Done means a generic such as SomeInput causes incorrect example fields to be rejected while valid values remain accepted.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
api, backend-api-design
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.