gcanti / tcomb-validation

Validation library based on type combinators
MIT License
400 stars 23 forks source link

`tcomb-validation` error message doesn't contain struct name #56

Open ArmorDarks opened 7 years ago

ArmorDarks commented 7 years ago

For some reason tcomb-validation outputs different from tcomb error message — it doesn't account for name option of t.struct.

Example:

const Schema = t.struct({
  name: t.String,
  surname: t.maybe(t.String),
  age: t.Integer,
  tags: t.list(t.String)
}, { name: 'Person', strict: true })

Schema(data)
console.log(tv.validate(data, Schema).errors)

tcomb will produce

[tcomb] Invalid value undefined supplied to Person/name: String

tcomb-validation will produce

Invalid value undefined supplied to /name: String

Note how tcomb prints Person as name from struct, but tcomb-validate does not.

Is it per design?

gcanti commented 7 years ago

Is it per design?

Yes (in order to handle nested types), however you can change the default behaviour adding a prefix through the path option

function myvalidate(value, type) {
  return t.validate(value, type, { path: [type.displayName] })
}
ArmorDarks commented 7 years ago

Hm, I see. Though, it's unclear, why tcomb-validate can't try to access struct's name at first place, without need to specify path?

Also, maybe it worth to add in readme?

Obviously, I'm not familiar with all pitfalls and tcomb is very new for me, so, I'm sorry if I'm raising something that already has been discussed hundreds times.

gcanti commented 7 years ago

IIRC validations are assumed to be relative (hence the "/name" string in the error message) to a root type which is never mentioned, except in this trivial case

console.log(t.validate(1, Schema).errors) // => Invalid value 1 supplied to Person

The path of the root however can be customized through the path option.

(I don't necessarily agree with the old me on this topic, I'm just explaining why it works like that)

ArmorDarks commented 7 years ago

Hm, I'm quite confused. I'm probably missing something...

So, this

console.log(t.validate(1, Schema).errors) // => Invalid value 1 supplied to Person

will mention Schema name (Person)?

But this one will not?

console.log(tv.validate(data, Schema).errors) // => Invalid value undefined supplied to /name: String