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

raml-sanitize

v2.0.0

Published

Sanitization of RAML parameters into strict values

Downloads

5,460

Readme

RAML Sanitize

NPM version Build status Test coverage Greenkeeper badge

Strict sanitization of RAML 0.8 named parameters and RAML 1.0 built-in types.

Why?

This module sanitizes values using webapi-parser Parameters and PropertyShapes. 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.

const wap = require('webapi-parser').WebApiParser
const sanitize = require('raml-sanitize')()

// returns Array<webapi-parser.PropertyShape>
async function getProperties () {
  const ramlStr = `
    #%RAML 1.0
    title: API with Types
    types:
      User:
        type: object
        properties:
          username: string
          password:  string
          birthday:
            type: date
            default: Mon, 23 Jun 2014 01:19:34 GMT
  `
  const model = await wap.raml10.parse(ramlStr)
  return model.declares[0].properties
}

async function main () {
  // These could also be Array<webapi-parser.Parameter>
  const properties = await getProperties()
  const user = sanitize(properties)
  const requestData = {
    username: 'blakeembrey',
    password: 'hunter2'
  }
  user(requestData)
  // => { username: 'blakeembrey', password: 'hunter2', birthday: new Date('Mon, 23 Jun 2014 01:19:34 GMT') }
}

main()

Module does not currently support wild-card parameters (RAML 0.8) and regular expression patterns in property declaration (RAML 1.0)

Type sanitization

The module comes with built-in type sanitization of string, number, integer, array, object, date and boolean as well as nested data. 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 - 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.

Caveats

Limitations with types (RAML 1.0)

The module does not support some Type Expressions (bi-dimensional array A[][] and array of union (A|B)[]).

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