Objection.js: Model validation doesn't report the model it fails for

Created on 22 Oct 2020  路  9Comments  路  Source: Vincit/objection.js

Version: 2.1.3

Given a model with required name and description attributes, I'm seeing the following validation errors:

     ValidationError: name: is a required property, description: is a required property
      at Function.createValidationError (node_modules/objection/lib/model/Model.js:357:12)
      at parseValidationError (node_modules/objection/lib/model/AjvValidator.js:189:21)
      at AjvValidator.validate (node_modules/objection/lib/model/AjvValidator.js:78:19)
      at validate (node_modules/objection/lib/model/modelValidate.js:35:20)
      at Object.$validate (node_modules/objection/lib/model/Model.js:93:12)
      at GraphInsertAction._insert (node_modules/objection/lib/queryBuilder/graph/insert/GraphInsertAction.js:177:16)
      at GraphInsertAction._insertBatch (node_modules/objection/lib/queryBuilder/graph/insert/GraphInsertAction.js:48:16)
      at processTicksAndRejections (internal/process/task_queues.js:97:5)

Shouldn't this error report which model it fails for? Seems like it'd be easier if multiple models are involved (test setup for example) to determine which one is the offending one, without requiring further debugging.

I'm imagining something like this for a model named Task:

     ValidationError (Task): name: is a required property, description: is a required property
      at Function.createValidationError (node_modules/objection/lib/model/Model.js:357:12)
      at parseValidationError (node_modules/objection/lib/model/AjvValidator.js:189:21)
      at AjvValidator.validate (node_modules/objection/lib/model/AjvValidator.js:78:19)
      at validate (node_modules/objection/lib/model/modelValidate.js:35:20)
      at Object.$validate (node_modules/objection/lib/model/Model.js:93:12)
      at GraphInsertAction._insert (node_modules/objection/lib/queryBuilder/graph/insert/GraphInsertAction.js:177:16)
      at GraphInsertAction._insertBatch (node_modules/objection/lib/queryBuilder/graph/insert/GraphInsertAction.js:48:16)
      at processTicksAndRejections (internal/process/task_queues.js:97:5)
enhancement

Most helpful comment

ValidationError and NotFoundError now have a modelClass property.

All 9 comments

Working on this now.

ValidationError and NotFoundError now have a modelClass property.

Thank you for this work! 馃

Indeed, thanks much! Apologies for the out-of-band communication, but I had one leftover question about this work which I left as a comment on the commit: https://github.com/Vincit/objection.js/commit/aa1e095093eba87ece5f74ab13a7542107d2a203#r46314524

Was it intentional to make the argument required to the NotFoundError constructor? If it was unintentional then I will be happy to contribute a patch.

@devinivy I fixed the issue and released a new version. Sorry about that.

No problem and always appreciated, @koskimas!

So my application just broke because of this change. All of the sudden, you can't do new ValidationError({...}) without a modelClass. Shouldn't code breaking changes not be assigned to minor releases?

@dryu I am sure that if this was considered a breaking change at the time of release then it would have gone into a major release. What I expect is more likely is that there is a bug or some kind of unintended use. Either way I think you'll be best off opening an issue with a more detailed account of the issue you're running into and some kind of reproduction so that we have some specifics to talk about. For example, I can call new ValidationError({}) without any issue, so it's hard to guess what you may have run into (however I would recommend using Model.createValidationError() if possible so that it takes care of this detail for you). If you understand the source of the problem then a pull request with a proposed fix may also help illustrate the issue you ran into, and could expedite a fix if one is necessary.

So, just to give you a bit of an update, I'm using a Typescript, and my code was like this :

new objection.ValidationError({
    message: "...",
    type: "...",
    data: {...}
});

This is how it is mentioned in the documentation for the custom validation by the way.

Before 2.2.10 everything was fine, but now ValidationError requires a modelClass argument and without it Typescript compilation fails with the error about a missing required param.

I had to add this param to make builds work.

The new code may work with a pure JS, but it is Typescript compilation that is a problem, because the arguments for ValidationError now includes & {modelClass: ModelClass<Model>} which wasn't there before.

Was this page helpful?
0 / 5 - 0 ratings