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

[Objection 3.0] unknown format "iso-date-time" ignored in schema at path #2147

Closed
rhazegh opened this issue Nov 17, 2021 · 5 comments
Closed

Comments

@rhazegh
Copy link

rhazegh commented Nov 17, 2021

"objection": "^3.0.0"
"knex": "^0.95.14"
"ajv-formats": "^2.1.1"

I updated to the latest objection and realized I had to change some models to avoid type errors (for example date is now an invalid type in the json schema). I saw #2142 and updated my code to use ajv-formats package. But I am still getting this error:

unknown format "iso-date-time" ignored in schema at path "#/properties/main/properties/registeredAt"

iso-date-time is in the list of supported formats according to https://ajv.js.org/packages/ajv-formats.html#formats

Any idea what I am doing wrong?

Here is what my model looks like:

base-model.js

import { Model, QueryBuilder, AjvValidator } from "objection";

const addFormats = require('ajv-formats').default;

class DefaultSchemaQueryBuilder extends QueryBuilder {
  constructor(modelClass) {
    super(modelClass);
    if (modelClass.defaultSchema) {
      this.withSchema(modelClass.defaultSchema);
    }
  }
}

export default class BaseModel extends Model {
  // Add all shared configurations here
  static get QueryBuilder() {
    return DefaultSchemaQueryBuilder;
  }

  static createValidator() {
    return new AjvValidator({
      onCreateAjv: (ajv) => {
        addFormats(ajv);
      },
      options: {
        allErrors: true,
        validateSchema: false,
        ownProperties: true,
        v5: true,
      },
    });
  }
}

user-setting.js

import { set } from "lodash";

import BaseModel from "./base-model";
import { languages } from "../constants";

class UserSettingAbstract extends BaseModel {
  static tableName = "user_settings";

  static get jsonSchema() {
    return {
      type: "object",
      properties: {
        id: { type: "string", format: "uuid" },
        userId: { type: "string", format: "uuid" },
        main: {
          type: "object",
          properties: {
            registeredAt: {type: "string", format: "iso-date-time" },
            dailyGoal: { type: "integer" },
            welcomedAt: { type: "string", format: "iso-date-time" },
          },
        },
      },
    };
  }
}

let dynClassMemoizer = {};

function UserSetting(lang) {
  if (!lang) {
    throw new Error(`lang: argument is required`);
  } else if (!languages.map((l) => l.code).includes(lang)) {
    throw new Error(`Invalid language: ${lang}`);
  }

  if (!dynClassMemoizer[lang]) {
    const dynClass = class extends UserSettingAbstract {
      static defaultSchema = lang;
    };
    dynClassMemoizer[lang] = dynClass;
  }
  return dynClassMemoizer[lang];
}

export default UserSetting;
@koskimas
Copy link
Collaborator

I think this is more an Ajv issue than objection issue. I don't know why that's happening.

@rhazegh
Copy link
Author

rhazegh commented Nov 17, 2021

@koskimas Understood. I used format: "date-time" instead and it's working.

I will try to find out what's going on with iso-date-time. Thanks for this fantastic project.

@rhazegh
Copy link
Author

rhazegh commented Nov 17, 2021

It seems iso-date-time is a new supported format that will appear in ajv-formats version 3.0.0 and does not exist in the version we are currently using.

@jboxxx
Copy link

jboxxx commented Jun 12, 2022

@LordA98
Copy link

LordA98 commented Oct 12, 2022

For anyone wondering, if you go down the route of adding the ajv-formats package and creating a property with type: string and format: date-time - the easiest way to get a value that matches this format is new Date().toISOString().

Wasn't obvious to me so thought I'd add a note.

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

4 participants