Skip to content

Commit

Permalink
CHANGE array type validation by array field
Browse files Browse the repository at this point in the history
  • Loading branch information
Боронин Дмитрий Алексеевич committed Sep 12, 2016
1 parent 2cbc336 commit 1408a6c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 35 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ function check(field, data, key) {
}
}

if (!Type(field, data, key)) return error(key, 'Wrong type of field!');

if (field.array && data[key]) {
var result = null;

Expand All @@ -51,8 +53,6 @@ function check(field, data, key) {
if (result && !result.success) return error(key + '.' + i, result.error.text);
}

if (!Type(field, data, key)) return error(key, 'Wrong type of field!');

if (field.enum && field.enum.constructor && field.enum.constructor !== Array) return error(key, 'Wrong data for enum!');
if (!Enum(field, data, key)) return error(key, 'Value is not part of enum!');

Expand Down
14 changes: 8 additions & 6 deletions lib/type.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module.exports = function (field, data, key) {
if (field.type && data.hasOwnProperty(key)) {
if (!data.hasOwnProperty(key)) return true;

if (field.array) {
return data[key].constructor && data[key].constructor === Array;
}

if (field.type) {
field.type = field.type.toLowerCase();

if (field.type === "array") {
if (data[key].constructor && data[key].constructor !== Array) return false;
} else {
if (typeof data[key] !== field.type) return false;
}
if (typeof data[key] !== field.type) return false;
}

return true;
Expand Down
17 changes: 17 additions & 0 deletions test/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ describe('Array', function() {
expect(result.success).to.equal(true);
});

it('field with array but not array type => error', function () {
var scheme = validator({
test : {
array : {
type : "string"
}
}
});

var result = scheme.validate({ test : "123" });
expect(result).to.be.a("object");
expect(result.success).to.equal(false);
expect(result.error).to.be.a("object");
expect(result.error.key).to.equal("test");
expect(result.error.text).to.equal("Wrong type of field!");
});

it('field with array and bad validation => error', function () {
var scheme = validator({
test : {
Expand Down
27 changes: 0 additions & 27 deletions test/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,6 @@ describe('Type', function() {
expect(result.error.text).to.equal("Wrong type of field!");
});

it('field with type array and wrong type => error', function () {
var scheme = validator({
test : {
type : "array"
}
});

var result = scheme.validate({ test : {} });
expect(result).to.be.a("object");
expect(result.success).to.equal(false);
expect(result.error).to.be.a("object");
expect(result.error.key).to.equal("test");
expect(result.error.text).to.equal("Wrong type of field!");
});

it('field with type and right type => success', function () {
var scheme = validator({
test : {
Expand All @@ -56,18 +41,6 @@ describe('Type', function() {
expect(result.success).to.equal(true);
});

it('field with type array and right type => success', function () {
var scheme = validator({
test : {
type : "array"
}
});

var result = scheme.validate({ test : [ {}, {} ] });
expect(result).to.be.a("object");
expect(result.success).to.equal(true);
});

it('field type not case sensitive', function () {
var scheme = validator({
test : {
Expand Down

0 comments on commit 1408a6c

Please sign in to comment.