npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

fleek-validator

v2.0.1

Published

A validator for the fleek framework

Downloads

82

Readme

Fleek Validator

Build Status

Middleware and utilities for validating data against swagger schema's.

Requirements:

Usage

This package is to be used as middleware for Koa2 to validate swagger documentation using ctx.fleek.context defined by fleek-context or an equivalent custom middleware. Results of the validation are mapped to ctx.fleek.validation, including passed, failed, and errors.

npm install --save fleek-validator

Examples

For a swagger example, refer to the test swagger json

const Koa = require('koa');
const fleekCtx = require('fleek-context');
const fleekValidator = require('fleek-validator');

const SWAGGER = require('./swagger.json');

let app = new Koa();

app.use(fleekCtx(SWAGGER));

app.use(fleekValidator()); // Reject promise for validation failure

// OR

app.use(fleekValidator().catch((ctx, next) => {
  console.log(ctx.fleek.validation); // =>{
  //   passed: [Boolean],
  //   failed: [Boolean],
  //   errors: [Array({ - github.com/fleekjs/fleek-validator/blob/lib/error/index.js
  //    name: [String] - github.com/fleekjs/fleek-validator/blob/lib/error/codes.json
  //    code: [Integer] - github.com/fleekjs/fleek-validator/blob/lib/error/codes.json,
  //    message: [String] - github.com/fleekjs/fleek-validator/blob/lib/error/codes.json,
  //    parameter : { [rejected param definition from swagger doc] }
  //  })]
  // }
  return next();
}));

app.listen(3000);

Documentation

Middleware

  • Accepts
    • Object - options
      • throw: Boolean - if false, do not reject the middleware promise on validation failure
      • catch: Function - must act as Koa2 middleware. this will be call if validation fails, with next referring to the next middleware in the chain. prioritized over throw
  • Returns
    • Function - returns a promise when called
      • Accepts
        • Object - context of the request. must have ctx.fleek.context to perform validation
        • Function - returns promise when called
  • Binds
    • ctx.fleek.validation
{
  passed: [Boolean],
  failed: [Boolean],
  errors: [Array({ - github.com/fleekjs/fleek-validator/blob/lib/error/index.js
   name: [String] - github.com/fleekjs/fleek-validator/blob/lib/error/codes.json
   code: [Integer] - github.com/fleekjs/fleek-validator/blob/lib/error/codes.json,
   message: [String] - github.com/fleekjs/fleek-validator/blob/lib/error/codes.json,
   parameter : { [rejected param definition from swagger doc] }
 })]
}

Example

app.use(validator()); // reject middleware promise on failure

app.use(validator({ throw: false })); // continue down middleware on failure

app.use(validator({ catch: (ctx, next) => { return next(); } })); // continue down middleware on failure
app.use(validator().catch((ctx, next) => { return next(); })); // continue down middleware on failure

Ctx

  • Accepts
    • Object - context of a request definted by Koa2, and containing ctx.fleek.context to define the validations
    • Object - swagger definition to validate, matches ctx.fleek.context set by fleek-context
    • Boolean - if true, the ctx will be cloned and returned. defaults to false, and updates in place
  • Returns
    • Object - updated context

Example

ctx.request.body = { name: 'foo' };

validator.ctx(ctx);
validator.ctx(ctx, {
  parameters: [{
    name: "user",
    in: "body",
    schema: {
      type: "object",
      required: ["name"],
      properties: {
        name: { type: "string", uppercase: true }
      }
    }
  }]
});

let newCtx = validator.ctx(ctx, null, true);

console.log(newCtx); // => {
//   passed: false,
//   failed: true,
//   errors: [{
//     name: 'VALUE.UPPERCASE'
//     code: 207,
//     message: 'Must be uppercase',
//     parameter : { name: 'user.name' type: 'string', uppercase: true }
//   }]
// }
// }

Object

  • Accepts
    • Mixed - value to be validated
    • Object - swagger object definition
  • Returns
    • Either
      • Mixed - normalized result of the validations
      • Error - error containing .errors list of validation errors

Example

