1.0.0 • Published 3 years ago

ajv-disinfection v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

ajv-disinfection

Sanitization using Ajv.

Installation

Install the library with npm install ajv-disinfection --save

How to use

import Ajv from 'ajv'
import ajvDisinfection from 'ajv-disinfection'
const ajv = new Ajv();
ajvDisinfection(ajv);

const schema = {
  type: 'object',
  properties: {
    content: {
      type: 'string',
      disinfect: 'html' //boolean, date, email, escape, float, int, number, text, trim, regex
    }
  }
};

const data = {
  content: 'data to <b>disinfect</b>'
};

ajv.validate(schema, data);

Advanced HTML sanitization

You have the ability to set options based on apostrophecms/sanitize-html. This example just allow tags.

import Ajv from 'ajv'
import ajvDisinfection from 'ajv-disinfection'
const ajv = new Ajv();
ajvDisinfection(ajv);

const schema = {
  type: 'object',
  properties: {
    content: {
      type: 'string',
      disinfect: { name: 'html', allowedTags: ['u'] }
    }
  }
};

const data = {
  content: 'data to <b>disinfect</b>'
};

ajv.validate(schema, data);

RegEx sanitization

Also you have the ability to create regular expressions inside the schema. This example will leave just the "123".

import Ajv from 'ajv'
import ajvDisinfection from 'ajv-disinfection'
const ajv = new Ajv();
ajvDisinfection(ajv);

const schema = {
  type: 'object',
  properties: {
    content: {
      type: 'string',
      disinfect: { name: 'regex', regex: '\\d+' }
    }
  }
};

const data = {
  content: 'data to <b>disinfect</b> 123'
};

ajv.validate(schema, data);