Skip to content

Commit

Permalink
Any type validations tests complete
Browse files Browse the repository at this point in the history
  • Loading branch information
yanickrochon committed Jun 21, 2017
1 parent dd38edf commit 713024e
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 29 deletions.
13 changes: 7 additions & 6 deletions src/types/any.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ function anyValidator(field, specs) {
}
} 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 All @@ -37,15 +36,15 @@ function anyValidator(field, specs) {
const asyncResults = [];
var typeValidator, result;
var error = undefined;
var valid = true;
var valid = false;

for (typeValidator of typeValidators) {
result = typeValidator(value, ctx);

if (!message) {
if (!result) {
valid = true;
} else if ((typeof message === 'string') && (!error || (error === 'invalidType'))) {
error = message;
} else if ((typeof result === 'string') && (!error || (error === 'invalidType'))) {
error = result;
} else if (result instanceof Promise) {
asyncResults.push(result);
}
Expand All @@ -63,6 +62,8 @@ function anyValidator(field, specs) {

return !valid ? error : undefined;
});
} else {
return !valid ? error : undefined;
}
};
} else {
Expand Down
112 changes: 89 additions & 23 deletions test/types/any.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,126 @@ describe('Testing any type validation', () => {

const assert = require('assert');

const any = require('../../src/types/any');
const anyType = require('../../src/types/any');
const types = require('../../src/validators/types');

const field = 'test';

it('should validate "any" with no arguments', () => {
const type = any('test', {});
const type = anyType(field, {});

[
undefined, null, false, true, NaN, Infinity, [], {},
() => {}, new Date(), /./, "", "test", -1, 0, 1
].forEach(value => assert.strictEqual(type(value), undefined, 'Failed with : ' + JSON.stringify(value)));
});

it('should validate "any" with empty allowed types', () => {
const type = anyType(field, { allowedTypes: [] });

[
undefined, null, false, true, NaN, Infinity, [], {},
() => {}, new Date(), /./, "", "test", -1, 0, 1
].forEach(value => assert.strictEqual(type(value), undefined, 'Failed with : ' + JSON.stringify(value)));
});

it('should only accept an array for allowed types', () => {
[
undefined, null, true, false, NaN, -1, 0, 1,
'', 'test', () => {}, {}, /./, new Date()
].forEach(types => assert.throws(() => anyType(field, { allowedTypes: types }), 'Failed with : ' + JSON.stringify(types)));
});



describe('Testing any one type', () => {

it('should validate built-in', () => {
const type = anyType(field, { allowedTypes: [String] });

[
'', 'test'
].forEach(value => assert.strictEqual(type(value), undefined, 'Failed with : ' + JSON.stringify(value)));

[
-1, 0, 1, false, true, NaN,
[], {}, /./, new Date(), () => {}
].forEach(value => assert.strictEqual(type(value), 'invalidType', 'Failed with : ' + JSON.stringify(value)));

});

it('should validated asynchronously', () => {
const TestType = { type: 'Test' };
const testTypeValidator = function testValidator(field, specs) {
return function (value, ctx) {
return Promise.resolve(value === 'test' ? undefined : 'error');
};
};

types.registerType(TestType, testTypeValidator, ['TestType']);

const validator = anyType(field, { allowedTypes: ['TestType'] });

return;
return validator('test').then(result => {
assert.strictEqual(result, undefined, 'Failed at validating any');

return validator('invalid');
}).then(result => {
types.unregisterType(TestType);

it('should return "any" with arguments', () => {
const type = any(String, Boolean);
assert.strictEqual(result, 'error', 'Failed at validating any');
});
});

assert.strictEqual(any.isAny(type), true, 'Failed');
assert.strictEqual(type.length, 2, 'Not an array');
assert.strictEqual(type[0], String, 'Invalid type at index 0');
assert.strictEqual(type[1], Boolean, 'Invalid type at index 1');
});

it('should not be any', () => {
const type = [];

type.$any = Object.create(null, {
toString: {
configurable: false,
enumerable: false,
writable: false,
value: function toString() { return 'any'; }
}
describe('Testing any two types', () => {

it('should validate built-in', () => {
const type = anyType(field, { allowedTypes: [{ type: Number, min: 0 }, String ] });

[
'', 'test',
0, 1
].forEach(value => assert.strictEqual(type(value), undefined, 'Failed with : ' + JSON.stringify(value)));

assert.strictEqual(type(-1), 'minNumber', 'Failed to invalidate out of range');

[
false, true, NaN,
[], {}, /./, new Date(), () => {}
].forEach(value => assert.strictEqual(type(value), 'invalidType', 'Failed with : ' + JSON.stringify(value)));

});

assert.strictEqual(any.isAny(type), false, 'Failed');
it('should validated asynchronously', () => {
const TestType = { type: 'Test' };
const testTypeValidator = function testValidator(field, specs) {
return function (value, ctx) {
return Promise.resolve(value === 'test' ? undefined : 'error');
};
};

types.registerType(TestType, testTypeValidator, ['TestType']);

const validator = anyType(field, { allowedTypes: [{ type: Number, min: 0 }, 'TestType'] });

return Promise.all([
validator('test'),
validator('invalid'),
validator(0),
validator(-1)
]).then(messages => {
types.unregisterType(TestType);

assert.strictEqual(messages[0], undefined, 'Failed to validate async');
assert.strictEqual(messages[1], 'error', 'Failed to invalidate async');
assert.strictEqual(messages[2], undefined, 'Failed to validate number async');
assert.strictEqual(messages[3], 'minNumber', 'Failed to invalidate number async');
});
});

[
undefined, null, false, true, NaN, Infinity, [], {},
() => {}, new Date(), /./, "", "test", -1, 0, 1
].forEach(type => assert.strictEqual(any.isAny(type), false, 'Failed with : ' + JSON.stringify(type)));
});

});

0 comments on commit 713024e

Please sign in to comment.