Skip to content

Commit

Permalink
add validator for peernames.json
Browse files Browse the repository at this point in the history
  • Loading branch information
antirek committed Aug 23, 2015
1 parent c010aac commit e7fecac
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 31 deletions.
42 changes: 20 additions & 22 deletions lib/source/fileSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,46 @@

var Q = require('q');

var FileSource = function (fileReader) {
var FileSource = function (fileReader, validator) {

var find = function (text, data) {
var defer = Q.defer();
var channel = null,
key = text.toLowerCase(),
json;
var key = text.toLowerCase();

try {
json = JSON.parse(data);
} catch (err) {
defer.reject(err);
}

var result = json.filter(function (element) {
var result = data.filter(function (element) {
var variants = element['variants'] || [];

var arr = variants.filter(function (variant){
var arr = variants.filter(function (variant) {
return variant.toLowerCase() === key;
});
if (arr.length > 0) {
return true;
} else {
return false;
}

return (arr.length > 0);
});

if (result.length > 0) {
if (result[0]['target']) {
defer.resolve(result[0]);
} else {
defer.reject(new Error('Lookup: finded object haven\'t target property'));
}
defer.resolve(result[0]);
} else {
defer.reject(new Error('Lookup: not found in source'));
}

return defer.promise;
};

var parse = function (data) {
var defer = Q.defer();
try {
var json = JSON.parse(data);
defer.resolve(json);
} catch (err) {
defer.reject(err);
}
return defer.promise;
};

this.lookup = function (text) {
return fileReader.readFile()
.then(parse)
.then(validator.validate)
.then(function (data) {
return find(text, data)
});
Expand Down
4 changes: 3 additions & 1 deletion lib/source/sourceFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var path = require('path');
var FileReader = require('./fileReader');
var Validator = require('./validator');

var Sources = {
File: function (options) {
Expand All @@ -10,7 +11,8 @@ var Sources = {
return path.resolve(datafile);
};
var filepath = getDataFilePath();
return new (require('./fileSource'))(new FileReader(filepath));
var validator = new Validator();
return new (require('./fileSource'))(new FileReader(filepath), validator);
}
};

Expand Down
29 changes: 29 additions & 0 deletions lib/source/validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

var Q = require('q');
var Joi = require('joi');

var Validator = function () {

var itemSchema = Joi.object().keys({
name: Joi.string().required(),
target: Joi.string().required(),
variants: Joi.array().items(Joi.string()).required().min(1)
});

var arraySchema = Joi.array().items(itemSchema);

this.validate = function (object) {
var defer = new Q.defer();
Joi.validate(object, arraySchema, function (err, value) {
if (err) {
defer.reject(err);
} else {
defer.resolve(value);
}
});
return defer.promise;
}
};

module.exports = Validator;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"dependencies": {
"ding-dong": "0.1.1",
"google-speech": "0.0.2",
"joi": "^6.0.8",
"joi": "^6.6.1",
"node-uuid": "^1.4.2",
"q": "^1.2.0",
"voicer-web": "0.0.4",
Expand All @@ -32,4 +32,4 @@
"url": "https://github.com/antirek/voicer/issues"
},
"homepage": "https://github.com/antirek/voicer"
}
}
18 changes: 12 additions & 6 deletions spec/FileSourceSpec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var FileSource = require('../lib/source/fileSource');
var Q = require('q');
var Validator = require('../lib/source/validator');

describe('FileSource', function () {
var expectedObject = {"name":"Дмитриев","target":"SIP/1234","variants":["дмитриев","дмитриев сергей"]};
Expand All @@ -18,21 +19,26 @@ describe('FileSource', function () {
return defer.promise;
};
};

var validator = new Validator();

it('return result if finder find result', function (done) {

var fileSource = new FileSource(new FileReader(content.good));
var fileSource = new FileSource(new FileReader(content.good), validator);

fileSource.lookup('Дмитриев')
.then(function (result) {
expect(result).toEqual(expectedObject);
done();
})
.fail(function (err) {
console.log('1234', err);
});
});

it('return error if finder not find result', function (done) {

var fileSource = new FileSource(new FileReader(content.empty));
var fileSource = new FileSource(new FileReader(content.empty), validator);

fileSource.lookup('Дмитриев')
.fail(function (error) {
Expand All @@ -43,7 +49,7 @@ describe('FileSource', function () {

it('return error if finder find result but not have channel', function (done) {

var fileSource = new FileSource(new FileReader(content.good_without_channel));
var fileSource = new FileSource(new FileReader(content.good_without_channel), validator);

fileSource.lookup('Дмитриев')
.fail(function (error) {
Expand All @@ -54,11 +60,11 @@ describe('FileSource', function () {

it('return error if finder get broken json', function (done) {

var fileSource = new FileSource(new FileReader(content.bad));
var fileSource = new FileSource(new FileReader(content.bad), validator);

fileSource.lookup('Дмитриев')
.fail(function (err) {
expect(err instanceof TypeError).toBe(true);
.fail(function (err) {
expect(err instanceof SyntaxError).toBe(true);
done();
});
});
Expand Down
60 changes: 60 additions & 0 deletions spec/ValidatorSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

var Validator = require('../lib/source/validator');

describe('Validator', function () {
var validator = new Validator();

var goodItem1 = {
"name": "Дмитриев Сергей",
"target": "SIP/Sf745025",
"variants": [
"дмитриев",
"сергей",
]
};

var goodItem2 = {
"name": "Студеная вода",
"target": "Local/2555575@AC000032",
"variants": [
"студеная вода",
"заповедная вода",
"вода"
]
};

var badItem1 = {
"name": "sadas",
"target": "safsafsaf",
"variants": []
};

var badItem2 = {
"target": "safsafsaf",
"variants": ["12", "13"]
};

it('validate good array peernames.json', function (done) {
var array = [goodItem1, goodItem2];
validator.validate(array)
.then(function (value) {
done();
});
});

it('not validate without variants', function (done) {
var array = [goodItem1, badItem1];
validator.validate(array)
.then(console.log, function (err) {
done();
})
});

it('not validate without name', function (done) {
var array = [goodItem1, badItem2];
validator.validate(array)
.then(console.log, function (err) {
done();
})
});
});

0 comments on commit e7fecac

Please sign in to comment.