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

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

Open
erwan-joly opened this issue Apr 27, 2023 · 4 comments
Open

Comments

@erwan-joly
Copy link

erwan-joly commented Apr 27, 2023

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.

@Code-Hex
Copy link
Owner

Code-Hex commented May 5, 2023

@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)

@erwan-joly
Copy link
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.

@Code-Hex
Copy link
Owner

Code-Hex commented May 13, 2023

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.

@erwan-joly
Copy link
Author

Yes something like this would works I think

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