Some helpful validators
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)this.checkBody('username') .notEmpty('Username required') .ensureNot(yield db.findUserByUsername(this.request.body.username), 'Username taken') -
ensureNot(or perhapsrefute) - inverse ofensure
Implementations:
// Ensure that a string does not match the supplied regular expression.
Validator.prototype.notMatch = function(reg, tip) {
if (this.goOn && reg.test(this.value)) {
this.addError(tip || this.key + ' is bad format.');
}
return this;
};
// Ensure that `assertion`, an arbitrary value, is falsey.
Validator.prototype.ensureNot = function(assertion, tip, shouldBail) {
if (shouldBail) this.goOn = false;
if (this.goOn && !!assertion) {
this.addError(tip || this.key + ' failed an assertion.');
}
return this;
};
// Ensure that `assertion`, an arbitrary value, is truthy.
Validator.prototype.ensure = function(assertion, tip, shouldBail) {
if (shouldBail) this.goOn = false;
if (this.goOn && !assertion) {
this.addError(tip || this.key + ' failed an assertion.');
}
return this;
};
edit: ignore shouldBail
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!