4 ms·
Validation is important, but the serialization layer is the wrong place to put validation logic. A protobuf is a low-level abstraction, like a struct or record
by haberman 4y ago
Validation is important, but the serialization layer is the wrong place to put validation logic.
A protobuf is a low-level abstraction, like a struct or record type in your favorite programming language. You want validation logic to be a separate layer on top. You don't want it so coupled to parsing/serialization that you literally cannot parse/serialize something that doesn't validate.
Protobuf has a rich facility for adding custom annotations to anything (messages, fields, etc). These annotations are the right place to put validation predicates. That will let anyone build a validation layer on top of protobuf, for example: https://scalapb.github.io/docs/validation/ https://scalapb.github.io/docs/validation/
- fuzzy2 4y agoIn general, I agree. The problem is that serialization (at least with proto3) is “lossy”. As I mentioned elsewhere in the discussion, proto3 messages discard certain information in the name of efficiency. The end result (message semantically invalid) does not change, of course, but the “why” could.
- haberman 4y agoYes, proto3 "singular" fields are a big problem. But now that proto3 supports "optional" fields (which remember the difference between unset and explicit 0) you can use true optional fields for all new messages going forward: https://developers.google.com/protocol-buffers/docs/proto3#specifying_field_rules https://developers.google.com/protocol-buffers/docs/proto3#s...