Skip to content

Constraints from directives are not applied to generated zod schema #750

@llwire

Description

@llwire

The plugin isn't applying the constraints from the GraphQL input definition to the generated zod schema. Based on what I can tell from the codebase, everything seems to be configured correctly. Am I missing something here?

Current config definitions:

overwrite: true
generates:
  ...
  ./src/graphql/__generated__/partnerApiValidations.ts:
    schema: '...'
    config:
      schema: zod
      strictScalars: true
      directives:
        constraint:
          min: min
          max: max
          pattern: [regex, /$1/, "invalid input"]
          startsWith: [regex, /^$1/, "invalid input"]
          format:
            email: email
            uri: url
            emoji: emoji
    plugins:
      - typescript-validation-schema

Directive definition:

  directive @constraint(min: Int, max: Int, pattern: String, startsWith: String, format: ConstraintFormat) on INPUT_FIELD_DEFINITION

  enum ConstraintFormat {
    email
    uri
  }

Directive used as:

  input ProductCreateInput {
    title: String! @constraint(min: 5, max: 80)
  }

Codegen output:

export function ProductCreateInputSchema(): z.ZodObject<Properties<ProductCreateInput>> {
  return z.object({
    title: z.string()
  })
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions