adonisjs / core

AdonisJS is a TypeScript-first web framework for building web apps and API servers. It comes with support for testing, modern tooling, an ecosystem of official packages, and more.
https://adonisjs.com
MIT License
16.33k stars 624 forks source link

ValidationException in @adonisjs/validator not being caught correctly #4271

Closed dranduil closed 6 months ago

dranduil commented 9 months ago

Prerequisites

Package version

Node.js and npm version

Sample Code (to reproduce the issue)

const validationSchema = schema.create({
  supplier_type: schema.enum(Object.values(SupplierType)),
    business_name: schema.string(),
    legal_name: schema.string.optional(),
    legal_business_address: schema.string(),
    business_address: schema.string.optional(),
    vat_number: schema.string(),
    fiscal_code: schema.string(),
    birth_date: schema.date.optional(),
    birth_place: schema.string.optional(),
    number_phone: schema.number.optional(),
    email: schema.string.optional([
      rules.email(),
      rules.normalizeEmail({
        allLowercase: true,
      })
    ]),
    icao_code: schema.string.optional(),
    company_code: schema.string.optional(),
    country_code: schema.string(),
    payment_mode: schema.string(),
    iban: schema.string(),
    currency_code: schema.string([
      rules.maxLength(3)
    ]),
    self_employed_invoice: schema.boolean.optional(),
    unique_code: schema.string.optional(), // codice univoco
    pec: schema.string.optional([
      rules.email(),
      rules.normalizeEmail({
        allLowercase: true,
      })
    ])
});

  const payload = await request.validate({
    schema: validationSchema
  });

// When calling `this.toJSON()` within the error handling logic, the following validation errors are returned:
// {
//   "errors": [
//     {
//       "rule": "required",
//       "field": "supplier_type",
//       "message": "required validation failed"
//     },
//     // ... (other similar error objects)
//   ]
// }

// The error seems to originate from `ApiErrorReporter.toError` within the `@adonisjs/validator` package:
// projecet\node_modules\@adonisjs\validator\build\src\ErrorReporter\Api.js:50
// return new ValidationException_1.ValidationException(false, this.toJSON());
               ^
//ValidationException: E_VALIDATION_FAILURE: Validation Exception
//    at ApiErrorReporter.toError //(C:\Apache24\htdocs\gattinoni_api\node_modules\@adonisjs\validator\build\src\ErrorReporter\Api.js:50:16)
//    at eval (eval at compile //(C:\Apache24\htdocs\gattinoni_api\node_modules\@adonisjs\validator\build\src\Compiler\index.js:218:16), <anonymous>:421:31)
//    at validate (C:\Apache24\htdocs\gattinoni_api\node_modules\@adonisjs\validator\build\src\Validator\index.js:114:72)
 //   at Request.validateRequest [as validate] //(C:\Apache24\htdocs\gattinoni_api\node_modules\@adonisjs\validator\build\src\Bindings\Request.js:48:37)
//    at FormController.handleForm (C:\Apache24\htdocs\gattinoni_api\app\Controllers\Http\FormController.ts:8:33)
//    at Injector.callAsync (C:\Apache24\htdocs\gattinoni_api\node_modules\@adonisjs\fold\build\src\Ioc\Injector.js:124:30)
//    at processTicksAndRejections (node:internal/process/task_queues:96:5)

//package.json (part)
"devDependencies": {
    "@adonisjs/assembler": "^5.9.6",
    "@japa/preset-adonis": "^1.2.0",
    "@japa/runner": "^2.5.1",
    "@types/proxy-addr": "^2.0.1",
    "@types/source-map-support": "^0.5.8",
    "adonis-preset-ts": "^2.1.0",
    "eslint": "^8.51.0",
    "eslint-config-prettier": "^9.0.0",
    "eslint-plugin-adonis": "^2.1.1",
    "eslint-plugin-prettier": "^5.0.1",
    "pino-pretty": "^10.2.3",
    "prettier": "^3.0.3",
    "typescript": "~4.6",
    "youch": "^3.3.2",
    "youch-terminal": "^2.2.3"
  },
  "dependencies": {
    "@adonisjs/core": "^5.9.0",
    "@adonisjs/repl": "^3.1.11",
    "@adonisjs/validator": "^12.5.0",
    "exceljs": "^4.3.0",
    "proxy-addr": "^2.0.7",
    "reflect-metadata": "^0.1.13",
    "source-map-support": "^0.5.21"
  }
RomainLanz commented 9 months ago

Hey @dranduil! 👋🏻

Could you please create a repository with the minimum amount of code to reproduce your issue?

dranduil commented 8 months ago

yes of course

thetutlage commented 6 months ago

Closing since no response from the issue reporter and not actionable as well

thetutlage commented 6 months ago

Feel free to re-open the issue once you submit a reproduction.