Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
alexf-bond opened this issue Apr 17, 2023 · 0 comments
Labels
Bug Reports and/or fixes a bug Good First Issue Newcomer-friendly issue Python Python language support

Comments

@alexf-bond
Copy link

alexf-bond commented Apr 17, 2023

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?

@rodaine rodaine added Bug Reports and/or fixes a bug Python Python language support triaged Good First Issue Newcomer-friendly issue labels Apr 20, 2023
@rodaine rodaine removed the triaged label May 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Reports and/or fixes a bug Good First Issue Newcomer-friendly issue Python Python language support
Projects
None yet
Development

No branches or pull requests

2 participants