Sha256: 566fe19ce6a7f2041e44138aa28763e77961b66a5b029842df048ada55026120

Contents?: true

Size: 744 Bytes

Versions: 8

Compression:

Stored size: 744 Bytes

Contents

# frozen_string_literal: true
module Hyrax
  class OrcidValidator < ActiveModel::Validator
    ORCID_REGEXP = %r{^(?<prefix>https?://orcid.org/)?(?<orcid>\d{4}-\d{4}-\d{4}-\d{3}[\dX])/?$}.freeze
    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

    # @api public
    # @param [String] from
    # @return nil if the given string is not in the Orcid form
    # @return string of the form "0000-0000-0000-0000" if the given string conforms to Orcid's format
    # @see ORCID_REGEXP
    def self.extract_bare_orcid(from:)
      ORCID_REGEXP.match(from) { |m| m[:orcid] }
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
hyrax-5.1.0.pre.beta1 app/models/hyrax/orcid_validator.rb
hyrax-5.0.4 app/models/hyrax/orcid_validator.rb
hyrax-5.0.3 app/models/hyrax/orcid_validator.rb
hyrax-5.0.2 app/models/hyrax/orcid_validator.rb
hyrax-5.0.1 app/models/hyrax/orcid_validator.rb
hyrax-5.0.0 app/models/hyrax/orcid_validator.rb
hyrax-5.0.0.rc3 app/models/hyrax/orcid_validator.rb
hyrax-5.0.0.rc2 app/models/hyrax/orcid_validator.rb