protobuf.js icon indicating copy to clipboard operation
protobuf.js copied to clipboard

Throw error when validating fields that do not exists

Open araujo-luis opened this issue 3 years ago • 0 comments

protobuf.js version: 7.1.0

This is my proto definition:

package test;
syntax = "proto3";

message Data {
   string dataVal = 1;
   string moreData = 2;
}

message Error {
   string error = 1;
   string stackTrace = 2;
}

message TestResponse {
  string id = 1;
  Data data = 2;
   Error error = 3;
}

then I do:

  const root = await protobuf.load('test.proto');

  const Response = root.lookupType('test.TestResponse');

  console.log(Response.verify({ propertyDoesntExist: 'test' })); // null, I would expect an error as the property doesn't exist in its Proto definition

The generated verify function looks a bit strange, with additional curly braces{} and stuff. The propertyDoesntExist is considered false in all conditions so return null

TestResponse$verify(m){
    if (typeof m !== "object" || m === null)
        return "object expected"
    if (!util.isString(m.guid))
        return "guid: string expected"
    {
        var e = types[1].verify(m.data);
        if (e)
            return "data." + e
    }
    if (m.error != null && m.hasOwnProperty("error")) {
        {
            var e = types[2].verify(m.error);
            if (e)
                return "error." + e
        }
    }
    return null
}

is there a way to validate this? a property or so?

araujo-luis avatar Aug 31 '22 18:08 araujo-luis