let result = validator.object({ name: 'FOO' }, { name: { type: 'string', uppercase: true } });
console.log(result); // => { name: 'FOO' }
let result = validator.object({ name: 'foo' }, { name: { type: 'string', toUpperCase: true } });
console.log(result); // => { name: 'FOO' }
let result = validator.object({ name: 'foo' }, { name: { type: 'string', uppercase: true } });
console.log(result); // => {
//   message 'Validation failed',
//   errors: [{
//     name: 'VALUE.UPPERCASE'
//     code: 207,
//     message: 'Must be uppercase',
//     parameter : { type: 'string', uppercase: true }
//   }]
// }

One

  • Accepts
    • Mixed - value to be validated
    • Object - swagger parameter definition
  • Returns
    • Either
      • Mixed - normalized result of the validations
      • ValidationError - validation error

Example

let result = validator.one('FOO', { type: 'string', uppercase: true });
console.log(result); // => 'FOO'
let result = validator.one('foo', { type: 'string', toUpperCase: true });
console.log(result); // => 'FOO'
let result = validator.one('foo', { type: 'string', uppercase: true });
console.log(result); // => {
//   name: 'VALUE.UPPERCASE'
//   code: 207,
//   message: 'Must be uppercase',
//   parameter : { type: 'string', uppercase: true }
// }

Normalizations

default

  • Expect: [Boolean]
  • Type: [Mixed]
  • Action: if the value is not defined, set it to the default
  • Note: First action taken in order of operations

trim

  • Expect: [Boolean]
  • Type: [String]
  • Action: trim whitespace

toUpperCase

  • Expect: [Boolean]
  • Type: [String]
  • Action: convert to uppercase

toLowerCase

  • Expect: [Boolean]
  • Type: [String]
  • Action: convert to uppercase

Validations

required

  • Expect: [Boolean]
  • Type: [String]
  • Action: reject undefined
  • Note: Second action taken in order of operations, after default

type

  • Expect: [String]
  • Type: [Mixed]
  • Action: reject the value if it does not match the expected type

maxItems

  • Expect: [Integer]
  • Type: [Array]
  • Action: rejects an array with more than the expected max

minItems

  • Expect: [Integer]
  • Type: [Array]
  • Action: rejects an array with fewer than the expected min

uniqueItems

  • Expect: [Boolean]
  • Type: [Array]
  • Action: reject an array with duplicate items

multipleOf

  • Expect: [Integer]
  • Type: [Number]
  • Action: reject a number thats not a multiple of the expected integer

maximum

  • Expect: [Integer]
  • Type: [Number]
  • Action: reject a number thats above the expected max

exclusiveMaximum

  • Expect: [Integer]
  • Type: [Number]
  • Action: reject a number thats above or equal to the expected max

minimum

  • Expect: [Integer]
  • Type: [Number]
  • Action: reject a number thats below the expected min

exclusiveMinimum

  • Expect: [Integer]
  • Type: [Number]
  • Action: reject a number thats below or equal to the expected min

maxProperties

  • Expect: [Integer]
  • Type: [Object]
  • Action: reject an object with more than the expected max number of properties

minProperties

  • Expect: [Integer]
  • Type: [Object]
  • Action: reject an object with fewer than the expected min number of properties

email

  • Expect: [Boolean]
  • Type: [String]
  • Action: reject a string that is not a valid email

alphanumeric

  • Expect: [Boolean]
  • Type: [String]
  • Action: reject a non-alphanumeric string

lowercase

  • Expect: [Boolean]
  • Type: [String]
  • Action: reject a string containing uppercase characters

uppercase

  • Expect: [Boolean]
  • Type: [String]
  • Action: reject a string containing lowercase characters

minLength

  • Expect: [Integer]
  • Type: [String]
  • Action: reject a string of length below the expected length

maxLength

  • Expect: [Integer]
  • Type: [String]
  • Action: reject a string of length above the expected length

pattern

  • Expect: [RegExp String]
  • Type: [String]
  • Action: reject a string that doesnt pass the rejex

enum

  • Expect: [Array]
  • Type: [Mixed]
  • Action: reject any value not listed in the enumeration

Authors

Built and maintained with by the Hart team.