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

Discussion for a new feature - allow union to be optional #75

Open
sjriccrv opened this issue Oct 3, 2024 · 3 comments
Open

Discussion for a new feature - allow union to be optional #75

sjriccrv opened this issue Oct 3, 2024 · 3 comments
Assignees
Labels
Type: Enhancement Improving an existing feature

Comments

@sjriccrv
Copy link

sjriccrv commented Oct 3, 2024

I have a use case where I'd like to have a query string parameter (type) to be either a string or an array of strings if defined, otherwise allow it to be optional.

I'm pretty new to AdonisJS so if might just be me learning, but I couldn't figure out how to do it without a more complicated approach.

I tried this approach which seems reasonable:

export const getValidator = vine.compile(
  vine.object({
    type: vine.union([vine.string(), vine.array(vine.string())]).optional(),
  })
)

and I got an error that I couldn't use .optional() on a union.

So, I ended up with something like this (which I'm not sure is the best approach though it worked):

const typeString = vine.string().in(['core', 'addon'])
const typeSchema = vine.group([
  vine.group.if((data) => vine.helpers.isMissing(data.type), {}),
  vine.group.else({
    type: vine.unionOfTypes([vine.array(typeString), typeString]),
  }),
])

export const getValidator = vine.compile(vine.object({}).merge(typeSchema))

But then in the controller when I do:

const payload = await request.validateUsing(getValidator)
return await myService.getAll(params.id, payload.type)

I get the typescript error: Property 'type' does not exist on type '{} & {}'.

I would love if .union() would allow .optional() unless there is a better way that I'm not aware of.

Thanks.

@thetutlage thetutlage self-assigned this Nov 30, 2024
@thetutlage thetutlage added the Type: Enhancement Improving an existing feature label Nov 30, 2024
@thetutlage
Copy link
Contributor

It will be a nice to have feature. But something I will skip for now and come back to it later (once I have more bandwidth or use-cases)

@sjriccrv
Copy link
Author

sjriccrv commented Dec 19, 2024

Understandable.

Thank you for the response and thanks for everything you do with AdonisJS and Vine!

@karimfromjordan
Copy link

karimfromjordan commented Jan 16, 2025

I just ran into this too. I'm trying to create a schema that looks like this:

const schema = vine.object({
  "rating": vine.unionOfTypes([
    vine.number().min(0).max(5),
    vine.literal("*"),
  ]).optional().nullable(),
})

The rating field should be optional but if it is provided it should be

  • a number between 0 and 5
  • the literal string of * meaning any rating
  • null meaning no rating yet

But unionOfTypes(...) doesn't except literal as a type nor can the field be optional() or nulable().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Enhancement Improving an existing feature
Projects
None yet
Development

No branches or pull requests

3 participants