Closed fernandocanizo closed 7 years ago
Old but related https://github.com/gcanti/tcomb-validation/issues/3#issuecomment-55791609
TLDR tcomb just throws and is meant to drive out bugs, tcomb-validation returns a list of errors and is meant to validate input
(tcomb can be derived from tcomb-validation but at that time I wrote tcomb first)
Hello Giulio,
So I think you may start a FAQ now :stuck_out_tongue_closed_eyes: ... Or maybe just state the difference explained in your answer to Brian in a prominent place, in both projects.
I'm traversing the path backwards than Brian: I'd like to drop Joi and use tcomb for everything.
So let me see if I understood this correctly:
const foo = (data) => { const name = t.String(data.name); // ... };
So if a thing that's not a String
reaches data.name
it will throw.
Am I correct?
P.S.: also I'd like to suggest that you use tv
to hold tcomb-validation
and t
just for tcomb
, maybe the sample code made it look like the same thing in my head.
Yes, you nailed it. tcomb-validation just augments tcomb's export with the validate
function, runtime types are the same.
Speaking of TypeScript you may also want to take a look at io-ts. It's basically a modern tcomb-validation, compatible with TypeScript. There's no assert
function, but is pretty easy to define
import * as t from 'io-ts'
import { PathReporter } from 'io-ts/lib/PathReporter'
function assert<T>(value: any, type: t.Type<T>): T {
const validation = t.validate(value, type)
return validation.fold(() => {
throw new Error(PathReporter.report(validation).join('\n'))
}, x => x)
}
assert(1, t.number)
assert(1, t.string) // throws "Invalid value 1 supplied to : string"
Excuse me but I'm a little bit puzzled about the need of
tcomb-validation
, what's the gain of this:Versus this:
Wasn't the whole point of
tcomb
to have an identity function which did validation in the background?With
tcomb-validation
now I have to add boilerplate code to check every single thing.Please, don't misunderstand me, I'm not trolling, it's I'm not seeing the benefit, but since someone took the time to make
tcomb-validation
, then I guess I'm missing something.