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

type must be JSONType or JSONType[]: date-time #2142

Closed
thebrownfox opened this issue Nov 11, 2021 · 15 comments
Closed

type must be JSONType or JSONType[]: date-time #2142

thebrownfox opened this issue Nov 11, 2021 · 15 comments

Comments

@thebrownfox
Copy link

After update to the 3.0.0 I got this validation error.

static get jsonSchema() {
		return {
			type: "object",
			required: [
				"first_name",
				"last_name",
				"email",
				"phone_number",
			],
			properties: {
				first_name: { type: "string" },
				last_name: { type: "string" },
				last_change: { type: "date-time" },
				email: { type: "email" },
				phone_number: { type: "string" },
				last_try: { type: "date-time" },
				last_logged: { type: "date-time" },
			},
		};
	}
const now = new Date(Date.now());
const lastLogged = response.isValid ? { last_logged: now } : {};

const toPatch = {
	last_try: now,
	...lastLogged,
};

await user.$query().patch(toPatch);
@koskimas
Copy link
Collaborator

koskimas commented Nov 11, 2021

date-time is not a valid JSON schema type. It's been working before by accident. I think Ajv has added more validation in recent versions. email is also not a valid type.

Valid types are JSON types string, number, boolean, object, array and null (and also integer). You can use the format field to implement additional format checks like date-time or email. See what formats Ajv supports by default. You may need to add a library like ajv-formats to make those work.

@koskimas
Copy link
Collaborator

Here's the JSON schema spec https://json-schema.org/specification.html

@thebrownfox
Copy link
Author

Thank you, I see now 😄

https://ajv.js.org/guide/formats.html

@thebrownfox
Copy link
Author

Is there any way how to use/import ajv-formats globally in objection?

@thebrownfox
Copy link
Author

@WafaBoussada
Copy link

Ok my fault RTFM smile https://vincit.github.io/objection.js/api/types/#class-ajvvalidator

how can i use it in model like this please @thefoxie
class Cart extends Model {
// Table name is the only required property.
static get tableName() {
return 'cart';
}

static get jsonSchema() {
return {
type: 'object',
properties: {
id: { type: 'integer' },
history_id: { type: 'integer' },
currency_id: { type: ['integer', 'null'] },
modification_date: { type: 'datetime' },
},
};
}
}

@thebrownfox
Copy link
Author

@WafaBoussada you have to change it somewhere where you init objectionjs. In my case:

import knex from "knex";
import Objection from "objection";
import addFormats from "ajv-formats";
import config from "./config.js";

const { Model: ObjectionModel, AjvValidator } = Objection;

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

const knexInit = knex(config[process.env.NODE_ENV || "development"]);
Model.knex(knexInit);
export default Model;

@WafaBoussada
Copy link

@thefoxie in my case i did this i create file validate model with this code
const {Model, AjvValidator} = require('objection')
const addFormats = require("ajv-formats")

class ValidatedModel extends Model {
static createValidator() {
return new AjvValidator({
onCreateAjv: (ajv) => {
addFormats(ajv);
},
options: {
$data: true,
allErrors: true,
validateSchema: false,
ownProperties: true,
v5: true,
coerceTypes: true,
removeAdditional: true,
},
});
}
}

module.exports = ValidatedModel

and then i do this in the model cart
class Cart extends ValidatedModel {
// Table name is the only required property.
static get tableName() {
return 'cart';
}

static get jsonSchema() {
return {
type: 'object',
properties: {
id: { type: 'integer' },
history_id: { type: 'integer' },
currency_id: { type: ['integer', 'null'] },
modification_date: { type: 'datetime' },
},
};
}
}

but it didin't work the same error always type must be JSONType or JSONType[]: date

@thebrownfox
Copy link
Author

@WafaBoussada see https://ajv.js.org/guide/formats.html
{ type: "datetime"} is not valid. Try to use { type: "string", format: "date-time" } instead. Take into account, that it must be a string -> you have to parse it from js Date object.

@WafaBoussada
Copy link

@thefoxie i got this error unknown format "date-time" ignored in schema at path "#/properties/modification_date"

@thebrownfox
Copy link
Author

@WafaBoussada might be somewhere else (in different model).

@WafaBoussada
Copy link

@thefoxie i will see thxx

@WafaBoussada
Copy link

@thefoxie hello for the type timestamp when i put { type: "string", format: "timestamp" } it make an error. i think timestamp format not used in ajv-formats. there is a solution fo this ?

@thebrownfox
Copy link
Author

@WafaBoussada { type: "string", format: "date-time" } should work I guess.

@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