Class: Hyrax::RedirectValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
app/validators/hyrax/redirect_validator.rb

Overview

Validates the redirects attribute on a work or collection. Runs only when the redirects feature is fully enabled (config + Flipflop).

See documentation/redirects.md for the validation rules.

Constant Summary collapse

PATH_CHAR =

Allow-list per RFC 3986 path chars: unreserved + sub-delims + ":" + "@" + "/" plus pct-encoded "%XX". Rejects quotes, backslashes, angle brackets, pipes, braces, control chars, whitespace, "?", and "#" — anything Rails routing would refuse or that would be unsafe to round-trip through a URL.

%r{[A-Za-z0-9\-._~!$&'()*+,;=:@/]|%[0-9A-Fa-f]{2}}.freeze
PATH_FORMAT =
%r{\A/(?:#{PATH_CHAR})+\z}.freeze

Instance Method Summary collapse

Instance Method Details

#validate(record) ⇒ Object

Override validate so we can short-circuit before ActiveModel calls record.read_attribute_for_validation, which would crash on records that don't have the redirects attribute.

Two guards, each catching a distinct case:

  1. config + Flipflop — the runtime feature gate. Skips validation when the feature isn't actively in use.
  2. record_supports_redirects? — the structural gate. Catches the case where both feature gates are on but the property wasn't added


25
26
27
28
29
# File 'app/validators/hyrax/redirect_validator.rb', line 25

def validate(record)
  return unless Hyrax.config.redirects_active?
  return unless record_supports_redirects?(record)
  super
end

#validate_each(record, attribute, entries) ⇒ Object



31
32
33
34
35
36
37
38
# File 'app/validators/hyrax/redirect_validator.rb', line 31

def validate_each(record, attribute, entries)
  return if entries.blank?

  validate_each_entry(record, attribute, entries)
  validate_intra_record_uniqueness(record, attribute, entries)
  validate_global_uniqueness(record, attribute, entries)
  validate_at_most_one_display_url(record, attribute, entries)
end