Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add goodparts to project #46

Merged
merged 10 commits into from
Sep 28, 2016
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
coverage
npm-debug.log
.eslintrc*
config.env
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strictly shouldn't be in this PR but will let it slide just this once ;)

18 changes: 9 additions & 9 deletions lib/add_default_values.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
'use strict';

/**
* Initialse with the default values the schema if they are not defined Yet
* Write schema in the file if the default values are added
* @param {object} schema - the user schema object
* @return {boolean} - any changes apply to the schema
*/
var defaultSchema = {
email: {
type: 'email'
},
password: {
type: 'password'
}
email: { type: 'email' },
password: { type: 'password' }
};

module.exports = function (schema) {
var anyChanges = false;
Object.keys(defaultSchema).forEach(function(field) {
if(!schema.hasOwnProperty(field)){

Object.keys(defaultSchema).forEach(function (field) {
if (!schema.hasOwnProperty(field)) {
schema[field] = defaultSchema[field];
anyChanges = true;
};
}
});

return anyChanges;
};
24 changes: 13 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
'use strict';

var addDefaultValues = require('./add_default_values.js');
var loadSchema = require('./load_schema.js');
var updateSchema = require('./update_schema.js');
var fs = require('fs');

module.exports = function (server, options, next) {
if (!options.hasOwnProperty('user_schema_path')) {
return next('Error: user_schema_path is not defined, please define a user_schema_path');
return next(new Error('user_schema_path is not defined'));
}

loadSchema(options.user_schema_path, function(errorLoad, schema) {
return loadSchema(options.user_schema_path, function (errorLoad, schema) {
if (errorLoad) {
return next(errorLoad);
}

if (addDefaultValues(schema)) {
updateSchema(options.user_schema_path, schema, function(errorUpdateSchema) {
return next(errorUpdateSchema);
});
} else {
if (!addDefaultValues(schema)) {
return next();
}

return updateSchema(
options.user_schema_path,
schema,
function (errorUpdateSchema) {
return next(errorUpdateSchema);
});
});
};

module.exports.attributes = {
name: 'Abase'
};
module.exports.attributes = { name: 'Abase' };
22 changes: 16 additions & 6 deletions lib/load_schema.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
'use strict';

var fs = require('fs');

/**
* load the shema user
* load the schema user
* @param {sting} schemaPath - the file path of the schema
* @param {function} cb - callback with error and the schema object
* @returns {void}
*/
var fs = require('fs');

module.exports = function (schemaPath, cb) {
fs.readFile(schemaPath, function (err, data) {
var schema;

if (err) {
return cb('Error: sorry impossible to read the file at ' + schemaPath);
}

try {
var schema = JSON.parse(data);
schema = JSON.parse(data);
} catch (parseError) {
return cb('Error: the schema user file contains unconventianal type, please make sure an schema object is defined');
return cb(
'Error: the schema user file contains unconventianal type,'
+ 'please make sure an schema object is defined'
);
}
return cb(undefined, schema);

return cb(null, schema);
});
};
13 changes: 8 additions & 5 deletions lib/update_schema.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
'use strict';

var fs = require('fs');

/**
* Update the schema json file
* @param {string} user_schema_path, the path of the schema file
* @param {string} userSchemaPath, the path of the schema file
* @param {object} schema - the schema user object
* @param {function} cb - callback with error
* @returns {void}
*/

var fs = require('fs');
module.exports = function (user_schema_path, schema, cb) {
fs.writeFile(user_schema_path, JSON.stringify(schema), 'utf8', cb);
module.exports = function (userSchemaPath, schema, cb) {
fs.writeFile(userSchemaPath, JSON.stringify(schema), 'utf8', cb);
};
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "abase2",
"version": "1.0.8",
"name": "abase",
"version": "1.0.0",
"description": "A simple service to help people manage their personal data in your app.",
"main": "lib/index.js",
"repository": {
Expand All @@ -18,14 +18,15 @@
"codecov": "^1.0.1",
"eslint": "^3.5.0",
"hapi": "^15.1.0",
"goodparts": "^1.0.4",
"istanbul": "^0.4.5",
"pre-commit": "^1.1.3",
"tape": "^4.6.0"
},
"scripts": {
"test": "node_modules/.bin/tape ./test/*.test.js",
"lint": "node_modules/.bin/eslint .",
"lint:fix": "node_modules/.bin/eslint . --fix",
"lint": "node_modules/.bin/goodparts .",
"lint:fix": "node_modules/.bin/goodparts . --fix",
"coverage": "node_modules/.bin/istanbul cover node_modules/.bin/tape ./test/*.test.js",
"codecov": "node_modules/.bin/codecov --token=${CODECOV_TOKEN}"
},
Expand Down
23 changes: 11 additions & 12 deletions test/add_default_values.test.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
'use strict';

var tape = require('tape');
var addDefaultValues = require('../lib/add_default_values.js');

tape('add default values to empty object', function (t) {
var anyChanges = addDefaultValues({});

t.ok(anyChanges, 'Default values added to the schema');
t.end();
});

tape('add email to schema if not defined yet', function (t) {
var obj = {
password: {type: 'password'}
};

var obj = { password: { type: 'password' } };
var anyChanges = addDefaultValues(obj);

t.ok(anyChanges, 'Changes applied to the schema');
t.ok(obj.hasOwnProperty('email'), 'email added');
t.end();
});

tape('add password to schema if not defined yet', function (t) {
var obj = {
email: {type: 'email'}
};

var obj = { email: { type: 'email' } };
var anyChanges = addDefaultValues(obj);

t.ok(anyChanges, 'Changes applied to the schema');
t.ok(obj.hasOwnProperty('password'), 'password added');
t.end();
});

tape('do not add default values if they are already defined', function (t) {
var obj = {
email: {type: 'email'},
password: {type: 'password'}
email: { type: 'email' },
password: { type: 'password' }
};

var anyChanges = addDefaultValues(obj);

t.ok(!anyChanges, 'No changes applied to the schema');
t.end();
});
});
8 changes: 4 additions & 4 deletions test/helpers/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
*/
'use strict';

exports.beforeEach = function beforeEach (test, handler) {
exports.beforeEach = function (_test, handler) {
return function tapish (name, listener) {
test(name, function (assert) {
_test(name, function (assert) {
var _end = assert.end;

assert.end = function () {
Expand All @@ -19,9 +19,9 @@ exports.beforeEach = function beforeEach (test, handler) {
};
};

exports.afterEach = function afterEach (test, handler) {
exports.afterEach = function (_test, handler) {
return function tapish (name, listener) {
test(name, function (assert) {
_test(name, function (assert) {
var _end = assert.end;

assert.end = function () {
Expand Down
47 changes: 34 additions & 13 deletions test/load_schema.test.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,48 @@
'use strict';

var tape = require('tape');
var loadSchema = require('../lib/load_schema.js');
var path = require('path');

tape('attempt to load a schema with a wrong path file', function (t) {
loadSchema('./wrongpathoffile.json', function (error, schema) {
loadSchema('./wrongpathoffile.json', function (error) {
t.ok(error, 'can\'t read a file with a wrong path');
t.equal(error, 'Error: sorry impossible to read the file at ./wrongpathoffile.json', 'error message is passed to the callback');
t.equal(
error,
'Error: sorry impossible to read the file at ./wrongpathoffile.json',
'error message is passed to the callback'
);
t.end();
});
});

tape('the schema is not a json object', function (t) {
loadSchema(path.join(__dirname, 'fixtures', 'wrong_schema.json'), function (error, schema) {
t.ok(error, 'can\'t parse the content of the schema');
t.equal(error, 'Error: the schema user file contains unconventianal type, please make sure an schema object is defined', 'error schema parse displayed properly');
t.end();
});
loadSchema(
path.join(__dirname, 'fixtures', 'wrong_schema.json'),
function (error) {
t.ok(error, 'can\'t parse the content of the schema');
t.equal(
error,
'Error: the schema user file contains unconventianal type,'
+ 'please make sure an schema object is defined',
'error schema parse displayed properly'
);
t.end();
});
});

tape('load the schema', function (t) {
loadSchema(path.join(__dirname, 'fixtures', 'right_schema.json'), function (error, schema) {
t.ok(!error, 'no errors');
t.deepEqual(schema, {email: {type: "email"}, password: {type: "password"}}, 'the schema is loaded properly');
t.end();
});
});
loadSchema(
path.join(__dirname, 'fixtures', 'right_schema.json'),
function (error, schema) {
t.ok(!error, 'no errors');
t.deepEqual(
schema,
{
email: { type: 'email' }, password: { type: 'password' }
},
'the schema is loaded properly'
);
t.end();
});
});
7 changes: 5 additions & 2 deletions test/update_schema.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
'use strict';

var tape = require('tape');
var updateSchema = require('../lib/update_schema.js');
var path = require('path');

tape('attempt to update a schema with a wrong schema path', function (t) {
var wrongPath = path.join(__dirname, 'fixtures', 'wrongpath', 'schema.json');
updateSchema(wrongPath, {}, function (error, response) {

updateSchema(wrongPath, {}, function (error) {
t.ok(error, 'can\'t write a file with a wrong path');
t.end();
});
});
});