Giter VIP home page Giter VIP logo

express-ajv's Introduction

Hi there ๐Ÿ‘‹

  • ๐Ÿ‘ท๐Ÿผโ€โ™‚๏ธ CTO at Apio | Energy Platform Provider
  • ๐Ÿ‘ท๐Ÿผโ€โ™‚๏ธ Currently focused on delivering the best platform and tools for energy market stakeholders and the energy grid of the future.
  • ๐Ÿค“ Currently experimenting with Rust, Polars and a lot of time series data. In the past i dug deep into APIs and NodeJS.
  • ๐Ÿ“– Currently reading Mark Fisher and David Foster Wallace
  • ๐Ÿ“ซ Reach me on Linkedin for work releated chatting
  • ๐Ÿ˜„ Pronouns: He/Him
  • ๐ŸŽง Probably listening to Death/Black Metal as you read this lines

express-ajv's People

Contributors

dependabot[bot] avatar fatmatto avatar twoabove avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

twoabove

express-ajv's Issues

Add the ability to add formatters

It would be great to allow adding new formatters like the following

https://ajv.js.org/packages/ajv-formats.html

I think the change could be similar to this.

// --------------------------------------------------------------------------------------

const extensions = new Set();
const formatter = []
const addExtension = (extension) => {
if( extensions.has(extension) ) {
// throw error if extension isn't found
formatter.push(require(extension));
extensions.add(extension);
}
}

/**

  • Returns a middleware with compiled ajv validators
  • @param {Object} routeSchema An object with ajv schemas {body: Schema1, query: Schema2, prams: Schema3}
    */
    const validateRoute = routeSchema => {
    let ajv = null;

const validators = {}

// Compiling query schema beforehand
if (Object.prototype.hasOwnProperty.call(routeSchema, 'query')) {
ajv = new Ajv()
formatter.forEach( (module) => require(module)(ajv));
validators.query = ajv.compile(routeSchema.query)
}

// Compiling params schema beforehand
if (Object.prototype.hasOwnProperty.call(routeSchema, 'params')) {
// We coerce types on params because they can only be strings
// since they are part of an url
ajv = new Ajv({ coerceTypes: true })
formatter.forEach( (module) => require(module)(ajv));
validators.params = ajv.compile(routeSchema.params)
}

// Compiling body schema beforehand
if (Object.prototype.hasOwnProperty.call(routeSchema, 'body')) {
ajv = new Ajv()
formatter.forEach( (module) => require(module)(ajv));
validators.body = ajv.compile(routeSchema.body)
}

// The actual middleware that gets loaded by express
// has already-compiled validators
return (req, res, next) => {
let validation = null

if (Object.prototype.hasOwnProperty.call(validators, 'params')) {
  validation = ajv.validate(routeSchema.params, req.params)
  if (!validation) {
    return next(new Errors.BadRequest(`Request url parameters validation failed: ${ajv.errorsText()}`))
  }
}

if (Object.prototype.hasOwnProperty.call(validators, 'query')) {
  validation = ajv.validate(routeSchema.query, req.query)
  if (!validation) {
    return next(new Errors.BadRequest(`Request query validation failed: ${ajv.errorsText()}`))
  }
}

if (Object.prototype.hasOwnProperty.call(validators, 'body')) {
  validation = ajv.validate(routeSchema.body, req.body)
  if (!validation) {
    return next(new Errors.BadRequest(`Request body validation failed: ${ajv.errorsText()}`))
  }
}

return next()

}
}

module.exports = {
validateRoute: validateRoute,
addExtension: addExtension
}

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.