Json field not transmitted

We have in a model a JSON field that have validation on it.
https://docs.forestadmin.com/documentation/reference-guide/models/enrich-your-models#adding-validation-to-your-models
Last time I checked, it worked fine but now, the validation fail every time because the field is not a type object. The validation fail even if I set a json model into the field. The value retrieved in the validator is still “null”

I saw that the JSON field is not sent when creating an object but is sent when updating an object

Expected behavior

When I set a model in JSON and click “Save”, the validator should receive the sent model.

Actual behavior

When I set a model in JSON and click “Save”, the validator receive a string with always the value “null” as argument

Hi @Nicolas_S,

Thank you for sharing your issue.

I reckon you made no change on your side?

Could you tell me when that “last time” was? It will help investigating on our side.
Could you also share which DB you are using and which version of our tool you is setup in your project?

Hi,

I didn’t made any change on my side recently. I don’t remember the last time I checked this feature but the last time I updated the code, it was to update db models the 16 june 2020.
I have a PostgreSQL version 11.1 as DB.
Here my versions :

  • Node JS: 13.12
  • lumber-forestadmin: 1.2.2
  • forest-express-sequelize: 3.3.5
  • forest-express: 3.2.6

Sorry @Nicolas_S but I can’t reproduce your problem on my end.

Could you please share your model file and your network traces for creation and update?

I see that you version for forest-express-sequelize is a few months old.
Maybe you could try generating a new project on you database with an updated lumber-cli package to see if that solves things.

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;
};

I created a sample project on my end with a table matching the model you sent, but I still can’t reproduce the issue.
Both create and update work.

The difference I have is that I only kept the console.log call in the validator function.

Maybe this is not related, but we had an issue yesterday requiring some users to refresh their visualisation. Saying restarting you local environment fix the issue might be an hint.
Could you please follow these steps to try it?

I changed the displayName and save changes.
It works now. So maybe it was linked to the issue you mentioned.

Great to know!

Thank you for the feedback.

After a few tests, the bug disappear when changing reference name but come back when I change my environment then return to the previous environment.