Skip to content

[Feature Request] Is it possible to add validated input parameters and rule values in the error Violation? #17

@shuqingzai

Description

@shuqingzai

Feature description:

Example Proto

syntax = "proto3";

package project.api.v1;

import "buf/validate/validate.proto";

option go_package = "project/api/v1;v1";

// 分页参数
message PageParams {
  // 当前页, 从1开始(默认: 1, 最大: 1000)
  uint32 page = 1 [
    (buf.validate.field).uint32 = {gt: 0, lte: 1000}
  ];

  // 每页数量(默认: 20, 最大: 500)
  uint32 pageSize = 2 [
    (buf.validate.field).uint32 = {gt: 0, lte: 500}
  ];
}

go validate

func main() {
	msg := &pb.PageParams{
		Page:     0,
		PageSize: 10,
	}

	v, err := protovalidate.New()
	if err != nil {
		fmt.Println("failed to initialize validator:", err)
	}

	if err = v.Validate(msg); err != nil {
		var valErr *protovalidate.ValidationError
		if ok := errors.As(err, &valErr); ok {
			errPb := valErr.ToProto()
			fmt.Printf("\n%+v\n\n", errPb.GetViolations())
		}
	}
}

// ouput
//
// [field_path:"page"  constraint_id:"uint32.gt_lte"  message:"value must be greater than 0 and less than or equal to 1000"]
//

When I validate, the error returned by page is *validate.Violation, but I can’t get the request value 0 and the rule values: 0 and 1000, can the package add new fields (value and attributes ?? ) return?

Problem it solves or use case:

Users can get these values for more custom operations

I need to perform i18n now, because the semantics of each language are different when translating, and I need to customize the translation, but I can’t get these values, which makes the work very difficult

Proposed implementation or solution:

Contribution:

Examples or references:

Additional context:

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions