Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Invalid value error thrown on intersection value within struct #220

Closed
chrisui opened this issue Jul 4, 2016 · 3 comments
Closed

Invalid value error thrown on intersection value within struct #220

chrisui opened this issue Jul 4, 2016 · 3 comments

Comments

@chrisui
Copy link

chrisui commented Jul 4, 2016

Version

3.2.3

Expected behaviour

Should not error on intersection type as property of struct.

Actual behaviour

Invalid value error.

/Users/chris/Projects/testtest/node_modules/tcomb/lib/fail.js:2
  throw new TypeError('[tcomb] ' + message);
  ^

TypeError: [tcomb] Invalid value {
  "string": "Test"
} supplied to Struct{a: Struct{string: {String | <function1>}} & Struct{string: {String | <function1>}}}/a: Struct{string: {String | <function1>}} & Struct{string: {String | <function1>}}
    at Function.fail (/Users/chris/Projects/testtest/node_modules/tcomb/lib/fail.js:2:9)
    at assert (/Users/chris/Projects/testtest/node_modules/tcomb/lib/assert.js:14:12)
    at Intersection (/Users/chris/Projects/testtest/node_modules/tcomb/lib/intersection.js:27:7)
    at create (/Users/chris/Projects/testtest/node_modules/tcomb/lib/create.js:10:66)
    at new Struct (/Users/chris/Projects/testtest/node_modules/tcomb/lib/struct.js:82:19)
    at Object.validateStruct [as struct] (/Users/chris/Projects/testtest/node_modules/tcomb-validation/index.js:173:17)
    at recurse (/Users/chris/Projects/testtest/node_modules/tcomb-validation/index.js:71:38)
    at Function.validate (/Users/chris/Projects/testtest/node_modules/tcomb-validation/index.js:66:31)
    at Object.<anonymous> (/Users/chris/Projects/testtest/run.js:28:18)
    at Module._compile (module.js:413:34)

Steps to reproduce

Run the following code (note how the alternative commented lines work fine):

const t = require('tcomb-validation');

const Min = t.refinement(t.String, (s) => s.length > 2);
const Max = t.refinement(t.String, (s) => s.length < 5);

const TypeA = t.struct({string: Min});
const TypeB = t.struct({string: Max});

const Type = t.struct({
  test: t.intersection([TypeA, TypeB])
});

const val = {
  test: {
    string: 'Test'
  }
};

//const Type = t.intersection([TypeA, TypeB]);
//const val = {string: 'Test'};

const result = t.validate(val, Type);
console.log(result);

Stack trace and console log

Will follow up with this if required!

Thanks in advance! :)

Ps. Sorry if this is wrong repo, my guess was this is more core tcomb rather than actually validation related

@gcanti
Copy link
Owner

gcanti commented Jul 4, 2016

Hi @chrisui,

thanks for the detailed report. This is an edge case: please note how, since structs are nominal (i.e. their is functions are implemented with instanceof) this set is empty:

t.intersection([TypeA, TypeB])

so, from a theoretical point of view, the current behavior is correct.

I suggest to use interface instead of struct:

const Min = t.refinement(t.String, (s) => s.length > 2);
const Max = t.refinement(t.String, (s) => s.length < 5);

const TypeA = t.interface({string: Min}); // <= interface
const TypeB = t.interface({string: Max}); // <= interface

const Type = t.struct({
  test: t.intersection([TypeA, TypeB])
});

const val = {
  test: {
    string: 'Test'
  }
};

const result = t.validate(val, Type);
console.log(result);

The alternative commented lines work fine because... you have actually found a bug in tcomb-validation so this issue is in the right wrong place :)

@gcanti gcanti added bug and removed bug labels Jul 4, 2016
@chrisui
Copy link
Author

chrisui commented Jul 4, 2016

Gotcha! Thanks for the aid and glad to be of some help. ;)

@gcanti
Copy link
Owner

gcanti commented Jul 4, 2016

Closing in favour of gcanti/tcomb-validation#42

@gcanti gcanti closed this as completed Jul 4, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants