'use strict'
const schemaValidator = module.exports = { }

schemaValidator.validate = function (resources) {
  Object.keys(resources).forEach(resource => {
    Object.keys(resources[resource].attributes).forEach(attribute => {
      let joiSchema = resources[resource].attributes[attribute]
      if (!joiSchema._settings) return

      let types = joiSchema._settings.__one || joiSchema._settings.__many
      types.forEach(type => {
        if (!resources[type]) {
          throw new Error(`'${resource}'.'${attribute}' is defined to hold a relation with '${type}', but '${type}' is not a valid resource name!`)
        }
      })
      let foreignRelation = joiSchema._settings.__as
      if (!foreignRelation) return

      let backReference = resources[types[0]].attributes[foreignRelation]
      if (!backReference) {
        throw new Error(`'${resource}'.'${attribute}' is defined as being a foreign relation to the primary '${types[0]}'.'${foreignRelation}', but that primary relationship does not exist!`)
      }
    })
  })
}

@@ -226,18 +226,20 @@
 %7B%0A      
-le
+cons
 t joiSch
@@ -332,18 +332,20 @@
 %0A%0A      
-le
+cons
 t types 
@@ -637,18 +637,20 @@
 )%0A      
-le
+cons
 t foreig
@@ -728,18 +728,20 @@
 %0A%0A      
-le
+cons
 t backRe

Validate resource schema are valid on startup