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

[Proposal] Add context code for validation errors. #1322

Closed
brycereitano opened this issue Jul 28, 2017 · 3 comments
Closed

[Proposal] Add context code for validation errors. #1322

brycereitano opened this issue Jul 28, 2017 · 3 comments

Comments

@brycereitano
Copy link
Contributor

I propose that we add some extra metadata to the various validation helpers. With the addition to customizing the error responses. (#1321) This would aide in further customizing the error response.

It would include adding an extra key to the various validation helpers. For example,

// InvalidParamTypeError is the error produced when the type of a parameter does not match the type
// defined in the design.
func InvalidParamTypeError(name string, val interface{}, expected string) error {
	msg := fmt.Sprintf("invalid value %#v for parameter %#v, must be a %s", val, name, expected)
	return ErrInvalidRequest(msg, "validation_code", "invalid_param_type, "param", name, "value", val, "expected", expected)
}

It would be preferable to include variables or constants (whether editing these should be allowed) for easier consumption of this change.

const (
	InvalidParamTypeErrorCode     = "invalid_param_type"
	MissingParamErrorCode         = "missing_param"
	InvalidAttributeTypeErrorCode = "invalid_attribute_type"
	MissingAttributeErrorCode     = "missing_attribute"
	MissingHeaderErrorCode        = "missing_header"
	InvalidEnumValueErrorCode     = "invalid_enum_value"
	InvalidFormatErrorCode        = "invalid_format"
	InvalidPatternErrorCode       = "invalid_pattern"
	InvalidRangeErrorCode         = "invalid_range"
	InvalidLengthErrorCode        = "invalid_length"
)
@raphael
Copy link
Member

raphael commented Jul 29, 2017

Not sure I follow, the responses already have a code - the error class code. Can you expand a little bit on what you are trying to achieve?

@brycereitano
Copy link
Contributor Author

This isn't mission critical, but the idea is to provide codes for every error in the response.

{
    "code": "unprocessable_entity",
    "details": {
         "username": [
            {
                "Code": "invalid_format",
                "Message": "username must match the regexp \"^[A-Za-z0-9'_-]+(?:[.][A-Za-z0-9]+)*$\" but got value \"!aaaa\""
            },
            {
                "Code": "invalid_length",
                "Message": "length of username must be greater than or equal to 6 but got value \"!aaaa\" (len=5)"
            }
        ]
    },
    "id": "Fa8iKyff",
    "message": "Invalid user input.",
    "status": 422
}

@stale
Copy link

stale bot commented Mar 15, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Mar 15, 2018
@stale stale bot closed this as completed Mar 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants