fromJSON should do runtime type checks
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?
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?
@stephenh have you had a chance to give this some thoughts?
@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!