Giter Site home page Giter Site logo

peakmoney / whitelister Goto Github PK

View Code? Open in Web Editor NEW
47.0 6.0 2.0 305 KB

Simple, basic filtering and validation tool for Node.js.

Home Page: https://spireteam.github.io/whitelister/

License: MIT License

JavaScript 100.00%
javascript utilities parameters whitelist whitelisting whitelist-validation whitelisting-filter validation validation-library nodejs

whitelister's Introduction

Whitelister

Simple, dependency-free filtering and validation tool for Node.js.

Build Status

Quick Start

Using npm:

npm i --save whitelister

In Node.js

const whitelister = require('whitelister');

const rules = {
  q: 'string',
  page: { type: 'integer', min: 1, default: 1 },
  per_page: { type: 'integer', min: 1, max: 100, default: 20 },
};

const params = { q: 'hello' };

return whitelister(rules, params);
// => { page: 1, per_page: 20, q: 'hello' };

Documentation

You can find the full documentation on the website: https://spireteam.github.io/whitelister/

License

MIT

Changelog

v0.0.5

Oct. 25, 2017

  • Ignore non-required properties with undefined values
  • Treat external errors differently than internal errors

v0.0.4

Oct. 16, 2017

  • Ensure that type is treated like other attributes

whitelister's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

whitelister's Issues

Parsing dates needs work

Most importantly, the API is inconsistent with the other ranges; after/before vs min/max.

For formatting, if we're going to just be like w/e let's depend on moment then fine. If not, maybe format could be a RegExp we test against. Then, if parse = true, try using new Date().setTime(Date.parse(val)) or something.

Should we change the return signature of sync?

Do we want to automatically throw ourselves and stick with

let result;
try {
  result = whitelister.sync(rules, params);
} catch (e) {
  // handle error
}

or should we avoid the try block and let users deal with it as they please in the form of

const [err, result] = whitelister.sync(rules, params);

The main function should handle not-object params

Should support something like whitelister('integer', 100)

The most obvious use case (and something I expected when writing tests) is with arrays; this should probably work:

{ type: 'array', attributes: 'integer' }

and since a rule can be a string, it makes sense to plan for whitelister(obj || string, any).

v0.1.0 ๐ŸŽ‰

  • pass property name to filterWith, preTransform, and postTransform
  • build Whitelister for the browser w/ webpack and babel
  • build minified Whitelister for the browser w/ webpack and babel
  • publish 0.1.0-beta
  • promote beta to 0.1.0

Whitelister vs json-schema

Hi and thank you for sharing your tools with all of us. I am considering if it could be of help for my projects and cannot but think its a problem already solved with json-schema and it wide range of validation tools.

Could you please expose what are the advantages or differences between those two approaches? I am probably missing it :)

err.toJSON could be a little bit more helpful

Almost went nuts about the err.toJSON is not a function error message.

After all sort of investigation, found out that if any property is undefined, running with a filterWith function will produce such error.

It would be very helpful to tell where goes wrong instead of just showing err.toJSON is not a function.

const wl = require('whitelister');

const params = {
  firstName: 'John',
  lastName: 'Doe',
  age: 50,
  id: undefined,
};

const schema = {
  type: 'object',
  attributes: {
    age: { type: 'integer', required: true, min: 1 },
    firstName: { type: 'string', required: true, minLength: 1 },
    lastName: { type: 'string', required: true, minLength: 1 },
    id: {
      type: 'string',
      filterWith: val => val.length > 0
        ? /\w{2}\d+/.test(val)
        : true,
    },
  },
};

(async () => {
  try {
    const results = await wl(schema, { ...params });
    console.log(results);
  } catch (e) {
    console.error(e);
  }
})();

[FEATURE REQUEST] To allow renaming field name

This feature request might be useful for folks who need it:

const wl = require('whitelister');

const schema = {
  type: 'object',
  attributes: {
    first_name: { type: 'string', as: 'firstName' },
    last_name: { type: 'string', as: 'lastName' },
    type: { type: 'string', as: 'userType' },
  },
};
const data = {
  first_name: 'John',
  last_name: 'Doe',
  type: 'VIP',
};
const results = await wl(schema, data);
/**
 * {
 *   firstName: 'John'
 *   lastName: 'Doe',
 *   userType: 'VIP',
 * }
 */

Add note about custom errors

Okay, so if you pass a filterWith rule, that function should return true|false.

However, you can throw a custom error in that function instead. It's just that the customer error needs to implement toJSON and return { field, message }.

reserved keywords is not allowed

The following schema or rules will throw TypeError: err.toJSON is not a function:

const wl = require('whitelisted');
const schema = {
  type: 'object',
  attributes: {
    id: { type: 'string', required: true },
    type: { type: 'string', required: true }, // 'type' is not allowed. By changing 'type' to 'idType' does solve the problem.
  },
};

wl(schema, data).catch(err => console.error(err); 

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.