Giter VIP home page Giter VIP logo

Comments (3)

fedeci avatar fedeci commented on May 24, 2024

Hello @ArkinSolomon you can execute this kind of logic by running chains in an imperative way, see docs for reference. Please ping me for further help!

from express-validator.

ArkinSolomon avatar ArkinSolomon commented on May 24, 2024

Thanks for your fast response, however this does not answer the question. I basically want to just pass a string in, not pull it from a body or anything. There is no request object as this is simply validating on the client side before submission.

from express-validator.

fedeci avatar fedeci commented on May 24, 2024

I have been quite absent from the project in the last year and a half, but at a quick glance it seems nothing changed to me and you still need to provide a req object, you can create a mock one just to be passed to validate to express-validator.

// data you need to validate
const name = 'Federico'
const age = 21

// helper function
const validate = validations => {
  return async (req, res, next) => {
    for (let validation of validations) {
      const result = await validation.run(req);
      if (result.errors.length) break;
    }

    const errors = validationResult(req);
    return errors.array()
  };
};

const validators = [
  body('name').isString().isLength({ min: 4, max: 20 }),
  body('age').isInt(),
]

const validationChain = validate(validators)
const req = {
  body: {
    name,
    age
  }
}
const errors = await validationChain.run(req) // You can then reuse req.body to compose the request to the server
if errors.length === 0 {
  // perform server request here
} else {
  // handle errors in the UI
}

from express-validator.

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.