Multiline regexp validation generates an invalid .forestadmin-schema.json file

Expected behavior

I have a model HttpRequest, which validates presence and format of a field ‘uri’.
The validation in the model is
validates :uri, presence: true, format: { with: URI::DEFAULT_PARSER.make_regexp }

This regexp is

/
        ([a-zA-Z][\-+.a-zA-Z\d]*):                           (?# 1: scheme)
        (?:
           ((?:[\-_.!~*'()a-zA-Z\d;?:@&=+$,]|%[a-fA-F\d]{2})(?:[\-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]]|%[a-fA-F\d]{2})*)                    (?# 2: opaque)
        |
           (?:(?:
             \/\/(?:
                 (?:(?:((?:[\-_.!~*'()a-zA-Z\d;:&=+$,]|%[a-fA-F\d]{2})*)@)?        (?# 3: userinfo)
                   (?:((?:(?:[a-zA-Z0-9\-.]|%\h\h)+|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|\[(?:(?:[a-fA-F\d]{1,4}:)*(?:[a-fA-F\d]{1,4}|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|(?:(?:[a-fA-F\d]{1,4}:)*[a-fA-F\d]{1,4})?::(?:(?:[a-fA-F\d]{1,4}:)*(?:[a-fA-F\d]{1,4}|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))?)\]))(?::(\d*))?))? (?# 4: host, 5: port)
               |
                 ((?:[\-_.!~*'()a-zA-Z\d$,;:@&=+]|%[a-fA-F\d]{2})+)                 (?# 6: registry)
               )
             |
             (?!\/\/))                           (?# XXX: '\/\/' is the mark for hostport)
             (\/(?:[\-_.!~*'()a-zA-Z\d:@&=+$,]|%[a-fA-F\d]{2})*(?:;(?:[\-_.!~*'()a-zA-Z\d:@&=+$,]|%[a-fA-F\d]{2})*)*(?:\/(?:[\-_.!~*'()a-zA-Z\d:@&=+$,]|%[a-fA-F\d]{2})*(?:;(?:[\-_.!~*'()a-zA-Z\d:@&=+$,]|%[a-fA-F\d]{2})*)*)*)?                    (?# 7: path)
           )(?:\?((?:[\-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]]|%[a-fA-F\d]{2})*))?                 (?# 8: query)
        )
        (?:\#((?:[\-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]]|%[a-fA-F\d]{2})*))?                  (?# 9: fragment)
      /x

I expect forestadmin to handle this regexp validation.

Actual behavior

Forestadmin-schema file is an invalid json

...
}, {
        "message": null,
        "type": "is like",
        "value": "/
        ([a-zA-Z][\\-+.a-zA-Z\\d]*):                           (?# 1: scheme)
        (?:
           ((?:[\\-_.!~*'()a-zA-Z\\d;?:@&=+$,]|%[a-fA-F\\d]{2})(?:[\\-_.!~*'()a-zA-Z\\d;/?:@&=+$,\\[\]]|%[a-fA-F\\d]{2})*)                    (?# 2: opaque)
        |
           (?:(?:
...

Context

Please provide any relevant information about your setup.

  • Package Version: forest_liana (5.4.4)

Hello @remi_okarito,

Could you paste a bit more of code please?

If I understand correctly, in your ruby on rails application, you get a model HttpRequest with a field uri including validation this way:

class HttpRequest < ApplicationRecord
  validates :uri, presence: true, format: { with: URI::DEFAULT_PARSER.make_regexp }
end

I can see URI::DEFAULT_PARSER.make_regexp is defined by the uri gem:

ret[:X_ABS_URI] = "
        (#{scheme}):                           (?# 1: scheme)
        (?:
           (#{opaque_part})                    (?# 2: opaque)
        |
           (?:(?:
             //(?:
                 (?:(?:(#{userinfo})@)?        (?# 3: userinfo)
                   (?:(#{host})(?::(\\d*))?))? (?# 4: host, 5: port)
               |
                 (#{reg_name})                 (?# 6: registry)
               )
             |
             (?!//))                           (?# XXX: '//' is the mark for hostport)
             (#{abs_path})?                    (?# 7: path)
           )(?:\\?(#{query}))?                 (?# 8: query)
        )
        (?:\\#(#{fragment}))?                  (?# 9: fragment)
      "

What you are telling me is that the generated .forestadmin-schema.json is not a valid json file?
Could you add more data from you .forestadmin-schema.json please?
What is the json issue? Is there an extra double quote?

The last release of forest-rails is 6.3.7 : Releases · ForestAdmin/forest-rails · GitHub
Could you update to a newer version and see if it solves your issue please?

Let me know :slight_smile:

Here is the error from JSON Formatter (JSON Formatter & Validator)

Sending you our global schema file in DM.

It looks like the issue is because multilines are involved here. Json do not support multilines string.

It would be great to find a way to turn multilines string in ruby to a single line.
Could you try something like:

uri_regexp = URI::DEFAULT_PARSER.make_regexp.to_s.gsub(/\n+|\s+/, '')

class HttpRequest < ApplicationRecord
  validates :uri, presence: true, format: { with: /#{uri_regexp}/ }
end

Here we are recreating the regex for using a one line regexp, json formattable.

Tell me if it helps.

We have updated our code for handling this kind of behaviour.

Version 6.3.8 or forest-rails has been released :tada:
It now handles multilines regex for validation.

Thanks Guillaume, we’ll upgrade soon and let you know.

Have a great day!

Remi

2 Likes