gkz/type-check

Using more than one custom type on the same object fails

trodrigues opened this issue · 0 comments

Hi there,

I was trying to create a type definition where I'd have two custom types being used on two separate properties of an object.

Essentially, something like: '{prop1: CustomType1, prop2: CustomType2}'

However, this seems to fail in a very weird way if my custom type reuses type-check itself to validate something.

But it doesn't fail if there is only one custom type being used. I created 2 isolated test cases to demonstrate. The first one doesn't fail, but the second one does. Also, the inner assert inside the validation method is passing, so the inner use of typeCheck is working properly.

var typeCheck = require('type-check').typeCheck
var assert = require('assert')

// asserts to true
assert(typeCheck('{prop1: CustomType1, prop2: String}', {
  prop1: {
    subprop1: 'str1'
  },
  prop2: 'str2'
}, {
  customTypes: {
    CustomType1: {
      typeOf: 'Object',
      validate: function (x) {
        var firstcheck = typeCheck('String', x.subprop1)
        assert(firstcheck, 'first check')
        return firstcheck
      }
    }
  }
}))

// asserts to false
assert(typeCheck('{prop1: CustomType1, prop2: CustomType2}', {
  prop1: {
    subprop1: 'str1'
  },
  prop2: {
    subprop2: 'str2'
  }
}, {
  customTypes: {
    CustomType1: {
      typeOf: 'Object',
      validate: function (x) {
        var firstcheck = typeCheck('String', x.subprop1)
        assert(firstcheck, 'first check')
        return firstcheck
      }
    },
    CustomType2: {
      typeOf: 'Object',
      validate: function (x) {
        var secondcheck = typeCheck('String', x.subprop2)
        assert(secondcheck, 'second check')
        return secondcheck
      }
    }
  }
}))

I tried debugging this and stepping into the code of the library but couldn't really figure out where and why it fails.