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

with v3: error unknown format "date-time" #2146

Closed
alqu opened this issue Nov 17, 2021 · 10 comments
Closed

with v3: error unknown format "date-time" #2146

alqu opened this issue Nov 17, 2021 · 10 comments

Comments

@alqu
Copy link

alqu commented Nov 17, 2021

I updated objection in my project from v2.2.18 to v3.0.0 and suddenly my models are kind of invalid:
image
and there is an error at passwordUpdatedAt-field, which has the "date-time"-Format:

error: Error in request unknown format "date-time" ignored in schema at path "#/properties/passwordUpdatedAt"
    at unknownFormat (.../node_modules/ajv/lib/vocabularies/format/format.ts:87:15)
    at validateFormat (.../node_modules/ajv/lib/vocabularies/format/format.ts:75:9)
    at Object.code (.../node_modules/ajv/lib/vocabularies/format/format.ts:40:10)
    at keywordCode (.../node_modules/ajv/lib/compile/validate/index.ts:523:9)
    at .../node_modules/ajv/lib/compile/validate/index.ts:265:9
    at CodeGen.code (.../node_modules/ajv/lib/compile/codegen/index.ts:525:33)
    at CodeGen.block (.../node_modules/ajv/lib/compile/codegen/index.ts:680:20)
    at iterateKeywords (.../node_modules/ajv/lib/compile/validate/index.ts:262:7)
    at groupKeywords (.../node_modules/ajv/lib/compile/validate/index.ts:241:7)
    at .../node_modules/ajv/lib/compile/validate/index.ts:233:38

I am running a koaJS-app on node v14.18.1. with knex v0.95.14.

Regarding the upgrade instructions at https://vincit.github.io/objection.js/release-notes/migration.html everything should work out of the box - no problems there.

The model was working perfectly in v2.x.
Do you have any clue, what am I doing wrong?

@vdechef
Copy link

vdechef commented Nov 17, 2021

Same probleme here (and here #2147 ).

Based on #2142 I tried adding ajv-formats to enforce the validation: the error disapear, but the validation is not done. I can provide any data and it will still pass the validator.

My object contains a field theDate that should be a date (YYYY-MM-DD). But whatever the value of this field, the data is sent to the database: I can do patch({ theDate: "this_is_not_a_date" }) without validation error.

Here is what I did to enable ajv-formats:

import { Model, AjvValidator } from "objection"
import addFormats from('ajv-formats')

export default class MyModel extends Model {
    ...
    // enable ajv formats to  validate the date field when inserting in database
    static createValidator() {
      return new AjvValidator({
        onCreateAjv: (ajv) => {
          addFormats(ajv)
        },
        options: {
          allErrors: true,
          validateSchema: true,
          ownProperties: true
        },
      })
    }

    static get jsonSchema() {
        return {
            type: "object",
            required: ["theDate"],
            properties: {
                // if providing something that is not a date, this should cause an error   
                theDate: {
                    type: "string",
                    format: "date"
                }
            }
        }
    }
}

@koskimas
Copy link
Collaborator

This is the third issue about the exact same thing.

@alqu
Copy link
Author

alqu commented Nov 17, 2021

This is the third issue about the exact same thing.

I see. This is kind of enerving.
Would you recommend to add ajv-formats to my BaseModel to make it work again or is it more a thing like "this should be validated at a different location, at least not here"?.

@jstayton
Copy link

Hey @koskimas, I totally understand your frustration with the duplicate issues.

I ran into this issue myself today when upgrading to v3. If I may, I'd recommend adding this as a breaking change to the v3 migration guide. Objection.js v2 uses Ajv v6, which includes formats out of the box. Starting with Ajv v7, however – Objection.js v3 uses Ajv v8 – they separated formats into its own package that you have to manually add. In my mind, that's a breaking change to Objection.js, since it exposes the Ajv functionality.

Just a suggestion!

@koskimas
Copy link
Collaborator

Feel free to create a PR. I'll merge it.

@paulbjensen
Copy link

Just ran into this issue now. Thanks @vdechef for the workaround, I will use that until #2218 gets merged and is in a release.

@DominicRoyStang
Copy link

Same probleme here (and here #2147 ).

Based on #2142 I tried adding ajv-formats to enforce the validation: the error disapear, but the validation is not done. I can provide any data and it will still pass the validator.

My object contains a field theDate that should be a date (YYYY-MM-DD). But whatever the value of this field, the data is sent to the database: I can do patch({ theDate: "this_is_not_a_date" }) without validation error.

Here is what I did to enable ajv-formats:

import { Model, AjvValidator } from "objection"
import addFormats from('ajv-formats')

export default class MyModel extends Model {
    ...
    // enable ajv formats to  validate the date field when inserting in database
    static createValidator() {
      return new AjvValidator({
        onCreateAjv: (ajv) => {
          addFormats(ajv)
        },
        options: {
          allErrors: true,
          validateSchema: true,
          ownProperties: true
        },
      })
    }

    static get jsonSchema() {
        return {
            type: "object",
            required: ["theDate"],
            properties: {
                // if providing something that is not a date, this should cause an error   
                theDate: {
                    type: "string",
                    format: "date"
                }
            }
        }
    }
}

@vdechef I believe you may need to add validateSchema: true to the options when you create the AjvValidator if you want your date to be validated.

@jboxxx
Copy link

jboxxx commented Jun 12, 2022

https://stackoverflow.com/questions/55521116/can-i-validate-a-date-using-ajv-json-schema-without-converting-the-date-to-stri

helpful to me if you DONT want to convert your date into a string

@poacher2k
Copy link

Feel free to create a PR. I'll merge it.

@koskimas : Thank you so much for the wonderful library! A PR for this was added in #2218, have you had a chance to look at this?

@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

9 participants