refactor: improve url regex

This commit is contained in:
Juan Rodriguez
2023-03-26 19:18:08 +02:00
parent fee04cc26d
commit 211e4f40f4
3 changed files with 8 additions and 5 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
name: Deploy production to Heroku
name: Rubocop Linter
on:
push:
branches:
+5 -2
View File
@@ -1,11 +1,14 @@
# frozen_string_literal: true
VALID_URL_REGEX = /\A(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)? \
([a-zA-Z0-9-]+\.[a-zA-Z]{2,}(?:\.[a-zA-Z]{2,})?) \
(?:\/[^\s]*)?\z/
class Link < ApplicationRecord
validates :url, presence: true
validates :slug, uniqueness: true
validates :url,
format: { with: /\A#{URI::DEFAULT_PARSER.make_regexp(%w[http https])}\z/, message: 'invalid format' }
validates :url, format: { with: VALID_URL_REGEX, message: 'invalid format' }
validates :url, length: { within: 3..30_000, on: :create, message: 'max length is 30000' }
before_validation :generate_slug
+2 -2
View File
@@ -4,11 +4,11 @@
Shorten an url
</label>
<div class="mt-4 flex rounded-md shadow-sm">
<%= f.text_field :url, data: { target: "links.url" }, placeholder: "https://google.com", class: "focus:ring-indigo-500 focus:border-indigo-500 flex-1 block w-full rounded-none rounded-md sm:text-sm border-gray-300" %>
<%= f.text_field :url, pattern: '(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([a-zA-Z0-9-]+\.[a-zA-Z]{2,}(?:\.[a-zA-Z]{2,})?)(?:\/[^\s]*)?', data: { target: "links.url" }, placeholder: "https://google.com", class: "focus:ring-indigo-500 focus:border-indigo-500 flex-1 block w-full rounded-none rounded-md sm:text-sm border-gray-300" %>
</div>
</div>
<div class="py-3 text-right">
<%= f.submit "Generate", class: "inline-flex w-full sm:w-auto justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500" %>
</div>
<% end %>
<% end %>