5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-25 03:56:22 +00:00

feat: validar que las uris sean uris

This commit is contained in:
f 2024-03-06 17:45:21 -03:00
parent 7aa14bd292
commit a8f184ecbf
No known key found for this signature in database
4 changed files with 24 additions and 3 deletions

View file

@ -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) }

View file

@ -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.

View file

@ -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

View 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