From dd38edf1dede322e3d51bc2be1a4e7b41da6d4b1 Mon Sep 17 00:00:00 2001 From: yanickrochon Date: Tue, 20 Jun 2017 17:14:43 -0400 Subject: [PATCH] Array validation --- src/types/any.js | 13 +++++++++---- src/types/array.js | 4 +--- src/validators.js | 2 +- src/validators/types.js | 4 ++-- test/types/array.spec.js | 38 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/types/any.js b/src/types/any.js index c827485..f35a27c 100644 --- a/src/types/any.js +++ b/src/types/any.js @@ -14,12 +14,17 @@ Options: function anyValidator(field, specs) { const typeValidators = []; - if (specs.allowedTypes instanceof Array) { - for (var type of specs.allowedTypes) { - typeValidators.push(buildValidator(field, type)); - } + if ('allowedTypes' in specs) { + if (specs.allowedTypes instanceof Array) { + for (var type of specs.allowedTypes) { + typeValidators.push(buildValidator(field, type)); + } + } else { + throw new TypeError('Allowed types must be an array') + } } + if (typeValidators.length) { /** Validate the given value if it matches any of the types or undefined, and return the error message diff --git a/src/types/array.js b/src/types/array.js index e1f2741..ecc3de8 100644 --- a/src/types/array.js +++ b/src/types/array.js @@ -14,7 +14,7 @@ Options: function arrayValidator(field, specs) { const min = 'min' in specs ? specs.min : -Infinity; const max = 'max' in specs ? specs.max : Infinity; - const elementValidator = specs.elementType && (specs.elementType instanceof Array ? any(field, specs.elementType) : buildValidator(field, specs.elementType)); + const elementValidator = 'elementType' in specs ? buildValidator(field, specs.elementType) : undefined; /** Validate the given value if it is an array or undefined, and return the error message @@ -67,7 +67,5 @@ function arrayValidator(field, specs) { module.exports = arrayValidator; -const any = require('./any'); const validators = require('../validators'); - const buildValidator = validators.build; diff --git a/src/validators.js b/src/validators.js index 3278240..c148aa9 100644 --- a/src/validators.js +++ b/src/validators.js @@ -51,7 +51,7 @@ Unregister the given validator. @return {number} the validator count */ function unregisterValidator(validator) { - const validatorIndex = validators.indexOf(plugin); + const validatorIndex = validators.indexOf(validator); if (validatorIndex !== -1) { validators.splice(validatorIndex, 1); diff --git a/src/validators/types.js b/src/validators/types.js index 720edcb..9b5f4da 100644 --- a/src/validators/types.js +++ b/src/validators/types.js @@ -17,9 +17,9 @@ function typeValidator(field, specs, validator) { } else if (typeof specs.type === 'string') { specs.type = typeMap[specs.type] || userTypeMap[specs.type]; } else if (specs instanceof Array) { - specs = { type: Array, elementType: specs }; + specs = { type: Array, elementType: { type: anyValidator.Type, elementType: specs } }; } else if (specs.type instanceof Array) { - specs.elementType = specs.type; + specs.elementType = { type: anyValidator.Type, elementType: specs.type }; specs.type = Array; } else if (!('type' in specs)) { specs = { type: specs }; diff --git a/test/types/array.spec.js b/test/types/array.spec.js index c434862..1fab081 100644 --- a/test/types/array.spec.js +++ b/test/types/array.spec.js @@ -3,6 +3,7 @@ describe('Testing Array type validation', () => { const assert = require('assert'); const arrayValidator = require('../../src/types/array'); + const validators = require('../../src/validators'); const field = 'test'; @@ -54,4 +55,41 @@ describe('Testing Array type validation', () => { ].forEach(value => assert.strictEqual(validator(value), 'maxArray', 'Failed at validating max length : ' + JSON.stringify(value))); }); + it('should validate typed arrays', () => { + const validator = arrayValidator(field, { elementType: String }); + + [ + [], ['hello'], ['hello', 'world'] + ].forEach(value => assert.strictEqual(validator(value), undefined, 'Failed at validating typed array : ' + JSON.stringify(value))); + + [ + [null], [true], [false], [0], [1], + [[]], [{}], + ['hello', 0], [0, 'hello'], [0, 1] + ].forEach(value => assert.strictEqual(validator(value), 'invalidType', 'Failed at invalidating typed array : ' + JSON.stringify(value))); + }); + + it('should validated typed arrays (asynchronous)', () => { + const testValidator = function testValidator(field, specs) { + return function (value, ctx) { + return Promise.resolve(value === 'test' ? undefined : 'error'); + }; + }; + + validators.registerValidator(testValidator, 0); + + const validator = arrayValidator(field, { elementType: String }); + + return validator(['test']).then(result => { + assert.strictEqual(result, undefined, 'Failed at validating typed array'); + + return validator(['invalid']); + }).then(result => { + validators.unregisterValidator(testValidator); + + assert.strictEqual(result, 'error', 'Failed at validating typed array'); + }); + }); + + });