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

(#1718) Add support for verbose ajv options #2614

Merged
merged 9 commits into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions doc/release-notes/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## 3.1.4

### What's new

- Fix `upsertGraph()` `$beforeUpdate()` calls on empty relates [#2605](https://github.com/Vincit/objection.js/issues/2605)
- Don't call `onError()` with internal exceptions [#2603](https://github.com/Vincit/objection.js/issues/2603)
- Remove docs and typings for nonexistent `$pick()`
- Make `$omitFromJson()` + `$omitFromDatabaseJson()` compatible with old `$omit()` syntax

## 3.1.3

### What's new
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ version: '3'
services:
postgres:
image: 'postgres'
platform: linux/amd64
cesumilo marked this conversation as resolved.
Show resolved Hide resolved
container_name: 'objection_postgres'
command: postgres -c fsync=off -c synchronous_commit=off -c full_page_writes=off -c random_page_cost=1.0
ports:
Expand All @@ -10,6 +11,7 @@ services:
- POSTGRES_HOST_AUTH_METHOD=trust
mysql:
image: 'mysql:5'
platform: linux/amd64
container_name: 'objection_mysql'
ports:
- '3306:3306'
Expand Down
20 changes: 14 additions & 6 deletions lib/model/AjvValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class AjvValidator extends Validator {
}

validator.call(model, json);
const error = parseValidationError(validator.errors, modelClass, options);
const error = parseValidationError(validator.errors, modelClass, options, this.ajvOptions);

if (error) {
throw error;
Expand Down Expand Up @@ -143,7 +143,7 @@ class AjvValidator extends Validator {
}
}

function parseValidationError(errors, modelClass, options) {
function parseValidationError(errors, modelClass, options, ajvOptions) {
if (!errors) {
return null;
}
Expand Down Expand Up @@ -178,13 +178,21 @@ function parseValidationError(errors, modelClass, options) {
// More than one error can occur for the same key in Ajv, merge them in the array:
const array = errorHash[key] || (errorHash[key] = []);

// Use unshift instead of push so that the last error ends up at [0],
// preserving previous behavior where only the last error was stored.
array.unshift({
// Prepare error object
const errorObj = {
message: error.message,
keyword: error.keyword,
params: error.params,
});
};

// Add data if verbose enabled
if (ajvOptions.verbose) {
errorObj.data = error.data;
}

// Use unshift instead of push so that the last error ends up at [0],
// preserving previous behavior where only the last error was stored.
array.unshift(errorObj);

++numErrors;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "objection",
"version": "3.1.3",
"version": "3.1.4",
"description": "An SQL-friendly ORM for Node.js",
"main": "lib/objection.js",
"license": "MIT",
Expand Down
75 changes: 75 additions & 0 deletions tests/integration/misc/#1718.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const _ = require('lodash');
const expect = require('expect.js');
const { Model } = require('../../../');
const { AjvValidator } = require('../../../lib/model/AjvValidator');
const { ValidationError } = require('../../../lib/model/ValidationError');

module.exports = (session) => {
describe('When Ajv verbose is enabled, pass through the data field in exception #1718', () => {
class A extends Model {
static get tableName() {
return 'a';
}

static createValidator() {
return new AjvValidator({
onCreateAjv: (ajv) => {
// Here you can modify the `Ajv` instance.
},
options: {
allErrors: true,
validateSchema: false,
ownProperties: true,
v5: true,
verbose: true,
},
});
}

static get jsonSchema() {
return {
type: 'object',
required: ['id'],
properties: {
id: { type: 'integer' },
},
};
}
}

beforeEach(() => {
return session.knex.schema
.dropTableIfExists('a')
.createTable('a', (table) => {
table.integer('id').primary();
})
.then(() => {
return Promise.all([session.knex('a').insert({ id: 1 })]);
});
});

afterEach(() => {
return session.knex.schema.dropTableIfExists('a');
});

it('the test', () => {
return A.query(session.knex)
.insert({ id: '2' })
.catch((err) => {
expect(err).to.be.an(ValidationError);
expect(err.data).to.be.an('object');
expect(err.data.id).to.be.an('array');
expect(err.data).to.eql({
id: [
{
message: 'must be integer',
keyword: 'type',
params: { type: 'integer' },
data: '2',
},
],
});
});
});
});
};