Giter Club home page Giter Club logo

node-raml-sanitize's Introduction

RAML Sanitize

NPM version Build status Test coverage

Strict sanitization of RAML parameters into correct types for JavaScript. If sanitization fails, the original value is returned.

Why?

This module sanitizes values using the RAML parameter syntax. You should use this if you need to convert any request parameters (usually strings) into the corresponding JavaScript types. For example, form request bodies, query parameters and headers all have no concept of types. After running sanitization, you can use raml-validate to validate the strict values.

Installation

npm install raml-sanitize --save

Usage

The module exports a function that needs to be invoked to get a sanitization instance.

var sanitize = require('raml-sanitize')();

var user = sanitize({
  username: {
    type: 'string'
  },
  password: {
    type: 'string'
  },
  birthday: {
    type: 'date',
    default: new Date()
  }
});

user({
  username: 'blakeembrey',
  password: 'hunter2'
});
// => { username: 'blakeembrey', password: 'hunter2', birthday: new Date() }

Module does not currently support wild-card parameters

Type sanitization

The module comes with built-in type sanitization of all RAML parameters - string, number, integer, date and boolean. To add a new type sanitization, add a new property with the corresponding name to the sanitize.TYPES object.

Rule sanitization

The module can be extended with rule sanitization by adding properties to the sanitize.RULES object. A few core rules are implemented by default and can not be overriden - repeat, default and type.

Empty values

Empty values are automatically allowed to pass through sanitization. The only values considered to be empty are undefined and null.

Default values

When the value is empty and a default value has been provided, it will return the default value instead.

Repeated values

When the repeat flag is set to true, the return value will be an array. If the value is not an array, it will be wrapped in an array. If the value is empty, an empty array will be returned.

Caveats

Invalid Sanitization

If a sanitization is invalid, the original value will be returned instead.

Booleans

Only false, 0, "false", "0" and "" will return false. Everything else is considered true.

License

Apache 2.0

node-raml-sanitize's People

Contributors

blakeembrey avatar

Watchers

 avatar  avatar

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.