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

Add examples support #58

Open
2 tasks done
sayem314 opened this issue Apr 16, 2023 · 3 comments
Open
2 tasks done

Add examples support #58

sayem314 opened this issue Apr 16, 2023 · 3 comments

Comments

@sayem314
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

The library is currently unable to exhibit examples from the "examples" array in the UI schema. Ideally, the framework should be able to choose an example from the array or produce one automatically if no example is available.

The current image shows what the library is currently generating:

image

Motivation

Improving the library's ability to display examples from the "examples" array in the UI schema is to make it easier for developers to understand the expected structure and values of the input and output data in their APIs. By providing clear examples in the UI, developers can quickly understand the expected data formats and use them as a reference when building their applications.

Example

Here is my schema.

export const UserSignup = {
  body: Type.Object(
    {
      displayname: Type.String({
        minLength: 1,
        maxLength: 48,
        description: "The user's display name",
      }),
      username: Type.String({
        minLength: 2,
        maxLength: 16,
        pattern: usernamePattern,
        description:
          "The user's username. Must contain only alphanumeric characters or single hyphens, and cannot begin or end with a hyphen.",
      }),
      password: Type.String({
        minLength: 6,
        maxLength: 32,
        description: "The user's password",
      }),
      sex: Type.Union([Type.Literal('male'), Type.Literal('female'), Type.Literal('unknown')], {
        default: 'unknown',
        description: "The user's gender",
      }),
    },
    {
      examples: [
        {
          displayname: 'John Doe',
          username: 'john_doe',
          password: 'p@ssword123',
          sex: 'male',
        },
      ],
    },
  ),
  response: {
    201: Type.Literal('User created successfully.', {
      description: 'The success message after a user has been created',
    }),
  },
};

The library could benefit from an enhancement that checks whether body.examples is available before generating an example. This would prevent the generation of unnecessary or redundant examples and ensure that the user's intended example is always displayed in the UI schema.

@sayem314
Copy link
Author

Additionally, if a description is provided for the schema, it would be beneficial to display it alongside the generated example in the UI.

@sayem314
Copy link
Author

Here's the output for the schema with examples array using @sinclair/typebox.

{
  "body": {
    "examples": [
      {
        "displayname": "John Doe",
        "username": "john_doe",
        "password": "p@ssword123",
        "sex": "male"
      }
    ],
    "type": "object",
    "properties": {
    ...
    },
    "required": [
    ...
    ]
  },
  "response": {
   ...
  }
}

@mcollina
Copy link
Member

Thanks for reporting! Would you like to send a Pull Request to address this issue?

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

No branches or pull requests

2 participants