ts-proto icon indicating copy to clipboard operation
ts-proto copied to clipboard

fromJSON should do runtime type checks

Open fenos opened this issue 3 years ago • 3 comments

Hi! me again! I can't thank you enough for this piece of art!

I came across an inconsistent behaviour while implementing the Twirp specification.

When using Message.decode(), it throws an error if the data-type is not respected:

TypeError [ERR_INVALID_ARG_TYPE]: The "string" argument must be of type string or an instance of Buffer or ArrayBuffer. Received an instance of Object

Which I believe is the correct behaviour.

However when using the method Method.fromJSON() it tries to cast the parameter instead of checking for the data-type validity as you can see HERE

It could have been great if we could error if the data-type is not respected to mimic the same behaviour and return the exact error in both protocols

What are your suggestions over this?

fenos avatar Jun 15 '21 11:06 fenos

Right! What I'm after is to have the verify() method that protobuf.js has. Can you guide me on what should be needed to extend the generator to have that embedded?

fenos avatar Jun 17 '21 20:06 fenos

@stephenh have you had a chance to give this some thoughts?

fenos avatar Jun 19 '21 22:06 fenos

@fenos hey, sorry for the delay here; yeah I agree fromJSON should be able to do runtime type-checking (probably all the time, right? I.e. it could be a configuration flag but it seems like a good idea to just always do).

I think my initial implementation was used primarily for internal-to-internal services, so we were pretty trusting of the incoming input, at least in terms of basic things like types/shape.

Are you interested in adding this to fromJSON? Would be great to have a PR.

Thanks!

stephenh avatar Jul 11 '21 22:07 stephenh