mirror of
https://0xacab.org/sutty/sutty
synced 2024-11-25 03:46:21 +00:00
feat: validar que las uris sean uris
This commit is contained in:
parent
7aa14bd292
commit
a8f184ecbf
4 changed files with 24 additions and 3 deletions
|
@ -21,7 +21,7 @@ class ActivityPub
|
|||
|
||||
validates :activity_pub_id, presence: true
|
||||
# Las actividades son únicas con respecto a su estado
|
||||
validates :uri, presence: true, uniqueness: { scope: :activity_pub_id, message: 'estado duplicado' }
|
||||
validates :uri, presence: true, url: true, uniqueness: { scope: :activity_pub_id, message: 'estado duplicado' }
|
||||
|
||||
# Siempre en orden descendiente para saber el último estado
|
||||
default_scope -> { order(created_at: :desc) }
|
||||
|
|
|
@ -16,7 +16,7 @@ class ActivityPub
|
|||
has_many :remote_flags
|
||||
|
||||
# Les actores son únicxs a toda la base de datos
|
||||
validates :uri, presence: true, uniqueness: true
|
||||
validates :uri, presence: true, url: true, uniqueness: true
|
||||
|
||||
# Obtiene el nombre de la Actor como mención, solo si obtuvimos el
|
||||
# contenido de antemano.
|
||||
|
|
|
@ -6,7 +6,7 @@ class ActivityPub
|
|||
include ActivityPub::Concerns::JsonLdConcern
|
||||
|
||||
# Los objetos son únicos a toda la base de datos
|
||||
validates :uri, presence: true, uniqueness: true
|
||||
validates :uri, presence: true, url: true, uniqueness: true
|
||||
|
||||
has_many :activity_pubs, as: :object
|
||||
|
||||
|
|
21
app/validators/url_validator.rb
Normal file
21
app/validators/url_validator.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# Valida URLs
|
||||
#
|
||||
# @see {https://storck.io/posts/better-http-url-validation-in-ruby-on-rails/}
|
||||
class UrlValidator < ActiveModel::EachValidator
|
||||
def validate_each(record, attribute, value)
|
||||
if value.blank?
|
||||
record.errors.add(attribute, :url_missing)
|
||||
return
|
||||
end
|
||||
|
||||
uri = URI.parse(value)
|
||||
|
||||
record.errors.add(attribute, :scheme_missing) if uri.scheme.blank?
|
||||
record.errors.add(attribute, :host_missing) if uri.host.blank?
|
||||
record.errors.add(attribute, :path_missing) if uri.path.blank?
|
||||
rescue URI::Error
|
||||
record.errors.add(attribute, :invalid)
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue