protoc-gen-validate icon indicating copy to clipboard operation
protoc-gen-validate copied to clipboard

Ignore empty with no validation causes invalid python to be generated via the jinja template

Open alexf-bond opened this issue 1 year ago • 0 comments

Version: protoc-gen-validate = "^0.10.1"

message RequestMessage {
  string some_string_field = 1 [(validate.rules).string = {uuid: true, ignore_empty: true}];
  string other_string_field_no_validation = 2 [(validate.rules).string = {ignore_empty: true}];
}
from app.request_pb2 import RequestMessage
from protoc_gen_validate.validator import ValidationFailed, validate, print_validate
try:
     validate(RequestMessage())
except:
     print_validate()
# Validates RequestMessage
def generate_validate(p):
    if p.some_string_field:
        try:
            uuid.UUID(p.some_string_field)
        except ValueError:
            raise ValidationFailed("p.some_string_field is not a valid UUID")
    if p.other_string_field_no_validation:
    return None

File "/app/app/service.py", line 724, in FindCard
    validate(proto_message=request)
  
File "/usr/local/lib/python3.9/site-packages/protoc_gen_validate/validator.py", line 57, in validate
    return _validate_inner(ValidatingMessage(proto_message))(proto_message)
  
File "/usr/local/lib/python3.9/site-packages/protoc_gen_validate/validator.py", line 66, in _validate_inner
    exec(func)
  
File "<string>", line 18
    
return None
    
^
IndentationError: expected an indented block

I didn't see any docs indicating the validation would raise an exception if ignore_empty wasn't placed alongside an actual validation rule. Not sure on the lift here to simply ignore generating the python code template for fields that express ignore empty without validation rules.

Could we just make these indented pass for instead of returning None?

alexf-bond avatar Apr 17 '23 16:04 alexf-bond