forest-express-sequelize icon indicating copy to clipboard operation
forest-express-sequelize copied to clipboard

Validation regex are not correctly escaped in .forestadmin-schema.json

Open swan-admin opened this issue 6 years ago • 4 comments

Expected behavior

The validation decorator parser should escape the regex correctly in order to not break the .forestadmin-schema.json file

Actual behavior

When a validation regex applied on a field model contains a \ character, for instance:

@Table()
class MyModel {
   @Is(/^\+\d{7,15}$/)
   @Column
   phoneNumber?: string;
}

The generated .forestadmin-schema.json file is broken because of the string is not correctly escaped:

"validations": [{
        "message": null,
        "type": "is like",
        "value": "/^\\+\d{7,15}$/"
}]

Here, only the first \ is escaped, not the second one.

I take a look in the code and I found this: https://github.com/ForestAdmin/forest-express-sequelize/blob/cd7059e6abf868414c1b8f40c05e1a117b71efa6/src/services/apimap-field-builder.js#L96

But I can't figure out why the first \ is escaped and not the second one.

Failure Logs

When we start the app containing the forest-sequelize middleware in production mode, we can see the log below: The content of .forestadmin-schema.json file is not a correct JSON.

Context

Create a project with forest-express-sequelize middleware Create in this project a sequelize model that contains a field validated with a regex containing at least one \. Start the project in production mode The generated .forestadmin-schema.json file is broken.

  • Package Version: 3.3.0
  • Express Version: 4.17.1
  • Sequelize Version: 5.10.1
  • Database Dialect: pg
  • Database Version: 11

swan-admin avatar Aug 21 '19 16:08 swan-admin

Hi @swan-admin , Thanks for your feedback.

Can you try to upgrade to the latest liana version (v3.3.0) https://github.com/ForestAdmin/forest-express-sequelize/blob/devel/CHANGELOG.md#release-330---2019-08-07 ?

As we recently worked on and released a few patches about this topic, maybe it will close your issue. Let us know.

arnaudbesnier avatar Aug 22 '19 06:08 arnaudbesnier

Hi @arnaudbesnier,

Thanks for your quick response 🙏

We already tried it, but the problem continues.

PS : We found a workaround by replacing the \d by [0-9], but it doesn't resolve the problem :D

swan-admin avatar Aug 22 '19 07:08 swan-admin

Ok thanks, let me update your issue description to specify v3.3.0 instead of v3.2.4.

arnaudbesnier avatar Aug 22 '19 07:08 arnaudbesnier

fwiw, my workaround for this was to add parentheses around any escaped character

ex: /(\A)(\d+)(\.)(\d+)(\.)(\d+)(\z)/

rayzeller avatar Oct 27 '20 15:10 rayzeller