Open danneu opened 9 years ago
I don't particularly see the need for the not
api.
I propose two additional functions though:
ensureFn
:: Takes a function, calls it with the value
ensureYield
:: Takes a generator, calls it with the value and yields it.
Simple example:
this.checkBody('username').trim().ensureYield(db.userExists) // yields db.userExists function with username
this.checkBody('address').trim().ensureFn(isValidBitcoinAddress) // calls sync function with the address value
I don't think ensureYield is general enough at first glance, but I like the idea of piping the value through a function. I'll play with it.
Also, ensureNot
is useful because negating a yield is annoying. You have to wrap the whole expression in parens.
this.checkBody('uname').ensure(!(yield db.findUserByUname(this.request.body.uname)))
this.checkBody('uname').ensureNot(yield db.findUserByUname(this.request.body.uname)) // nicer
Sure, not a big deal, but this library already has a precedent for not
in its API.
yeah, very powerful! happy Chinese new year!
Here are some validators I use in my project that I think should belong to koa-validate.
notMatch
- inverse ofmatch
ensure
- executes arbitrary expression. if falsey, then error. (koa-validate needs a generic validator for arbitrary expressions)ensureNot
(or perhapsrefute
) - inverse ofensure
Implementations:
edit: ignore shouldBail