poppinss / indicative

Indicative is a simple yet powerful data validator for Node.js and browsers. It makes it so simple to write async validations on nested set of data.
https://indicative.adonisjs.com/
MIT License
417 stars 52 forks source link

Feature/subset #203

Closed AndrewJo closed 5 years ago

AndrewJo commented 6 years ago

Proposed changes

This PR adds support for adding subset validator which is useful for validating the include query string in JSONAPI:

The value of the include parameter MUST be a comma-separated (U+002C COMMA, “,”) list of relationship paths. A relationship path is a dot-separated (U+002E FULL-STOP, “.”) list of relationship names.

If a server is unable to identify a relationship path or does not support inclusion of resources from a path, it MUST respond with 400 Bad Request.

Example usage:

Here's a contrived example:

const rule = {
  include: 'subset:author,comments,comments.author'
}

If for example, the query string looks like: include=author,comments.author, the validation will pass. If however, the query string looks like: include=invalid-relationship, the validation will fail.

Types of changes

What types of changes does your code introduce?

Put an x in the boxes that apply

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

Further comments

@thetutlage I only read the Contributing guidelines only after I finished this implementation and created this PR. I hope it's okay that I didn't create an RFC first as I wasn't aware before submitting this PR.

thetutlage commented 6 years ago

If I am not wrong, at-least one of the values has to match from the given subset right?

AndrewJo commented 6 years ago

Yes as long as it doesn't contain any values that's not in the superset. For example, if you have a rule:

const rule = {
  numbers: 'subset:1,2,3,4,5'
}

Matches:

const data = {
  numbers: [2, 1, 5]
}

But will throw validation message on:

const data = {
  // 6 is not an element in the original array
  numbers: [1, 4, 3, 6]
}

It's most useful for matching whether multiple values are in a whitelist (versus in rule which only validates a single value in a list).

thetutlage commented 6 years ago

Looks beautiful 😄

GuilhermeGuitte commented 5 years ago

Cool! Any news about this feature?

AndrewJo commented 5 years ago

@RomainLanz I just noticed you approved the PR. When can we expect to see this merged in?

RomainLanz commented 5 years ago

Hey @AndrewJo! 👋

I don't have the right to merge this PR. @thetutlage will probably do it when he has time to review it.

AndrewJo commented 5 years ago

Ah, got it. Thank you for the clarification! @RomainLanz

thetutlage commented 5 years ago

Sorry for getting late. At times, it's hard to look at Github notifications coz of all other work you have in life :)

AndrewJo commented 5 years ago

@thetutlage No worries 😄 I totally understand. I have few open source projects myself that I forget to check sometimes. :wink: