sindresorhus / ow

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

Mandatory parameters? #83

Open Martin-Pitt opened 6 years ago

Martin-Pitt commented 6 years ago

Could there be a ow.mandatory that throws by using it as default arg?

function getPage(identifier = ow.mandatory()) {
    ow(identifier, ow.string);
}
ArgumentError: Expected a mandatory argument but received `undefined` or missing

Reference: 16. Mandatory Parameter Shorthand

mandatory = () => {
  throw new Error('Missing parameter!');
}

foo = (bar = mandatory()) => {
  return bar;
}
transitive-bullshit commented 6 years ago

I know @SamVerschueren is working on explicitly optional predicates, but I think your use case is handled by ow normally without adding anything to its API surface.

function getPage(identifier) {
  ow(identifier, ow.string)
}

Afaik it's not currently possible to use ow to ensure a variable is defined without checking its type, so maybe that's something we could add:

function getPage(identifier) {
  // note: this doesn't currently work, but is a proposed workaround
  ow(identifier, ow.any)
}