Skip to content

Commit

Permalink
Revises Validation Utilities
Browse files Browse the repository at this point in the history
This commit revises the type validation utilities.

Signed-off-by: Lui de la Parra <[email protected]>
  • Loading branch information
Luidog committed Oct 27, 2018
1 parent f1bc26f commit 5a4815c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
2 changes: 0 additions & 2 deletions lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ const isType = (value, type) => {
return isArray(value);
} else if (type === Object) {
return isObject(value);
} else if (type === undefined || null) {
throw new Error(`Unsupported type: ${type}`);
} else if (type.documentClass && type.documentClass() === 'document') {
return isDocument(value) || Client().isNativeId(value);
} else if (type.documentClass && type.documentClass() === 'embedded') {
Expand Down
20 changes: 18 additions & 2 deletions test/validations.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
/* global describe it */

const { expect } = require('chai');
const { isType, isSupportedType } = require('../lib/validate');
const { EmbeddedDocument } = require('../lib/index.js');
const { isType, isSupportedType, isValidType } = require('../lib/validate');

describe('Validation Utility Tests', () => {
it('should throw an error if the type is unsupported', () =>
Expand All @@ -12,8 +13,19 @@ describe('Validation Utility Tests', () => {
describe('Type Detection', () => {
let array = ['one', 'two', 'three'];
it('should detect Arrays', () => expect(isType(array, Array)).to.be.true);
it('should detect Embedded Documents', () => {
class EmbeddedDoc extends EmbeddedDocument {
constructor() {
super();
this.schema({});
}
}
const embedded = new EmbeddedDoc();
return expect(isType(embedded, EmbeddedDoc)).to.be.true;
});
it('should reject undefined', () =>
expect(() => isType(undefined, undefined)).to.throw);
expect(() => isType(undefined, undefined)).to.throw());
it('should reject null', () => expect(() => isType(null, null)).to.throw);
});

describe('Supported Type Detection', () => {
Expand All @@ -28,4 +40,8 @@ describe('Validation Utility Tests', () => {
it('should support booleans', () =>
expect(isSupportedType(Boolean)).to.be.true);
});

describe('it rejects multiple types on one property', () => {
return expect(() => isValidType(1, [Number, String])).to.throw();
});
});

0 comments on commit 5a4815c

Please sign in to comment.