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

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

Open
swan-admin opened this issue Aug 21, 2019 · 4 comments
Open
Labels

Comments

@swan-admin
Copy link

swan-admin commented Aug 21, 2019

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:

if (column.validate.is && !_.isArray(column.validate.is)) {

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
@arnaudbesnier
Copy link
Member

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.

@swan-admin
Copy link
Author

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

@arnaudbesnier
Copy link
Member

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

@rayzeller
Copy link

rayzeller commented Oct 27, 2020

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants