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

Bug: Enum "in / not_in" in the Map

Open yrcs opened this issue 2 years ago • 7 comments

The closed #619 issue is still unresolved.

enum Order {
  ASC = 0;
  DESC = 1;
}

message PagingRequest {
  map<string, Order> orderBy = 1 [(validate.rules).map = {ignore_empty: true, keys: {string: {min_len: 1}}, values: {enum: {defined_only: true, in:[0, 1]}}}];
}

yrcs avatar Oct 08 '22 09:10 yrcs

Hi @yrcs, looking at the rule comparison chart in / not_in is not supported in maps at this stage. If you require them, id be happy to review a PR but at this stage we are focused on bugs and language support parity.

elliotmjackson avatar Oct 14 '22 17:10 elliotmjackson

Hi @yrcs, looking at the rule comparison chart in / not_in is not supported in maps at this stage. If you require them, id be happy to review a PR but at this stage we are focused on bugs and language support parity.

OK! Just keep this issue open...

yrcs avatar Oct 17 '22 06:10 yrcs

looking at the rule comparison chart in / not_in is not supported in maps at this stage

I don't think it's specified in this comparison chart. The chart shows keys and values are supported; it doesn't list all the possible sub-rules that are supported within the keys and values.

I'd argue that this is a bug, not a lack of a feature, because pgv doesn't ignore in/not_in, or throw an error indicating that they are not supported - it exits successfully having generated code that then fails to compile.

haines avatar Nov 29 '22 15:11 haines

@haines You're right, it's a bug. And I solved it temporarily by adding the following variables to the generated *.pb.validate.go file.

var _PagingRequest_OrderBy_InLookup = map[Order]struct{}{
    0: {},
    1: {},
}

yrcs avatar Nov 30 '22 01:11 yrcs

@elliotmjackson I have encountered the same problem with IDLs like this one.

message CreateTextRequest {
  map<string, string> texts = 1 [(validate.rules).map.keys.string = { in: [ "en", "zh" ] }];  
};

The definition of _CreateTextRequest_Texts_InLookup is missing from the generated go code.

	if _, ok := _CreateTextRequest_Texts_InLookup[key]; !ok {
		err := CreateTextRequestValidationError{
			field:  fmt.Sprintf("Texts[%v]", key),
			reason: "value must be in list [en zh]",
		}
		if !all {
			return err
		}
		errors = append(errors, err)
	}

Can this issue be fixed?

rolandcxc avatar Dec 13 '22 09:12 rolandcxc

Same problem +1

m430 avatar Mar 10 '23 10:03 m430

Good news! this has since been resolved in our protovalidate project. I'm pleased to let you know that protovalidate is where we're focusing our efforts for the future.

Given the improvements and advancements in protovalidate, I recommend considering migration to this newer version. It offers a more comprehensive and user-friendly experience within the Protobuf ecosystem.

elliotmjackson avatar Aug 10 '23 20:08 elliotmjackson