Repository: TechnologyAdvice/obey
Pull request #33 (2016-03-15 15:04:31)

Load all type strategies at run


Patch:
diff --git a/README.md b/README.md
index 359bc70..353bd9b 100644
--- a/README.md
+++ b/README.md
@@ -145,7 +145,7 @@ When setting definitions for rules or model properties, the following are suppor
 
 ## Types
 
-**Reference: [Type Documentaion](/src/types)**
+**Reference: [Type Documentaion](/src/typeStrategies)**
 
 > Types are basic checks against native types, built-ins or customs. The library includes native types (`boolean`, `number`, `string`, `array`, and `object`) as well other common types. A [list of built-in types](/src/types) is contained in the source.
 
diff --git a/src/types/README.md b/src/typeStrategies/README.md
similarity index 100%
rename from src/types/README.md
rename to src/typeStrategies/README.md
diff --git a/src/types/any.js b/src/typeStrategies/any.js
similarity index 100%
rename from src/types/any.js
rename to src/typeStrategies/any.js
diff --git a/src/types/array.js b/src/typeStrategies/array.js
similarity index 100%
rename from src/types/array.js
rename to src/typeStrategies/array.js
diff --git a/src/types/boolean.js b/src/typeStrategies/boolean.js
similarity index 100%
rename from src/types/boolean.js
rename to src/typeStrategies/boolean.js
diff --git a/src/types/email.js b/src/typeStrategies/email.js
similarity index 100%
rename from src/types/email.js
rename to src/typeStrategies/email.js
diff --git a/src/typeStrategies/index.js b/src/typeStrategies/index.js
new file mode 100644
index 0000000..35d04ef
--- /dev/null
+++ b/src/typeStrategies/index.js
@@ -0,0 +1,30 @@
+// Import all strategies
+import any from './any'
+import array from './array'
+import boolean from './boolean'
+import email from './email'
+import ip from './ip'
+import number from './number'
+import object from './object'
+import phone from './phone'
+import string from './string'
+import url from './url'
+import uuid from './uuid'
+import zip from './zip'
+
+// Export object with all built-in strategies
+export default {
+  any,
+  array,
+  boolean,
+  email,
+  ip,
+  number,
+  object,
+  phone,
+  string,
+  url,
+  uuid,
+  zip
+}
+
diff --git a/src/types/ip.js b/src/typeStrategies/ip.js
similarity index 100%
rename from src/types/ip.js
rename to src/typeStrategies/ip.js
diff --git a/src/types/number.js b/src/typeStrategies/number.js
similarity index 100%
rename from src/types/number.js
rename to src/typeStrategies/number.js
diff --git a/src/types/object.js b/src/typeStrategies/object.js
similarity index 100%
rename from src/types/object.js
rename to src/typeStrategies/object.js
diff --git a/src/types/phone.js b/src/typeStrategies/phone.js
similarity index 100%
rename from src/types/phone.js
rename to src/typeStrategies/phone.js
diff --git a/src/types/string.js b/src/typeStrategies/string.js
similarity index 100%
rename from src/types/string.js
rename to src/typeStrategies/string.js
diff --git a/src/types/url.js b/src/typeStrategies/url.js
similarity index 100%
rename from src/types/url.js
rename to src/typeStrategies/url.js
diff --git a/src/types/uuid.js b/src/typeStrategies/uuid.js
similarity index 100%
rename from src/types/uuid.js
rename to src/typeStrategies/uuid.js
diff --git a/src/types/zip.js b/src/typeStrategies/zip.js
similarity index 100%
rename from src/types/zip.js
rename to src/typeStrategies/zip.js
diff --git a/src/types.js b/src/types.js
index 0a98ec2..1082341 100644
--- a/src/types.js
+++ b/src/types.js
@@ -2,6 +2,7 @@
  * Copyright (c) 2015 TechnologyAdvice
  */
 import _ from 'lodash'
+import strategies from './typeStrategies'
 
 /**
  * Types determine and execute the appropriate validation to be performed on the
@@ -13,7 +14,7 @@ const types = {
    * @memberof types
    * @property {Object} Contains type strategies
    */
