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:


21
22
23
# File 'app/models/hyrax/orcid_validator.rb', line 21

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

.match(string) ⇒ Object

Deprecated.

11
12
13
14
# File 'app/models/hyrax/orcid_validator.rb', line 11

def self.match(string)
  Deprecation.warn "Use 'Hyrax::OrcidValidator.extract_bare_orcid(from:)'"
  extract_bare_orcid_from(from: string)
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