io-filter
Simple to use lib to runtime-check the structure of javascript objects
It provides a way to :
- ensure that an object matches a structure
- verify data is valid (regexp, number intervals)
- omit unwanted properties
- cast objects with filters using custom type guards for TypeScript
install
The easiest way to install io-filter is with npm
.
npm install --save io-filter
test
Tested with mocha in TypeScript. Run tests with npm test
. You can alternatively use mocha dist/tests/test.js
after transpiling TypeScript with tsc
.
try
Here's a live example using io-filter
; ; ; filter = new ObjectFilter;console.log"Test 1 ->", filter.masko;// 'testIgnoredValue' will be ignored and omitted in the returned value /** Tests results *//** - Value not set */filter = new ObjectFilter;console.log"Test 2 ->", filter.masko; /** - Number in wrong interval */filter = new ObjectFilter;console.log"Test 3 ->", filter.masko; /** - RegExp not matching */filter = new ObjectFilter;console.log"Test 4 ->", filter.masko; /** - Wrong value type */filter = new ObjectFilter;console.log"Test 5 ->", filter.masko; /*Will output: Test 1 -> { message: { testRegExp: 'the string', testNumber: true, testValueType: [ 1, 2, 3 ], testExists: 'I exist' } } Test 2 -> undefined Test 3 -> undefined Test 4 -> undefined Test 5 -> undefined*/