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

Question - Never type or exclusion of properties #80

Open
CodingDive opened this issue Nov 2, 2024 · 2 comments
Open

Question - Never type or exclusion of properties #80

CodingDive opened this issue Nov 2, 2024 · 2 comments

Comments

@CodingDive
Copy link

I have a validator that looks like this

const userValidator = vine.object({
   id: vine.string(),
   firstName: vine.string(),
  // ..
})

I then export them for my store and update controller endpoints to be used like this

export const createUserValidator = vine.compile(userValidator);

export const updateUserValidator = vine.compile(
  vine.object({
    ...userValidator.getProperties(),
    location: vine.string(),
  }),
);

What I am curious about if there is a way for me to say in the updateUserValidator that I explicitly do not want to include a property like the id. I know that I can move the property out of the base validator and structure it like this, but I ran into a couple of cases where having vine.never(), or a way to exclude/omit a property out of an existing schema would simplify the code a lot, or give me more confidence.

export const createUserValidator = vine.compile({
  ...userValidator.getProperties()
  id: vine.string(),
);
@thetutlage
Copy link
Contributor

While having the never schema type will work at the top-level. I am not sure if you can make it work at nested levels as well. So if you have any suggestions, feel free to share them.

Unrelated. Mindset shift can also help solve this problem

This is where I usually prefer to write duplicate schemas and not try to overuse an existing schema and create multiple combinations out of it.

I understand that DRY as a concept is very much advocated for, but in some places duplication leads to writing simple code. Just sharing an article on the Wet codebase analogy for anyone who is interested to read it. https://overreacted.io/the-wet-codebase/

@KeentGG
Copy link

KeentGG commented Dec 26, 2024

perhaps instead of defining .never() on field level, we can omit it top-level such as an .omit() modifider.

export const updateUserValidator = vine.compile(
  vine.object({
    ...userValidator.getProperties(),
    location: vine.string(),
  }).omit('id'),
);

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

3 participants