-  strategies: {},
+  strategies,
 
   /**
    * Checks for and applies sub-type to definition
@@ -83,16 +84,10 @@ const types = {
       if (context.def.type.match(/[\/\\]/)) {
         throw new Error(`Illegal type name: ${context.def.type}`)
       }
-      try {
-        types.strategies[context.def.type] = require(`./types/${context.def.type}`).default
-      } catch (e) {
-        /* istanbul ignore else */
-        if (e.message.indexOf('Cannot find module') >= 0) {
-          throw new Error(`Type '${context.def.type}' does not exist`)
-        } else {
-          throw e
-        }
-      }
+    }
+    // Ensure type
+    if (!types.strategies[context.def.type]) {
+      throw new Error(`Type '${context.def.type}' does not exist`)
     }
     // Ensure subtype
     if (!types.strategies[context.def.type][context.def.sub]) {


Test patch:
diff --git a/test/src/types/any.spec.js b/test/src/typeStrategies/any.spec.js
similarity index 84%
rename from test/src/types/any.spec.js
rename to test/src/typeStrategies/any.spec.js
index 53c7a5f..edcc724 100644
--- a/test/src/types/any.spec.js
+++ b/test/src/typeStrategies/any.spec.js
@@ -1,5 +1,5 @@
 /* global describe, it, expect */
-import any from 'src/types/any'
+import any from 'src/typeStrategies/any'
 
 describe('type:any', () => {
   it('passes through (via return) the value', () => {
diff --git a/test/src/types/array.spec.js b/test/src/typeStrategies/array.spec.js
similarity index 97%
rename from test/src/types/array.spec.js
rename to test/src/typeStrategies/array.spec.js
index 319d3e5..4440d49 100644
--- a/test/src/types/array.spec.js
+++ b/test/src/typeStrategies/array.spec.js
@@ -1,5 +1,5 @@
 /* global describe, it, expect, sinon */
-import array from 'src/types/array'
+import array from 'src/typeStrategies/array'
 
 describe('type:array', () => {
   it('calls context.fail if type is not an array', () => {
diff --git a/test/src/types/boolean.spec.js b/test/src/typeStrategies/boolean.spec.js
similarity index 91%
rename from test/src/types/boolean.spec.js
rename to test/src/typeStrategies/boolean.spec.js
index b6ea0cd..76fb199 100644
--- a/test/src/types/boolean.spec.js
+++ b/test/src/typeStrategies/boolean.spec.js
@@ -1,5 +1,5 @@
 /* global describe, it, expect, sinon */
-import boolean from 'src/types/boolean'
+import boolean from 'src/typeStrategies/boolean'
 
 describe('type:boolean', () => {
   it('calls context.fail if type is not a boolean', () => {
diff --git a/test/src/types/email.spec.js b/test/src/typeStrategies/email.spec.js
similarity index 92%
rename from test/src/types/email.spec.js
rename to test/src/typeStrategies/email.spec.js
index e66c90e..201b46e 100644
--- a/test/src/types/email.spec.js
+++ b/test/src/typeStrategies/email.spec.js
@@ -1,5 +1,5 @@
 /* global describe, it, expect, sinon */
-import email from 'src/types/email'
+import email from 'src/typeStrategies/email'
 
 describe('type:email', () => {
   it('calls context.fail if type is not a valid email', () => {
diff --git a/test/src/types/ip.spec.js b/test/src/typeStrategies/ip.spec.js
similarity index 96%
rename from test/src/types/ip.spec.js
rename to test/src/typeStrategies/ip.spec.js
index 15c3da5..b305bc8 100644
--- a/test/src/types/ip.spec.js
+++ b/test/src/typeStrategies/ip.spec.js
@@ -1,5 +1,5 @@
 /* global describe, it, expect, sinon */
-import ip from 'src/types/ip'
+import ip from 'src/typeStrategies/ip'
 
 describe('type:ip', () => {
   it('calls context.fail if type is not a valid ipv4', () => {
diff --git a/test/src/types/number.spec.js b/test/src/typeStrategies/number.spec.js
similarity index 93%
rename from test/src/types/number.spec.js
rename to test/src/typeStrategies/number.spec.js
index e774d6d..0a324c9 100644
--- a/test/src/types/number.spec.js
+++ b/test/src/typeStrategies/number.spec.js
@@ -1,5 +1,5 @@
 /* global describe, it, expect, sinon */
-import number from 'src/types/number'
+import number from 'src/typeStrategies/number'
 
 describe('type:number', () => {
   it('calls context.fail if type is not a number', () => {
diff --git a/test/src/types/object.spec.js b/test/src/typeStrategies/object.spec.js
similarity index 98%
rename from test/src/types/object.spec.js
rename to test/src/typeStrategies/object.spec.js
index 2bf702f..aea7217 100644
--- a/test/src/types/object.spec.js
+++ b/test/src/typeStrategies/object.spec.js
@@ -1,5 +1,5 @@
 /* global describe, it, expect, sinon */
-import object from 'src/types/object'
+import object from 'src/typeStrategies/object'
 
 describe('type:object', () => {
   it('iterates sub-object if keys prop is present', () => {
diff --git a/test/src/types/phone.spec.js b/test/src/typeStrategies/phone.spec.js
similarity index 95%
rename from test/src/types/phone.spec.js
rename to test/src/typeStrategies/phone.spec.js
index b5da29a..4c1520a 100644
--- a/test/src/types/phone.spec.js
+++ b/test/src/typeStrategies/phone.spec.js
@@ -1,5 +1,5 @@
 /* global describe, it, expect, sinon */
-import phone from 'src/types/phone'
+import phone from 'src/typeStrategies/phone'
 
 describe('type:phone', () => {
   it('calls context.fail if value is not a valid phone number', () => {
diff --git a/test/src/types/string.spec.js b/test/src/typeStrategies/string.spec.js
similarity index 95%
rename from test/src/types/string.spec.js
rename to test/src/typeStrategies/string.spec.js
index b67278f..ada4d84 100644
--- a/test/src/types/string.spec.js
+++ b/test/src/typeStrategies/string.spec.js
@@ -1,5 +1,5 @@
 /* global describe, it, expect, sinon */
-import string from 'src/types/string'
+import string from 'src/typeStrategies/string'
 
 describe('type:string', () => {
   it('calls context.fail if type is not a string', () => {
diff --git a/test/src/types/url.spec.js b/test/src/typeStrategies/url.spec.js
similarity index 92%
rename from test/src/types/url.spec.js
rename to test/src/typeStrategies/url.spec.js
index 857b07e..49f1849 100644
--- a/test/src/types/url.spec.js
+++ b/test/src/typeStrategies/url.spec.js
@@ -1,5 +1,5 @@
 /* global describe, it, expect, sinon */
-import url from 'src/types/url'
+import url from 'src/typeStrategies/url'
 
 describe('type:url', () => {
   it('calls context.fail if type is not a valid URL', () => {
diff --git a/test/src/types/uuid.spec.js b/test/src/typeStrategies/uuid.spec.js
similarity index 92%
rename from test/src/types/uuid.spec.js
rename to test/src/typeStrategies/uuid.spec.js
index ca5ef85..5c70b0b 100644
--- a/test/src/types/uuid.spec.js
+++ b/test/src/typeStrategies/uuid.spec.js
@@ -1,5 +1,5 @@
 /* global describe, it, expect, sinon */
-import uuid from 'src/types/uuid'
+import uuid from 'src/typeStrategies/uuid'
 
 describe('type:uuid', () => {
   it('calls context.fail if type is not a valid UUID', () => {
diff --git a/test/src/types/zip.spec.js b/test/src/typeStrategies/zip.spec.js
similarity index 97%
rename from test/src/types/zip.spec.js
rename to test/src/typeStrategies/zip.spec.js
index 573e726..73f8c1b 100644
--- a/test/src/types/zip.spec.js
+++ b/test/src/typeStrategies/zip.spec.js
@@ -1,5 +1,5 @@
 /* global describe, it, expect, sinon */
-import zip from 'src/types/zip'
+import zip from 'src/typeStrategies/zip'
 
 describe('type:zip', () => {
   it('calls context.fail if value is not a valid zip code', () => {