0.0.1 • Published 4 years ago
kempt v0.0.1
Kempt
Kempt is a data coercion package.
Kempt allows you to define barriers that will enforce constraints on data that goes through them. Nonconforming data will be coerced to a valid value and a helpful warning will be printed in the console.
Kempt is a work-in-progress.
Example
import * as K from 'kempt';
const personType = K.object({
name: K.string.fallback(''),
location: K.vector(K.number.fallback(0), 3),
friends: K.list(K.string.fallback('')),
});
const dirtyData = [
// a valid person
{
name: 'Marcus',
location: [120, 45, 607.3],
friends: ['Marcel'],
},
// name was corrupted
{
name: null,
location: [120, 45, 607.3],
friends: ['Marcel'],
},
// location was corrupted
{
name: 'Marcus',
location: [120, NaN],
friends: ['Marcel'],
},
// friends and location was corrupted
{
name: 'Marcus',
location: undefined,
friends: undefined,
},
];
const cleanData = dirtyData.map(d => personType.coerce(d));
// ^^^ Fills in the corruption and prints the following to the console:
/*
[kempt warning] Invalid value encountered
Received val: { name: null, location: [ 120, 45, 607.3 ], friends: [ 'Marcel' ] }
Issues:
- val.name isn't a string
Value patched to: { name: '', location: [ 120, 45, 607.3 ], friends: [ 'Marcel' ] }
[kempt warning] Invalid value encountered
Received val: { name: 'Marcus', location: [ 120, NaN ], friends: [ 'Marcel' ] }
Issues:
- val.location[1] is NaN
- val.location[2] is missing
Value patched to: { name: 'Marcus', location: [ 120, 0 ], friends: [ 'Marcel' ] }
[kempt warning] Invalid value encountered
Received val: { name: 'Marcus', location: undefined, friends: undefined }
Issues:
- val.location isn't an array
- val.friends isn't an array
Value patched to: { name: 'Marcus', location: [ 0, 0, 0 ], friends: [] }
*/
0.0.1
4 years ago