When creating an object, here the sent model :
{"data":{"attributes":{"apiKey":"toto","currency":"EUR","locationType":"SIMPHONY2X","name":"test nico","pos_configuration":"null","status":"DISABLED","uri":"http://tot.com","usesWebhooks":false},"type":"locations"}}
But pos_configuration
was set.
I just retried in my local environment after restarting the service and the problem seems to have disapeared. But in another environment when I didn’t restart the service, the problem is still there.
Here a subset of my model :
const simphony2xSchema = require('../schemas/simphony2X.json');
const Validator = require('jsonschema').Validator;
const v = new Validator();
const getSchema = locationType => {
switch (locationType) {
return simphony2xSchema;
default:
return null;
}
}
module.exports = (sequelize, DataTypes) => {
const { Sequelize } = sequelize;
const Model = sequelize.define('location', {
'status': {
type: DataTypes.ENUM(
'ENABLED',
'DISABLED',
),
},
currency: {
type: DataTypes.ENUM("EUR"),
},
name: {
type: DataTypes.STRING,
allowNull: false
},
locationType: {
type: DataTypes.ENUM(
'SIMPHONY2X',
),
field: 'location_type',
allowNull: false
},
apiKey: {
type: DataTypes.STRING,
field: 'api_key',
allowNull: false,
},
creationDate: {
type: DataTypes.DATE,
field: 'creation_date',
defaultValue: Sequelize.literal('now()'),
},
updateDate: {
type: DataTypes.DATE,
field: 'update_date',
defaultValue: Sequelize.literal('now()'),
},
pos_configuration: {
type: DataTypes.JSON,
validate: {
validator(conf) {
console.log({ conf, arguments });
const schema = getSchema(this.locationType);
if (schema !== null) {
const result = v.validate(conf, schema);
if (result.errors.length > 0) {
const anError = result.errors[0];
throw new Error(`Error in pos_configuration : ${anError.message}`);
}
}
}
}
}
}, {
tableName: 'location',
timestamps: false,
schema: process.env.DATABASE_SCHEMA,
});
Model.associate = (models) => {
//Model.hasMany(models...., {as: '...', foreignKey: 'location_id'});
};
return Model;
};