Skip to content

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

@rhazegh

Description

@rhazegh
"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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions