Skip to content
This repository has been archived by the owner on Nov 20, 2022. It is now read-only.
/ sanitizations Public archive

Setup sanitizations for object properties.

Notifications You must be signed in to change notification settings

LSVH/sanitizations

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sanitizations

Setup sanitization for a specific object structure.

API

Table of Contents

Schema

A Schema defines the configurations of how objects should be sanitized.

Parameters

  • instructions Object? instructions on how to sanitize (optional, default {})

Examples

const ambitiousReg = /[!@#$%^&*()[\]<>:;/\\]/g;

const schema = new Schema({
  title: {
    remove: ambitiousReg,
    transform: {
      from: /[._-]/g,
      to: ' '
    },
    convert: (input) =>
      input.split(' ').map(i =>
        i.charAt(0).toUpperCase() + i.substr(1).toLowerCase()
      ).join(' ')
    },
  slug: {
    remove: ambitiousReg,
    transform: {
      from: /[\s._]/g,
      to: '-'
    },
    convert: (input) => input.toLowerCase()
  }
});

const subject = {
  title: 'hell()O.woRld!',
  slug: 'example: /h@ello_world'
};

const expected = {
  title: 'Hello World',
  slug: 'example-hello-world'
};

const actual = schema.sanitize(subject);

expect(actual).toMatchObject(expected);

sanitize

Sanitize an object according to the instructions applied to the schema.

Parameters

  • input Object? object to sanitize

Returns Object a sanitized object

path

Get a property from the schema.

Parameters

  • propName string the property to select

Returns Object the property of the schema

mapInstructionsToProperty

Convert all plain JS objects to objects of the Property class.

Parameters

  • instructions Object object with properties

Returns Object object of properties with the supplied instructions

Property

A property of the sanitization schema.

Parameters

  • name String the property name in the schema
  • definition (null | Object)? the definitions of the sanitizers this property uses (optional, default null)

Examples

const schema = new Schema({ foo: { remove: /\s?bar\s?/ } });
const property = schema.path('foo');

const expected = 'chair';
const subject = 'bar chair';

const actual = property.sanitize(subject);

expect(actual).toMatch(expected);
const expected = { foo: 'chair' };
const subject = { foo: 'bar chair' };

const actual = property.sanitizeObject(subject);

expect(actual).toMatchObject(expected);

sanitize

Sanitize the input according to the schema property's definition.

Parameters

  • input any what to sanitize

Returns any the sanitized input

sanitizeObject

Sanitize a property of the input object what matches the schema property's name.

Parameters

  • input Object what to sanitize

Returns Object the input object with only the relevant property sanitized

About

Setup sanitizations for object properties.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published