cloudflare/chanfana

Arr ParameterType "example" property question

Closed this issue · 1 comments

I want to know if this is missing or I'm doing something wrong.

Is there a reason we don't have array types like string[] and number[] in the example property in the ParameterType interface?

image

Because, in some cases, I want to clarify with examples that it is possible to pass an array with several options of strings or numbers.

requestBody: {
  strings: new Arr(String, {
    description: "",
    required: false,
    example: ["lorem", "ipsum", "dolor"]
  }),
  numbers: new Arr(Number, {
    description: "",
    required: false,
    example: [1, 2, 3]
  }),
},

It works fine if I ignore the type error, but I doubt if this is the right way to do something like that.
image
image

After learning more about Zod Types, Native Types and playing around with the chanfana release I found a better solution to do this in the openapi route schema.

I found that I may use convertParams to put examples of any array type. But besides that, If it's needed to clarify all the allowed values of an array, I have to use enums, Enumeration for strings, and z.nativeEnum (from zod) for numbers.

request: {
  body: {
    content: {
      "application/json": {
            schema: Obj({
              /* For array of strings
               * I use Enumeration
               * To clarify all the allowed strings values
               */
              strings: convertParams(Arr(Enumeration({
                values: ["lorem", "ipsum", "dolor", "sit", "amet"],
              })), {
                description: "",
                required: false,
                example: ["lorem", "ipsum", "dolor"],
              }),
              /* For array of numbers
               * I use Native Enum using zod (I didn't found a way to do it with chanfana)
               * To clarify all the allowed integer values
               */
              numbers: convertParams(Arr(z.nativeEnum({
                "number1": 1,
                "number2": 2,
                "number3": 3,
                "number4": 4
              })), {
                description: "",
                required: false,
                example: [1, 2, 3]
              })
          })
      }
    }
  }
}

Leading to this:
image