lib/index.js
function checkLocalCircular (schema) {
  const dag = new DAG()
  const locals = traverse(schema).reduce(function (acc, node) {
    if (!_.isNull(node) && !_.isUndefined(null) && typeof node.$ref === 'string') {
      const refType = utils.getRefType(node)
      if (refType === 'local') {
        const value = utils.getRefValue(node)
        if (value) {
          const path = this.path.join('/')
          acc.push({
            from: path,
            to: value
          })
        }
      }
    }
    return acc
  }, [])

  if (!locals || !locals.length) {
    return
  }

  if (_.some(locals, elem => elem.to === '#')) {
    return new Error('Circular self reference')
  }

  const check = _.find(locals, elem => {
    const fromEdge = elem.from.concat('/')
    const dest = elem.to.substring(2).concat('/')
    try {
      dag.addEdge(fromEdge, dest)
    } catch (e) {
      return elem
    }

    if (fromEdge.indexOf(dest) === 0) {
      return elem
    }
  })

  if (check) {
    return new Error(`Circular self reference from ${check.from} to ${check.to}`)
  }
}
function checkLocalCircular (schema) {
  const dag = new DAG()
  const locals = traverse(schema).reduce(function (acc, node) {
    if (!_.isNull(node) && !_.isUndefined(null) && typeof node.$ref === 'string') {
      const refType = utils.getRefType(node)
      if (refType === 'local') {
        const value = utils.getRefValue(node)
        if (value) {
          const path = this.path.join('/')
          acc.push({
            from: path,
            to: value
          })
        }
      }
    }
    return acc
  }, [])

  if (!locals || !locals.length) {
    return
  }

  if (_.some(locals, elem => elem.to === '#')) {
    return new Error('Circular self reference')
  }

  const check = _.find(locals, elem => {
    const fromEdge = elem.from.concat('/')
    const dest = elem.to.substring(2).concat('/')
    try {
      dag.addEdge(fromEdge, dest)
    } catch (e) {
      return elem
    }

    if (fromEdge.indexOf(dest) === 0) {
      return elem
    }
  })

  if (check) {
    return new Error(`Circular self reference from ${check.from} to ${check.to}`)
  }
}
function checkLocalCircular ( schema ) { const dag = new DAG ( ) const locals = traverse ( schema ) . reduce ( function ( acc , node ) { if ( ! _ . isNull ( node ) && ! _ . isUndefined ( null ) && typeof node . $ref === 'string' ) { const refType = utils . getRefType ( node ) if ( refType === 'local' ) { const value = utils . getRefValue ( node ) if ( value ) { const path = this . path . join ( '/' ) acc . push ( { from : path , to : value } ) } } } return acc } , [ ] ) if ( ! locals || ! locals . length ) { return } if ( _ . some ( locals , elem => elem . to === '#' ) ) { return new Error ( 'Circular self reference' ) } const check = _ . find ( locals , elem => { const fromEdge = elem . from . concat ( '/' ) const dest = elem . to . substring ( 2 ) . concat ( '/' ) try { dag . addEdge ( fromEdge , dest ) } catch ( e ) { return elem } if ( fromEdge . indexOf ( dest ) === 0 ) { return elem } } ) if ( check ) { return new Error ( ` ${ check . from } ${ check . to } ` ) } }
Check the schema for local circular refs using DAG
@param {Object} schema the schema
@return {Error|undefined} <code>Error</code> if circular ref, <code>undefined</code> otherwise if OK
@private
Check the schema for local circular refs using DAG