-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
126 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}) | ||
}); | ||
}); |