Skip to content

Commit 1daf0b1

Browse files
fix(validation): support multilines regex (#466)
1 parent 44c4bae commit 1daf0b1

File tree

5 files changed

+29
-2
lines changed

5 files changed

+29
-2
lines changed

app/services/forest_liana/schema_adapter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ def add_validations(column_schema, column)
479479
regex = value.source
480480

481481
# NOTICE: Transform a Ruby regex into a JS one
482-
regex = regex.sub('\\A' , '^').sub('\\Z' , '$').sub('\\z' , '$')
482+
regex = regex.sub('\\A' , '^').sub('\\Z' , '$').sub('\\z' , '$').gsub(/\n+|\s+/, '')
483483

484484
column_schema[:validations] << {
485485
type: 'is like',

spec/dummy/app/models/product.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Product < ActiveRecord::Base
2+
validates :uri, presence: true, format: { with: URI::DEFAULT_PARSER.make_regexp }
3+
end
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class CreateProducts < ActiveRecord::Migration[6.0]
2+
def change
3+
create_table :products do |t|
4+
t.string :uri
5+
end
6+
end
7+
end

spec/dummy/db/schema.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2021_05_11_141752) do
13+
ActiveRecord::Schema.define(version: 2021_05_26_084712) do
1414

1515
create_table "isle", force: :cascade do |t|
1616
t.string "name"
@@ -32,6 +32,10 @@
3232
t.datetime "hired_at"
3333
end
3434

35+
create_table "products", force: :cascade do |t|
36+
t.string "uri"
37+
end
38+
3539
create_table "references", force: :cascade do |t|
3640
t.datetime "created_at", precision: 6, null: false
3741
t.datetime "updated_at", precision: 6, null: false

spec/services/forest_liana/schema_adapter_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ module ForestLiana
1212
)
1313
end
1414
end
15+
16+
context 'with a multiline regex validation' do
17+
it 'should remove new lines in validation' do
18+
19+
collection = ForestLiana.apimap.find do |object|
20+
object.name.to_s == ForestLiana.name_for(Product)
21+
end
22+
23+
uri_field = collection.fields.find { |field| field[:field] == 'uri' }
24+
uri_regex_validation = uri_field[:validations].find { |validation| validation[:type] == "is like"}
25+
expect(uri_regex_validation[:value].match('\n')).to eq(nil)
26+
end
27+
end
1528
end
1629
end
1730
end

0 commit comments

Comments
 (0)