You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there a way to check if a plain object is valid regarding to its protobuf message type ?
If I remember correctly, protobufjs v5 was doing so before encoding a message.
constfoo=root.lookup('test.Foo');// should not throwconstvalidMessage=foo.create({bar: 'foobar'});// should throw an Error as 'xyz' is not a valid fieldconstinvalidMessage=foo.create({bar: 'foobar',xyz: 123});
There is verify which checks for missing required fields and invalid enum values. In your example:
foo.verify(validMessage);// returns null if the message is considered valid, otherwise a string why it is not
verify does not type-check (yet), so encoding may still throw if present field values are entirely bogus.
The reason why this has its own method is performance. Let's say you already know for sure that the message is valid, because you double checked it, which is what I for example usually do, then calling verify implicitly would be unnecessary.
Ok, it makes perfect sense from a performance point of view.
However I found protobufjs v5 behavior was quite useful and very explicit on errors (eg. when trying to call a service method with wrong arguments for example)
Not sure to understand how verifier.generate works, but would you be interested in a PR on verifier.fallback which will :
The resulting implementation would then not just have code generation for encode, decode and verify but also for initialize and convert (to JSON). I'd then also update the verifier to perform type-checking as this is in fact quite useful and shouldn't be much of an issue as verify is optional anyway.
Activity
dcodeIO commentedon Dec 7, 2016
There is
verifywhich checks for missing required fields and invalid enum values. In your example:verifydoes not type-check (yet), so encoding may still throw if present field values are entirely bogus.The reason why this has its own method is performance. Let's say you already know for sure that the message is valid, because you double checked it, which is what I for example usually do, then calling verify implicitly would be unnecessary.
Nonetheless, like encode/decode also verify is generated code and thus about as fast as it probably can be.
subk commentedon Dec 7, 2016
Ok, it makes perfect sense from a performance point of view.
However I found protobufjs v5 behavior was quite useful and very explicit on errors (eg. when trying to call a service method with wrong arguments for example)
Not sure to understand how
verifier.generateworks, but would you be interested in a PR onverifier.fallbackwhich will :messagefor valid propertiesmessagevalid propertiesdcodeIO commentedon Dec 7, 2016
I will probably go ahead and refactor the code generator anyway.
Somewhat related: #521
The resulting implementation would then not just have code generation for encode, decode and verify but also for initialize and convert (to JSON). I'd then also update the verifier to perform type-checking as this is in fact quite useful and shouldn't be much of an issue as verify is optional anyway.
subk commentedon Dec 7, 2016
Ok 👍
I will keep an eye on #521 too
Thanks
Refactored codegen, put frozen objects on the prototype, see #526; Tr…
dcodeIO commentedon Dec 7, 2016
Actually, wrong thread, moved the post to the other one.
Initial type-checking verifier, see #526, added to bench out of compe…
dcodeIO commentedon Dec 9, 2016
This commit adds type checking to the verifier. It's quite strict. Let me know if you experience any issues.
Note: This doesn't work with map fields, yet.
Also validate map fields, see #526 (this really needs some tests)