Skip to content

[Feature request] support object in directive field to method params #360

Closed

Description

@erwan-joly

would be nice to support object in directive parsing.

Yup currently allow you to define a specific error message string quite easily for required.

directives: {
  required: {
    errorMessage: "defined"
  }
}

sadly this seems to be the equivalent in zod and it does not work. If we put the content between quotes we end up with this z.string().string("{required_error: 'The field is required.'}").
This is having two issues:

  • zod doesn't support multi chaining the same method so would need to remove the first one
  • the object is between quote :/
directives: {
  required: {
    errorMessage: ["string", { required_error: '$1' }]
  }
}

Had a quick look but sadly my understanding of the lib is quite small so haven't been able to add that feature.

Activity

  1. Code-Hex commented on May 5, 2023

    @Code-Hex
    Owner

    @erwan-joly

    input ComponentInput {
      type: ButtonComponentType!
      name: String! @required(msg: "Hello")
      event: EventInput
      child: ComponentInput
      childrens: [ComponentInput]
    }

    Then generated:

    export function ComponentInputSchema(): yup.ObjectSchema<ComponentInput> {
      return yup.object({
        child: yup.lazy(() => ComponentInputSchema()).optional(),
        childrens: yup.array(yup.lazy(() => ComponentInputSchema())).defined().nullable().optional(),
        event: yup.lazy(() => EventInputSchema()).optional(),
        name: yup.string().defined().nonNullable().required("Hello"), // This one
        type: ButtonComponentTypeSchema.nonNullable()
      })
    }

    This directive works even if it is not defined as a GraphQL schema.

    Or does the request want to assign default error messages? (to defined and nullable, etc)

  2. erwan-joly commented on May 5, 2023

    @erwan-joly
    Author

    yeah the idea is to be able to define the error message for those in my example which I can already do in yup but can't in zod because they use object parameter and the only thing we can use as param from my understanding are string and regexp

    z.string().string({required_error: 'The field is required.'})

    Additionally we can't reuse the same method in zod oposite to yup.

    this is valid in yup: yup.number().defined().defined().defined().defined() but invalid in zod.

  3. Code-Hex commented on May 13, 2023

    @Code-Hex
    Owner

    Ah, I think I understand.
    Would it be feasible to set up something like this for example?

    config:
      required: $1
        errorMessage: $1
        applyTo: defined # will be generated method

    GraphQL Schema looks like this.

    input ComponentInput {
      type: ButtonComponentType!
      name: String! @required(errorMessage: "Hello")
      event: EventInput
      child: ComponentInput
      childrens: [ComponentInput]
    }

    The expected generation is

    export function ComponentInputSchema(): yup.ObjectSchema<ComponentInput> {
      return yup.object({
        child: yup.lazy(() => ComponentInputSchema()).optional(),
        childrens: yup.array(yup.lazy(() => ComponentInputSchema())).defined().nullable().optional(),
        event: yup.lazy(() => EventInputSchema()).optional(),
        name: yup.string().defined("Hello").nonNullable(), // this one
        type: ButtonComponentTypeSchema.nonNullable()
      })
    }

    However, this method is not very intuitive for first-time users.

  4. erwan-joly commented on May 13, 2023

    @erwan-joly
    Author

    Yes something like this would works I think

  5. Code-Hex commented on Apr 25, 2026

    @Code-Hex
    Owner

    Fixed in #1423. Directive configuration now supports object arguments and recursively substitutes directive argument values into the configured schema template.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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