sindresorhus / ow

Function argument validation for humans
https://sindresorhus.com/ow/
MIT License
3.81k stars 108 forks source link

Tuple types in Ow? #179

Closed rauschma closed 3 years ago

rauschma commented 4 years ago

What is the best way to check for the tuple type [string, number] in Ow?

This is the best that I can come up with:

// [string, number]
const predPairStringNumber = ow
  .object.instanceOf(Array)
  .exactShape({
    length: ow.number.equal(2),
    0: ow.string,
    1: ow.number,
  });
sindresorhus commented 4 years ago

That is the best you can do right now. You could also use a custom validator with ow.array.validate(…). But I agree it would we useful to have a built-in validator for this. What would be the ideal API? Maybe something like ow.array.exactShape([ow.string, ow.number])?

rauschma commented 4 years ago

Using the same method name as ow.object.exactShape() seems like the right approach, yes.