New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if property is part of proto schema #526

Closed
subk opened this Issue Dec 7, 2016 · 6 comments

Comments

Projects
None yet
2 participants
@subk
Contributor

subk commented Dec 7, 2016

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.

I didnt find a way to do it in protobufjs v6

Eg, given this proto :

syntax = "proto3";
package test;
message Foo {
  string bar = 1;
}
const foo = root.lookup('test.Foo');

// should not throw
const validMessage = foo.create({ bar: 'foobar' });

// should throw an Error as 'xyz' is not a valid field
const invalidMessage = foo.create({ bar: 'foobar', xyz: 123 });
@dcodeIO

This comment has been minimized.

Owner

dcodeIO commented Dec 7, 2016

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.

Nonetheless, like encode/decode also verify is generated code and thus about as fast as it probably can be.

@dcodeIO dcodeIO added the question label Dec 7, 2016

@subk

This comment has been minimized.

Contributor

subk commented 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.generate works, but would you be interested in a PR on verifier.fallback which will :

  • check message for valid properties
  • type-check message valid properties
@dcodeIO

This comment has been minimized.

Owner

dcodeIO commented 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

This comment has been minimized.

Contributor

subk commented Dec 7, 2016

Ok 👍
I will keep an eye on #521 too
Thanks

dcodeIO added a commit that referenced this issue Dec 7, 2016

@dcodeIO

This comment has been minimized.

Owner

dcodeIO commented Dec 7, 2016

Actually, wrong thread, moved the post to the other one.

dcodeIO added a commit that referenced this issue Dec 9, 2016

@dcodeIO

This comment has been minimized.

Owner

dcodeIO commented 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.

dcodeIO added a commit that referenced this issue Dec 9, 2016

@dcodeIO dcodeIO closed this Dec 13, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment