Skip to content

Check if property is part of proto schema #526

Closed

Description

@subk

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 });

Activity

  1. dcodeIO commented on Dec 7, 2016

    @dcodeIO
    Member

    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.

  2. subk commented on Dec 7, 2016

    @subk
    ContributorAuthor

    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
  3. dcodeIO commented on Dec 7, 2016

    @dcodeIO
    Member

    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.

  4. subk commented on Dec 7, 2016

    @subk
    ContributorAuthor

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

  5. dcodeIO commented on Dec 7, 2016

    @dcodeIO
    Member

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

  6. added a commit that references this issue on Dec 9, 2016
  7. dcodeIO commented on Dec 9, 2016

    @dcodeIO
    Member

    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.

  8. added a commit that references this issue on Dec 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions