Skip to content

Toggable ordered flag for array to match items against input in order#688

Merged
Marsup merged 1 commit intohapijs:masterfrom
ck-lee:master
Oct 5, 2015
Merged

Toggable ordered flag for array to match items against input in order#688
Marsup merged 1 commit intohapijs:masterfrom
ck-lee:master

Conversation

@ck-lee
Copy link
Contributor

@ck-lee ck-lee commented Jul 19, 2015

Feel free to ping me for suggestion/feedback on my code. Happy to change them if necessary.
closes #659

lib/array.js Outdated
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One too many LF.

@ck-lee
Copy link
Contributor Author

ck-lee commented Jul 21, 2015

@Marsup Thanks for your feedback. I've fixed what I could.

Can you explain what is the purpose of strip option? I couldn't find a good example in the lib/array.js.
I also couldn't find the rogue LF for test/array.js.

README.md Outdated
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're still documenting things that have little to do with this method, the type is not in the method's signature.

@Marsup Marsup added this to the 6.7.0 milestone Jul 21, 2015
@Marsup Marsup added the feature New functionality or improvement label Jul 21, 2015
@Marsup Marsup self-assigned this Jul 21, 2015
@Marsup
Copy link
Collaborator

Marsup commented Jul 21, 2015

The strip flag is a consequence of the call of any.strip(), it's used just below, you should be able to understand the goal with the docs.
The LF is there in the github diff, so it's probably there in your source as well :)

@ck-lee
Copy link
Contributor Author

ck-lee commented Jul 25, 2015

@Marsup Thanks for your comment... Please review. Got the strip() implemented now. Missed the LF because JSlint wasn't complaining. Let me know if there are any more changes to do.

lib/array.js Outdated
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Careful with the index here, if your test didn't strip the last one it would fail, you have to change v.

@ck-lee
Copy link
Contributor Author

ck-lee commented Jul 27, 2015

@Marsup Please review.

@Marsup
Copy link
Collaborator

Marsup commented Aug 6, 2015

Sorry for the delay, busy days... One last thing, would you mind including all the w things into the loop parameters ? After that, if you know how to, can you rebase on current master and squash those commits ?

@ck-lee
Copy link
Contributor Author

ck-lee commented Aug 8, 2015

No worries.
Pretty scary to rebase and squash. Hope that works out alright.

@Marsup
Copy link
Collaborator

Marsup commented Aug 15, 2015

Hey, sorry again I'm a bit slow on this one, I had to think about it for a while :)
There are 2 last problems that have been bothering me :

  • it will crash if the number of items in the array is higher than the number of items
  • I think we should be able to specify optional items, like Joi.array().items(a.required(), b.required(), c, d, e) where the array should be at least 2 items (a, b) and an infinite variation of c, d or e. Thoughts ?

Does it look like something you can achieve ?

@ck-lee
Copy link
Contributor Author

ck-lee commented Aug 15, 2015

No worries. I don't have a specific need for this feature. I'm contributing to learn. So, no rush...

  • The first problem can be solved by better error handling, I think it also exists without ordered(). I can definitely help out with that.
  • The use case for ordered() is to have strict validation on the array of items. Can the use case for optional items be met without the ordered() flag?

@Marsup
Copy link
Collaborator

Marsup commented Aug 16, 2015

The 1st problem doesn't exist currently since loops on values and schemas are not tied together, unlike what you have to do here.

The use case cannot be covered with current API. I'm starting to think an even more powerful construct would be for ordered not to be a flag but to accept a list of schemas like items.

@ck-lee
Copy link
Contributor Author

ck-lee commented Aug 21, 2015

Can you explain more in detail because I'm a bit lost here?

Like Joi.array().ordered(Joi.string().valid('woohoo').required(), Joi.number().required(), Joi.string(), Joi.number()) where it would match ['woohoo', 2, 789, 'any string', 'from here']?

What about count of items in array? Should it pass if it doesn't match? Maybe not? Because that use case can be validated against length?

@Marsup
Copy link
Collaborator

Marsup commented Aug 24, 2015

Let's take that example :

Joi.array()
  .ordered(Joi.string().required(), Joi.boolean().required(), Joi.number())
  .items(Joi.date())

It would match anything containing at least a string and a boolean, optionally a number, then any number of dates.
It seems like a useful addition to me.

@pluma
Copy link
Contributor

pluma commented Aug 24, 2015

+1 on the PR as it is. My main use case is supporting tuples (e.g. to serialize ordered maps as arrays of key/value tuples).

I'm not sure how common the use case @Marsup is describing is as I haven't come across it myself yet.

I think a more common example would be .ordered(...).items(joi.any()) -- where you only care about the types of the first N items in a mixed array and don't care if it contains anything else after them.

@ck-lee
Copy link
Contributor Author

ck-lee commented Aug 24, 2015

Nice...!! I will give that a shot.

@ck-lee
Copy link
Contributor Author

ck-lee commented Aug 30, 2015

Sorry, I didn't get to work on this PR this week. 😞 And it would need to be put on hold for a couple of weeks while I'm travelling.

@Marsup
Copy link
Collaborator

Marsup commented Sep 30, 2015

I think we're missing a test case (which would probably fail) where there are fewer elements than the number of required ordered items.

@ck-lee
Copy link
Contributor Author

ck-lee commented Sep 30, 2015

Yes. You are right. That test case will fail. 😞 I will fix it tomorrow.

@ck-lee
Copy link
Contributor Author

ck-lee commented Oct 1, 2015

@Marsup Please review.

lib/array.js Outdated
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those 2 lines should be written :

var ordered = ordereds.shift();

@Marsup
Copy link
Collaborator

Marsup commented Oct 1, 2015

Very good job so far, we're getting to the final details :) 👍

@Marsup Marsup modified the milestones: 6.9.0, 6.8.0 Oct 2, 2015
@ck-lee
Copy link
Contributor Author

ck-lee commented Oct 3, 2015

Thanks, @Marsup. Please review.

lib/array.js Outdated
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need to splice here, just build a new array out of the required schemas.

@ck-lee
Copy link
Contributor Author

ck-lee commented Oct 4, 2015

Done. Please review.

@ck-lee ck-lee reopened this Oct 4, 2015
lib/array.js Outdated
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're not going to be using this flag, this line can be removed.

@Marsup
Copy link
Collaborator

Marsup commented Oct 4, 2015

Those were the final ones, ready to merge after that :)

@ck-lee
Copy link
Contributor Author

ck-lee commented Oct 5, 2015

Cool. All done now. :)

Marsup added a commit that referenced this pull request Oct 5, 2015
Toggable ordered flag for array to match items against input in order
@Marsup Marsup merged commit c797d70 into hapijs:master Oct 5, 2015
@Marsup
Copy link
Collaborator

Marsup commented Oct 5, 2015

Congrats, hope it was not too painful :)

@ck-lee
Copy link
Contributor Author

ck-lee commented Oct 5, 2015

Nice!! It was more fun than painful. :) Do let me know if you have other
issues that you would like help with...
Thanks.

-CK-
On 5 Oct 2015 23:38, "Nicolas Morel" notifications@github.com wrote:

Congrats, hope it was not too painful :)


Reply to this email directly or view it on GitHub
#688 (comment).

@lock
Copy link

lock bot commented Jan 9, 2020

This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 9, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

feature New functionality or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow treating arrays as objects

3 participants