express-openapi-validator icon indicating copy to clipboard operation
express-openapi-validator copied to clipboard

Getting type error when accessing errorCode property of a ValidationErrorItem

Open shashanksapre opened this issue 3 years ago • 1 comments

Describe the bug Getting a type error when I try to access properties of errors array within an error object of type ValidationError

To Reproduce Access the property errorCode inside an ValidationErrorItem

Actual behavior Type error: Property 'errorCode' does not exist on type 'ValidationErrorItem'. Did you mean 'error_code'. If I try to access error_code, it's undefined.

Expected behavior Should be able to access errorCode as that's what is returned by express-openapi-validator image

Examples and context I'm trying to transform errors returned by validator into another standard error body response.

image

shashanksapre avatar Dec 02 '21 11:12 shashanksapre

I ran into the same issue, looks like the codebase uses a mix of both errorCode and error_code. As a workaround I declared

interface OpenAPIValidationError extends ValidationErrorItem {
    errorCode?: string
}

Using an express middleware I transform them like

const errorCode = err.error_code || err.errorCode || "unknown"

konstantinblaesi avatar Jul 07 '22 10:07 konstantinblaesi