Skip to content

["min", "$1 - 1"] generate unexpected min("x - 1") #602

Closed
@azu

Description

@azu

For example, I want to implement lessThan(<) and lessThanEqual(<=) for string length.
There is no lessThanEqual function for string in zod, I think that it is necessary to use the min and max functions to implement this.

However, graphql-codegen-typescript-validation-schema can not support operation like $1 - 1.

Reproduce Example

Config:

            config: {
                schema: "zod",
                strictScalars: true,
                scalars: {
                    ID: "string"
                },
                directives: {
                    constraint: {
                        min: ["min", "$1 - 1"],
                        max: ["max", "$1 + 1"]
                    }
                },
            },

Schema:

input ExampleInput {
  email: String!
  message: String! @constraint(min: 1, max: 1000)
}

Actual Output

export function ExampleInputSchema(): z.ZodObject<Properties<ExampleInput>> {
  return z.object({
    email: z.string(),
    message: z.string().min("1 - 1").max("1000 + 1")
  })
}

Expected Output:

export function ExampleInputSchema(): z.ZodObject<Properties<ExampleInput>> {
  return z.object({
    email: z.string(),
-    message: z.string().min("1 - 1").max("1000 + 1")
+    message: z.string().min(1 - 1).max(1000 + 1)
  })
}

Unexpected quote is inserted.

Context

In the comment, it seems that ["min", "$1 - 1"] is supported, so I don't know if this is a bug or a function that does not support.

https://github.com/Code-Hex/graphql-codegen-typescript-validation-schema/blob/ba0a27de2550ae478667874b9439d977d2292960/src/config.ts#L247C20-L248

If this syntax is not supported, we can just close this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions