vine icon indicating copy to clipboard operation
vine copied to clipboard

Question: How to allow optional fields to be omitted?

Open CodingDive opened this issue 1 year ago • 3 comments
trafficstars

Package version

Latest (1.7.1)

Describe the bug

Thank you for this fantastic library and all the work in Adonis v6! ❤️

I tried to follow https://vinejs.dev/docs/schema_101#nullable-and-optional-modifiers

export const updateUserValidator = vine.compile(
  vine.object({
    password: vine.string().minLength(9).nullable().optional(),
  })

however, when I extract the type out of the validator:

type UpdateUser: Awaited<ReturnType<typeof updateUserValidator.validate>> I can see that the resulting type is password: string | null | undefined;. Is there a way to make it password?: string | null | undefined?

I could write a TypeScript helper to accomplish this in user land but it's not that pretty and I was wondering if there is an easier way.

type MakeUndefinedPropertiesOptional<T> = {
  [P in keyof T]-?: undefined extends T[P] ? P : never
}[keyof T] extends infer D
  ? { [P in keyof T as P extends D ? P : never]?: T[P] } & {
      [P in keyof T as P extends D ? never : P]: T[P]
    }
  : never

type UndefinedBar = { foo: string | null; bar: string | undefined; test: string }

type OptionalBar = MakeUndefinedPropertiesOptional<UndefinedBar> // { bar?: string | undefined } & { test: string; foo: string | null }

Small aside: In the repo, when clicking on "Get Help => Open", a new tab opens but it doesn't fill out the issue template with Question.

image

CodingDive avatar Feb 15 '24 22:02 CodingDive

Hey @CodingDive! 👋🏻

That's not how you should extract the type from the validator; you should use Infer.

import vine from '@vinejs/vine'
import type { Infer } from '@vinejs/vine/types'

const schema = vine.object({
  username: vine.string(),
  email: vine.string().email(),
  password: vine
    .string()
    .minLength(8)
    .maxLength(32)
    .confirmed()
})

type UserRegistration = Infer<typeof schema>

📚 https://vinejs.dev/docs/getting_started#inferring-types-from-schema

RomainLanz avatar Feb 15 '24 23:02 RomainLanz

This simplifies the code quite a bit 😅 Thank you!

I'd still need the MakeUndefinedPropertiesOptional helper as far as I can tell

CodingDive avatar Feb 15 '24 23:02 CodingDive

how can we make all the field optional in vine object

mdsadiqueinam avatar Apr 24 '24 02:04 mdsadiqueinam

I am also struggling with this, I've used the provided Infer type, but the .optional() fields are still required to be explicitly set to undefined. I've tried the MakeUndefinedPropertiesOptional, and the following type but to no avail. As well as other permutations.

type UndefinedKeys<T> = {
  [K in keyof T]: undefined extends T[K] ? K : never;
}[keyof T];

type OmitUndefined<T> = Omit<T, UndefinedKeys<T>> & Partial<Pick<T, UndefinedKeys<T>>>;

pohy avatar Nov 07 '24 09:11 pohy

This has been fixed by https://github.com/vinejs/vine/pull/79.

However, it is a breaking change and I will release it as a major release with few another planned improvements.

thetutlage avatar Nov 13 '24 07:11 thetutlage

Fixed in Vine 3.0 https://github.com/vinejs/vine/releases/tag/v3.0.0

thetutlage avatar Dec 01 '24 04:12 thetutlage