diff --git a/lib/validate.js b/lib/validate.js index 82b66da..5deb3f3 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -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') { diff --git a/test/validations.test.js b/test/validations.test.js index e8a483b..ff12713 100644 --- a/test/validations.test.js +++ b/test/validations.test.js @@ -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', () => @@ -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', () => { @@ -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(); + }); });