Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In/NotIn and const rules show enum values for enum type (#699)
## Problem The error message for enum `in/not_in` rules show numbers instead of enum values Sample case **Input:** ```protobuf ... enum Enum { UNKNOWN_ENUM = 0; OPTION_1 = 1; OPTION_2 = 2; } message Book { Enum enum1 = 1 [(validate.rules).enum = {in: [1, 2]}]; Enum enum2 = 2 [(validate.rules).enum = {not_in: [1]}]; Enum enum3 = 3 [(validate.rules).enum = {const: 1}]; } ``` **Current output:** ```go func (m *Book) validate(all bool) error { ... if _, ok := _Book_Enum1_InLookup[m.GetEnum1()]; !ok { err := BookValidationError{ field: "Enum1", reason: "value must be in list [1 2]", } ... } if _, ok := _Book_Enum2_NotInLookup[m.GetEnum2()]; ok { err := BookValidationError{ field: "Enum2", reason: "value must not be in list [1]", } ... } if m.GetEnum3() != 1 { err := BookValidationError{ field: "Enum3", reason: "value must equal 1", } ... } } ``` **Desired output:** ```go func (m *Book) validate(all bool) error { ... if _, ok := _Book_Enum1_InLookup[m.GetEnum1()]; !ok { err := BookValidationError{ field: "Enum1", reason: "value must be in list [OPTION_1 OPTION_2]", } ... } if _, ok := _Book_Enum2_NotInLookup[m.GetEnum2()]; ok { err := BookValidationError{ field: "Enum2", reason: "value must not be in list [OPTION_1]", } ... } if m.GetEnum3() != 1 { err := BookValidationError{ field: "Enum3", reason: "value must equal OPTION_1", } .... } } ``` ## Changes - Added `isEnum` function to check if the field type is enum - Added `enumList` function to return options string (e.g. `[OPTION_1 OPTION_2]` ) for given field and `in/notIn` list - Added `enumVal` function to return option (for const) - Change `cpp`, `goshared` templates ### TODO - [x] Add test cases - [x] Update [compatibility set](https://github.com/bufbuild/protoc-gen-validate/blob/main/rule_comparison.md) Co-authored-by: Elliot Jackson <[email protected]>
- Loading branch information