1

Using C# native package of 3.0.0-beta3 version, as per official documentation defaults are assigned during parsing stage and there is no way to distinguish whether value was not passed at all, for my purposes this is vital point since I wanted to do some kind of validation for decoded proto entities so wondering if anyone has experience with custom validation of data or overriding/intercepting parsing stage in any way?

Some basic examples:

  1. For int32 field in case it was not passed at all so instead of default 0 I want to handle this case and raise custom exception
  2. For int32 field I wanted to enforce allowed value range like 100-1000 otherwise throw parsing exception
  3. For string field I wanted to enforce minimum length on parsing/decoding stage

How would you suggest do this except writing custom validation layer on top of protobuf package/API? I still hope there is an extensibility points but not found yet

sll
  • 61,540
  • 22
  • 104
  • 156

1 Answers1

1

Validation needs to be done in the application code post-parsing. There are no hooks to do this in the parser itself.

Proto3 does not distinguish between default-valued fields and unsent fields. In fact, on the sending end, if the field has been explicitly set to its default value, it won't be encoded. So, there is no way to implement your first rule in proto3.

Kenton Varda
  • 41,353
  • 8
  • 121
  • 105
  • This is the same I found in docs, unfortunatly. Have no idea how systems which uses protobuf addresses such issues, seems by defining some custom defaults to distinguish from proto3 defaults, thanks – sll May 25 '16 at 16:53
  • @sll Proto3 also doesn't support custom defaults; only zero/empty. But really, the answer is that you don't need to do this validation. If the default value is an acceptable input, then carry on with it. It's the sender's responsibility to make sure that they are sending the values they want; the only thing you need to validate on the server is that the values they sent won't damage the server. – Kenton Varda May 25 '16 at 17:39