Skip to content

Instantly share code, notes, and snippets.

@scorsi
Created November 29, 2018 12:58
Show Gist options
  • Save scorsi/8ebe2da51f849a9dd13043381d9b3d85 to your computer and use it in GitHub Desktop.
Save scorsi/8ebe2da51f849a9dd13043381d9b3d85 to your computer and use it in GitHub Desktop.
Clean data and check for validity
function cleanData(data, authorizedData) {
if (typeof data === 'undefined')
throw new Error('data should not be undefined');
if (typeof data !== 'object')
throw new Error('data should be an object');
var ndata = {};
for (var filterName in authorizedData) {
if (Object.hasOwnProperty.call(authorizedData, filterName)) {
var filter = authorizedData[filterName];
if (Object.hasOwnProperty.call(data, filterName)) {
if (typeof data[filterName] === filter.type) {
ndata[filterName] = data[filterName];
} else if (filter.type === 'number' && typeof data[filterName] === 'string') {
var t = parseInt(data[filterName]);
if (!isNaN(t)) ndata[filterName] = t;
else throw new Error('attribute ' + filterName + ' should be of type ' + filter.type);
} else {
throw new Error('attribute ' + filterName + ' should be of type ' + filter.type);
}
} else {
if (filter.optional === false) {
throw new Error('attribute ' + filterName + ' is required');
}
}
}
}
return ndata;
}
var nbError = 0;
var nbSuccess = 0;
function assert(message, bool) {
if (bool === true) {
nbSuccess++;
console.log(message + ': OK');
}
else {
nbError++;
console.log(message + ': ASSERTION ERROR');
}
}
function displayTestingResult() {
console.log('There are ' + nbError + ' failed test.');
console.log('There are ' + nbSuccess + ' successful test.');
}
function testing(notCleanedData, authorizedData, testFunction) {
var cleanedData = {};
var err = undefined;
try {
cleanedData = cleanData(notCleanedData, authorizedData);
} catch (e) {
err = e.message;
}
console.log('notCleanedData:', notCleanedData);
console.log('authorizedData:', authorizedData);
console.log('cleanedData:', cleanedData);
console.log();
testFunction(notCleanedData, authorizedData, cleanedData, err);
console.log();
}
console.log('Testing attributes types...');
console.log();
testing(
{
email: 'toto'
},
{
email: {
type: 'string',
optional: false
}
},
function (notCleanedData, authorizedData, cleanedData, err) {
assert('CleanedData should not throw an error', typeof err === 'undefined');
assert('CleanedData should have email attribute of type string', typeof cleanedData.email === authorizedData.email.type);
});
console.log('----------------------------------------------');
console.log();
testing(
{
note: 3
},
{
note: {
type: 'number',
optional: false
}
},
function (notCleanedData, authorizedData, cleanedData, err) {
assert('CleanedData should not throw an error', typeof err === 'undefined');
assert('CleanedData should have note attribute of type number', typeof cleanedData.note === authorizedData.note.type);
});
console.log('----------------------------------------------');
console.log();
testing(
{
note: '3'
},
{
note: {
type: 'number',
optional: false
}
},
function (notCleanedData, authorizedData, cleanedData, err) {
assert('CleanedData should not throw an error', typeof err === 'undefined');
assert('CleanedData should have note attribute of type number', typeof cleanedData.note === authorizedData.note.type);
});
console.log('----------------------------------------------');
console.log();
testing(
{
note: 'not a number'
},
{
note: {
type: 'number',
optional: false
}
},
function (notCleanedData, authorizedData, cleanedData, err) {
assert('CleanedData should throw an error', typeof err !== 'undefined');
});
console.log();
console.log('========================================================================');
console.log();
console.log('Testing attributes required/optional...');
console.log();
testing(
{
firstname: 'a',
lastname: 'b'
},
{
firstname: {
type: 'string',
optional: true
},
lastname: {
type: 'string',
optional: true
},
email: {
type: 'string',
optional: true
}
},
function (notCleanedData, authorizedData, cleanedData, err) {
assert('CleanedData should not throw an error', typeof err === 'undefined');
assert('CleanedData should not have email attribute', typeof cleanedData.email === 'undefined');
assert('CleanedData should have firstname attribute', typeof cleanedData.firstname === authorizedData.firstname.type);
assert('CleanedData should have lastname attribute', typeof cleanedData.lastname === authorizedData.lastname.type);
});
console.log('----------------------------------------------');
console.log();
testing(
{
firstname: 'a',
lastname: 'b'
},
{
firstname: {
type: 'string',
optional: false
},
lastname: {
type: 'string',
optional: false
},
email: {
type: 'string',
optional: true
}
},
function (notCleanedData, authorizedData, cleanedData, err) {
assert('CleanedData should not throw an error', typeof err === 'undefined');
assert('CleanedData should not have email attribute', typeof cleanedData.email === 'undefined');
assert('CleanedData should have firstname attribute', typeof cleanedData.firstname === authorizedData.firstname.type);
assert('CleanedData should have lastname attribute', typeof cleanedData.lastname === authorizedData.lastname.type);
});
console.log('----------------------------------------------');
console.log();
testing(
{},
{
note: {
type: 'number',
optional: false
}
},
function (notCleanedData, authorizedData, cleanedData, err) {
assert('CleanedData should throw an error', typeof err !== 'undefined');
});
console.log('----------------------------------------------');
console.log();
testing(
{
note: 7
},
{
note: {
type: 'number',
optional: false
},
email: {
type: 'string',
optional: false
}
},
function (notCleanedData, authorizedData, cleanedData, err) {
assert('CleanedData should throw an error', typeof err !== 'undefined');
});
console.log();
console.log('========================================================================');
console.log();
console.log('Testing attributes not present in authorizedData...');
console.log();
testing(
{
email: 'a',
firstname: 'b'
},
{
email: {
type: 'string',
optional: true
}
},
function (notCleanedData, authorizedData, cleanedData, err) {
assert('CleanedData should not throw an error', typeof err === 'undefined');
assert('CleanedData should have email attribute', typeof cleanedData.email === authorizedData.email.type);
assert('CleanedData should not have firstname attribute', typeof cleanedData.firstname === 'undefined');
});
console.log();
console.log('========================================================================');
console.log();
displayTestingResult();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment