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

ajv-sanitizer

v1.2.1

Published

String sanitization with JSON-Schema using Ajv

Downloads

8,574

Readme

ajv-sanitizer

String sanitization with JSON-Schema using Ajv.

npm

It uses the library validator.js under the hood for string sanitizion.

Installation and Usage

Installation

Install the library with npm install ajv-sanitizer

Usage

const Ajv = require('ajv');
const ajvSanitizer = require('ajv-sanitizer');
const assert = require('assert');

const ajv = new Ajv();
ajvSanitizer(ajv);

const schema = {
	type: 'object',
	properties: {
		value: {
			type: 'string',
			sanitize: 'text',
		},
	},
};

// sanitized data must be an object property
const data = {
	value: ' trim & escape string',
};

ajv.validate(schema, data);

assert(data.value === 'trim & escape string');

ES6

import ajvSanitizer from 'ajv-sanitizer';

API

ajvSanitize(ajvInstance, [extraSanitizers])

Returns Ajv instance. It adds a sanitize keyword available for string types.

ajvInstance

Type: Ajv

The ajv instance to add the sanitize keyword.

extraSanitizers

Type: Object

Extend or override defaults sanitizers available in json schema.

Sanitizers

Available sanitizers

Here is a list of the sanitizers currently available :

  • boolean
  • date
  • email
  • escape
  • float
  • int
  • number
  • text (escape then trim)
  • trim

See validator.js sanitizers for details

Custom sanitizer

const schema = {
	type: 'object',
	properties: {
		value: {
			type: 'string',
			// Custom sanitizer
			sanitize: data => `-- ${data} --`,
		},
	},
};

Usage of email sanitization with custom options:

import { normalizeEmail } from 'validator';

const schema = {
	type: 'object',
	properties: {
		value: {
			type: 'string',
			sanitize: email => normalizeEmail(email, { gmail_remove_dots: false }),
		},
	},
};

If you want to sanitize email this way in every schema, use the following option

Extending default sanitizers

Adding a sanitizer or override a default globally :

const Ajv = require('ajv');
const ajvSanitizer = require('ajv-sanitizer');
const { normalizeEmail } = require('validator');

const ajv = new Ajv();

// Define extra sanitizer and override defaults
const extraSanitizers = {
	email: email => normalizeEmail(email, { gmail_remove_dots: false }), // overrides default email sanitizer
	uppercase: text => text.toUpperCase(), // new uppercase sanitizer
};

ajvSanitizer(ajv, extraSanitizers);

const schema = {
	type: 'object',
	properties: {
		email: {
			type: 'string',
			sanitize: 'email',
		},
		lastname: {
			type: 'string',
			sanitize: 'uppercase',
		},
	},
};