Class: Hyrax::OrcidValidator

Inherits:
ActiveModel::Validator
  • Object
show all
Defined in:
app/models/hyrax/orcid_validator.rb

Constant Summary collapse

ORCID_REGEXP =
%r{^(?<prefix>https?://orcid.org/)?(?<orcid>\d{4}-\d{4}-\d{4}-\d{3}[\dX])/?$}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extract_bare_orcid(from:) ⇒ Object

Parameters:

  • from (String)

Returns:

  • nil if the given string is not in the Orcid form

  • string of the form “0000-0000-0000-0000” if the given string conforms to Orcid’s format

See Also:



15
16
17
# File 'app/models/hyrax/orcid_validator.rb', line 15

def self.extract_bare_orcid(from:)
  ORCID_REGEXP.match(from) { |m| m[:orcid] }
end

Instance Method Details

#validate(record) ⇒ Object



5
6
7
8
# File 'app/models/hyrax/orcid_validator.rb', line 5

def validate(record)
  return if record.orcid.blank?
  record.errors.add(:orcid, 'must be a string of 19 characters, e.g., "0000-0000-0000-0000"') unless ORCID_REGEXP.match?(record.orcid)
end