Coder Social home page Coder Social logo

Some helpful validators about koa-validate HOT 3 OPEN

rocksonzeta avatar rocksonzeta commented on August 17, 2024
Some helpful validators

from koa-validate.

Comments (3)

RHavar avatar RHavar commented on August 17, 2024

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

from koa-validate.

danneu avatar danneu commented on August 17, 2024

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.

from koa-validate.

RocksonZeta avatar RocksonZeta commented on August 17, 2024

yeah, very powerful! happy Chinese new year!

from koa-validate.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.