hazmat
Version:
Validation and sanitization of input parameters
34 lines (23 loc) • 1.42 kB
Markdown
# About Hazmat #
Hazmat is a simple way to validate and sanitize inputs to externally exposed functions.
[Hazmat on github](https://github.com/ooyala/hazmat).
# Installation #
npm install hazmat
# Usage #
var config = {
log : console.log, // override default log method, the default will use console.log if available or be silent if not
fail : function(_reason, _data) {}, // override default fail method, the default will throw exception and write to log
warn : function(_reason, _data) {} // override default warn method, the default will write warning to log
};
var Hazmat = require('hazmat');
var hazmat = hazmat.create(config);
var param = hazmat.safeString('param name', 'foo'); // validates that 'foo' is valid string and returns
var param = hazmat.safeString('param name', 5); // validates that 5 is not valid string, calls fail() and returns null
var param = hazmat.safeString('param name', 5, 'default'); // validates that 5 is not valid string, calls warn() and returns 'default'
var param = hazmat.safeString('param name', 5, function(v) {return v.toString()}); // validates that 5 is not valid string, calls warn() and returns '5'
# Existing Validators #
hazmat.safeString()
hazmat.safeDomId()
hazmat.safeFunction()
# Existing Fixer Functions #
Hazmat.fixDomId // strips away all non-valid characters from given string