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

How to load ajv-formats for objection? (upgrading from v2) #2298

Closed
coolaj86 opened this issue Jul 11, 2022 · 2 comments
Closed

How to load ajv-formats for objection? (upgrading from v2) #2298

coolaj86 opened this issue Jul 11, 2022 · 2 comments

Comments

@coolaj86
Copy link

Due to complications with the latest version of npm, we've decided to upgrade from Objection v2 to the latest v3.

As part of the upgrade we've lost the ability to use the format option in the json schemas. We get errors like this:

unknown format "email" ignored in schema at path "#/properties/email"
    unknownFormat       |63  |/node_modules/objection/node_modules/ajv/dist/vocabularies/format/format.js
    validateFormat      |50  |/node_modules/objection/node_modules/ajv/dist/vocabularies/format/format.js
    code                |22  |/node_modules/objection/node_modules/ajv/dist/vocabularies/format/format.js
    keywordCode         |454 |/node_modules/objection/node_modules/ajv/dist/compile/validate/index.js
    null                |222 |/node_modules/objection/node_modules/ajv/dist/compile/validate/index.js
    code                |439 |/node_modules/objection/node_modules/ajv/dist/compile/codegen/index.js
    block               |568 |/node_modules/objection/node_modules/ajv/dist/compile/codegen/index.js
    iterateKeywords     |219 |/node_modules/objection/node_modules/ajv/dist/compile/validate/index.js
    groupKeywords       |200 |/node_modules/objection/node_modules/ajv/dist/compile/validate/index.js
    null                |192 |/node_modules/objection/node_modules/ajv/dist/compile/validate/index.js
    code                |439 |/node_modules/objection/node_modules/ajv/dist/compile/codegen/index.js
    block               |568 |/node_modules/objection/node_modules/ajv/dist/compile/codegen/index.js
    schemaKeywords      |190 |/node_modules/objection/node_modules/ajv/dist/compile/validate/index.js
    typeAndKeywords     |128 |/node_modules/objection/node_modules/ajv/dist/compile/validate/index.js
    subSchemaObjCode    |115 |/node_modules/objection/node_modules/ajv/dist/compile/validate/index.js
    subschemaCode       |91  |/node_modules/objection/node_modules/ajv/dist/compile/validate/index.js

(another unfortunate result is that we lose the stack trace into our code - despite preferring await over returning Promises, which we know can cause the call stack to be lost)

Our model code looks like this:

'use strict';

let Objection = require('objection');
let Model = Objection.Model;
let BaseModel = require('./base-model.js');

module.exports = class User extends BaseModel {
  static tableName = 'user';

  static timestamps = true;

  // ...

  static jsonSchema = {
    type: 'object',
    required: ['email'],

    properties: {
      email: { type: 'string', format: 'email' },
      password_digest: { type: ['string', null] },
      // ...
      created_at: { type: 'string', format: 'date-time' },
      updated_at: { type: 'string', format: 'date-time' },
    },
  };

  // ...
}

The offending line, although not specified in the stack trace, is obviously email: { type: 'string', format: 'email' },.

We see that the latest version of ajv has moved validations to ajv-formats, and that calling addFormats(ajv) requires an instance of ajv.

We're not clear on how we update Objection's instance of ajv to use ajv-formats.

What can we do to get the validations working again?

@eduardo-veras
Copy link

@coolaj86 take a look at this response: #2146 (comment)

It shows how to implement the ajv-formats on a model.

@lehni
Copy link
Collaborator

lehni commented Apr 13, 2023

Closing since it was answered

@lehni lehni closed this as completed Apr 13, 2023
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

No branches or pull requests

3 participants