Skip to content

Commit

Permalink
Array validation
Browse files Browse the repository at this point in the history
  • Loading branch information
yanickrochon committed Jun 20, 2017
1 parent 113c06f commit dd38edf
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
13 changes: 9 additions & 4 deletions src/types/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions src/types/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -67,7 +67,5 @@ function arrayValidator(field, specs) {
module.exports = arrayValidator;


const any = require('./any');
const validators = require('../validators');

const buildValidator = validators.build;
2 changes: 1 addition & 1 deletion src/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/validators/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
38 changes: 38 additions & 0 deletions test/types/array.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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');
});
});


});

0 comments on commit dd38edf

Please sign in to comment.