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

fix(schema): fix custom error message set through .Error() nested decorator #2699

Open
wants to merge 3 commits into
base: production
Choose a base branch
from

Conversation

Romakita
Copy link
Collaborator

@Romakita Romakita commented May 7, 2024

Closes: #2696

Information

Type Breaking change
fix No

Todos

  • Tests
  • Coverage
  • Example
  • Documentation

@Romakita Romakita force-pushed the fix-2696-ajv-errors-message branch 2 times, most recently from 0eb4730 to 9384b6a Compare May 8, 2024 08:22
@Romakita Romakita force-pushed the fix-2696-ajv-errors-message branch 3 times, most recently from 0f9654d to 4fef967 Compare May 8, 2024 18:22
Copy link

github-actions bot commented May 8, 2024

Benchmarks

  • Machine: linux x64 | 4 vCPUs | 15.6GB Mem
  • Node: v18.20.2
  • Run: Wed May 08 2024 18:31:38 GMT+0000 (Coordinated Universal Time)
  • Method: autocannon -c 100 -d 10 -p 10 localhost:3000 (two rounds; one to warm-up, one to measure)
Version Router Requests/s Latency Throughput/Mb
koa 2.13.4 33603.6 29.23 5.99
tsed-koa 7.69.0 11803.3 83.93 9.99
express 4.19.2 10329.8 95.97 1.84
nest 8.4.3 9882.6 100.26 2.38
express-injector 4.19.2 9361.6 105.75 2.13
tsed-express 7.69.0 8489.8 116.80 1.55
express-morgan 4.19.2 5571.5 177.53 0.99
fastify-big-json 3.29.4 N/A N/A N/A
fastify-injector 3.29.4 N/A N/A N/A
fastify 3.29.4 N/A N/A N/A
nest-fastify 8.4.3 N/A N/A N/A

Explanation

The benchmark shows a performance difference between the frameworks. We note that Ts.ED is often last. In fact, Ts.ED uses features useful to a production application which reduce its performance.

For example, Ts.ED initializes a sandbox (async_hook) for each request in order to work in an isolated context if necessary.
It also initializes the elements necessary for monitoring requests in a log manager.

All this at a necessary cost that reflects the reality of a production application ;)

Copy link

github-actions bot commented May 8, 2024

Benchmarks

  • Machine: linux x64 | 4 vCPUs | 15.6GB Mem
  • Node: v18.20.2
  • Run: Wed May 08 2024 18:33:49 GMT+0000 (Coordinated Universal Time)
  • Method: autocannon -c 100 -d 10 -p 10 localhost:3000 (two rounds; one to warm-up, one to measure)
Version Router Requests/s Latency Throughput/Mb
koa 2.13.4 33672.8 29.14 6.01
tsed-koa 7.69.0 11840.0 83.67 10.03
express 4.19.2 10454.7 94.86 1.86
nest 8.4.3 9751.6 101.68 2.34
express-injector 4.19.2 9515.5 104.22 2.17
tsed-express 7.69.0 8574.4 115.56 1.57
express-morgan 4.19.2 5530.7 179.00 0.99
fastify-big-json 3.29.4 N/A N/A N/A
fastify-injector 3.29.4 N/A N/A N/A
fastify 3.29.4 N/A N/A N/A
nest-fastify 8.4.3 N/A N/A N/A

Explanation

The benchmark shows a performance difference between the frameworks. We note that Ts.ED is often last. In fact, Ts.ED uses features useful to a production application which reduce its performance.

For example, Ts.ED initializes a sandbox (async_hook) for each request in order to work in an isolated context if necessary.
It also initializes the elements necessary for monitoring requests in a log manager.

All this at a necessary cost that reflects the reality of a production application ;)

@Romakita
Copy link
Collaborator Author

Romakita commented May 8, 2024

@ktalebian Can review this PR. I need your review over the AJV response error ;)
Thanks a lot!

@Romakita Romakita force-pushed the fix-2696-ajv-errors-message branch from 4fef967 to b9e1b87 Compare May 8, 2024 18:39
Copy link

github-actions bot commented May 8, 2024

Benchmarks

  • Machine: linux x64 | 4 vCPUs | 15.6GB Mem
  • Node: v18.20.2
  • Run: Wed May 08 2024 18:51:10 GMT+0000 (Coordinated Universal Time)
  • Method: autocannon -c 100 -d 10 -p 10 localhost:3000 (two rounds; one to warm-up, one to measure)
Version Router Requests/s Latency Throughput/Mb
koa 2.13.4 33112.7 29.66 5.91
tsed-koa 7.69.0 12027.2 82.40 10.18
express 4.19.2 10002.5 99.02 1.78
nest 8.4.3 9762.4 101.45 2.35
express-injector 4.19.2 9314.0 106.51 2.12
tsed-express 7.69.0 8397.1 118.11 1.54
express-morgan 4.19.2 5589.6 177.03 1.00
fastify-big-json 3.29.4 N/A N/A N/A
fastify-injector 3.29.4 N/A N/A N/A
fastify 3.29.4 N/A N/A N/A
nest-fastify 8.4.3 N/A N/A N/A

Explanation

The benchmark shows a performance difference between the frameworks. We note that Ts.ED is often last. In fact, Ts.ED uses features useful to a production application which reduce its performance.

For example, Ts.ED initializes a sandbox (async_hook) for each request in order to work in an isolated context if necessary.
It also initializes the elements necessary for monitoring requests in a log manager.

All this at a necessary cost that reflects the reality of a production application ;)

emUsed: true,
instancePath: "/word",
keyword: "minLength",
message: "must NOT have fewer than 10 characters",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this always remain the default error message? Shouldn't this also say Title must be at least 10 characters long?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would happen if you have

class Model {
      @MinLength(10).Error("Title must be at least 10 characters long")
      word: string;

      @MinLength(100).Error("Setence must be at least 100 characters long")
      sentence: string;
    }

The response would have a single message but the params.errors would then have two entries.

I feel the result.message should actually be the @DefaultMessage decorator that sits on Model class, not the individual schemaPath error.

Copy link
Collaborator Author

@Romakita Romakita May 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ajv-errors doesn't override the default message. it wrap the original error and add his message.
Look this page, there is all explanation:

https://ajv.js.org/packages/ajv-errors.html

I feel the result.message should actually be the @DefaultMessage decorator that sits on Model class, not the individual schemaPath error.

This is not the current test @ktalebian ^^. DefaultMsg decorator should works, if there is no other message that match the properties + rules.

Furthermore, I don't really have control over how the error message came out. Ajv-errors manages everything and I just follow what is indicated in their docs in order to have the expected behavior.

If we really want to have better message management, we would have to write the plugin ourselves to overload the error messages. I clearly don't have the time to do this nor the desire. This module won't be useful to me in a project, because I don't use these error messages in a front-end project.

But if you have time, you can do develop the module. Instead of adding custom keyword (like is do by ajv-errors), the solution could be to override the errors after the validation is done (by matching error.instancePath + errro.keyword).

See you

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ktalebian is it ok for you ? (but we haven't a lot of choices XD)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Custom .Error does not work on schemas other than @Required
2 participants