PayU / openapi-validator-middleware

Input validation using Swagger (Open API) and ajv
Apache License 2.0
144 stars 50 forks source link

Internationalization #147

Closed nioc closed 3 years ago

nioc commented 3 years ago

Hello, it is possible to set internationalized error messages like ajv-i18n combined with beautfy set to true?

I've tried to use it, but I have to choose between localized or beautified message:

const swaggerValidation = require('openapi-validator-middleware')
const localize = require('ajv-i18n')
(...)
swaggerValidation.init('swagger.yml', { beautifyErrors: true })
(...)
api.use((err, req, res, next) => {
  localize.fr(err.errors) 
  console.log(err.errors) //-> in english 
})
nioc commented 3 years ago

I found a workaround but it is ugly... do not set beautifyErrors option on init and create a new InputValidationError with original errors and beautifyErrors option set to true.

const swaggerValidation = require('openapi-validator-middleware')
const localize = require('ajv-i18n')
(...)
// set beautifyErrors to false
swaggerValidation.init('swagger.yml', { beautifyErrors: false })
(...)
api.use((err, req, res, next) => {
  localize.fr(err.errors)
  // create a new InputValidationError with beautifyErrors set to true
  const localizedErr = new swaggerValidation.InputValidationError(err.errors, { beautifyErrors: true })
  console.log(localizedErr.errors) //-> in french
})

If there is a less ugly way to achieve it, let me know otherwise feel free to close the issue.

kobik commented 3 years ago

Thanks @nioc , that would be a nice feature to add.

feel free to submit a PR.

nioc commented 3 years ago

I close this issue since workaround is enough for me.