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

Path params are not using Schema Types #159

Open
WilliamABradley opened this issue Jun 20, 2023 · 3 comments
Open

Path params are not using Schema Types #159

WilliamABradley opened this issue Jun 20, 2023 · 3 comments

Comments

@WilliamABradley
Copy link

It seems these get inlined instead of using the shared schema type.

Current:

const endpoints = makeApi([
    {
        method: "get",
        path: "/users/:id",
        requestFormat: "json",
        parameters: [
            {
                name: "id",
                type: "Path",
                schema: z.string().regex(/^user_[a-z0-9]{25,27}$/),
            },
        ],
        response: z.union([
            z.object({ success: z.literal(true), item: User }),
            z.object({
                success: z.literal(false),
                message: z.string().optional(),
                issues: z.array(APIIssue).optional(),
            }),
        ]),
        errors: [
            {
                status: 400,
                description: `Bad Request`,
                schema: z.object({
                    success: z.literal(false),
                    message: z.string().optional(),
                    issues: z.array(APIIssue).optional(),
                }),
            },
        ],
    },
]);

Expected:

const UserId = z.string().regex(/^user_[a-z0-9]{25,27}$/);

const User = z.object({
    _id: UserId,
    full_name: z.string().optional(),
    email: z.string().optional(),
});

const endpoints = makeApi([
    {
        method: "get",
        path: "/users/:id",
        requestFormat: "json",
        parameters: [
            {
                name: "id",
                type: "Path",
                schema: UserId,
            },
        ],
        response: z.union([
            z.object({ success: z.literal(true), item: User }),
            z.object({
                success: z.literal(false),
                message: z.string().optional(),
                issues: z.array(APIIssue).optional(),
            }),
        ]),
        errors: [
            {
                status: 400,
                description: `Bad Request`,
                schema: z.object({
                    success: z.literal(false),
                    message: z.string().optional(),
                    issues: z.array(APIIssue).optional(),
                }),
            },
        ],
    },
]);
@WilliamABradley
Copy link
Author

Another bug found here is if you mess with the complexity threshold settings, you do get a separate schema called id, and it overwrites each other, if you have another parameter with id and complexity.

@astahmer
Copy link
Owner

hey, yeah pretty much the same issue as the other one (#158).
how would you solve that ? feel free to make a PR if you come up with a good solution

@WickyNilliams
Copy link
Collaborator

i think this is also related to the problems i was experiencing around nullable. i think solving one would solve both. i have a PR with failing tests here, but no solution #177. just linking for completeness

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

3 participants