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

Possibility to validate in relation to other rules #164

Closed
Buzut opened this issue Sep 30, 2019 · 4 comments
Closed

Possibility to validate in relation to other rules #164

Buzut opened this issue Sep 30, 2019 · 4 comments

Comments

@Buzut
Copy link

Buzut commented Sep 30, 2019

Thank you very much for this incredible validation library. It's so lightweight and flexible, I use it everywhere and even created a Node.js API validation module on top of it.

I know v8n doesn't aim to mimic Joi, but it also has the ability to validate schemas. Would it be possible to have conditional validation at some point, like XOR, NAND etc?

Let me illustrate this:

const validation = v8n().schema({
  id: v8n()
    .number()
    .positive(),
  name: v8n()
    .string()
    .minLength(4),
  isMarried: v8n()
    .boolean(),
  birthname: v8n() // this one would be required only if isMarried is true
    .string()
    .minLength(4)
});
@imbrn
Copy link
Owner

imbrn commented Oct 5, 2019

Hey @Buzut, thank you for your really good question.

Actually, I believe it's possible to implement this feature in some way, and it's something really interesting to do. But we'll probably have to introduce some kind of abstraction in the middle of the schema rule, maybe a function.

What kind of ideas do you have for us to achieve that?

@Buzut
Copy link
Author

Buzut commented Oct 6, 2019

Thank you for your interest in my question.

So to keep the whole library coherent, I'd see something that works in the same way as passesAnyOf. Something like optionalIf (with variants optionalIfOneOf and optionalIfAllOf). It could work like so (the hardest part being how to refer to the actual object property):

const validation = v8n().schema({
  id: v8n()
    .number()
    .positive(),
  name: v8n()
    .string()
    .minLength(4),
  isMarried: v8n()
    .boolean(),
  birthname: v8n() // this one would be required only if isMarried is true
    .string()
    .minLength(4)
    .optionalIf(v8n().schema('isMarried').exact(true))
});

Or if having a polymorphic schema method leads to confusion, we could think of some specific method name like schemaProperty or even a method on schema itself (this would make sense, maintaining the chaining logic).

sbarfurth pushed a commit that referenced this issue Nov 5, 2020
@sbarfurth
Copy link
Collaborator

I think the above would be good style. It seems a bit difficult to implement, but definitely something to look at in my opinion.The main difficulty comes from schema('isMarried') I believe. This requires some sort of reference back to the validation. We would need to go back up the chain. This gets complicated with nested objects. Other than that this is essentially an optional operator, so it's not generally impossible.

@sbarfurth
Copy link
Collaborator

I'm gonna close this in favor of the combined issue #191. Thanks for the ideas @Buzut - I hope we can get to this at some point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants