summaryrefslogtreecommitdiff
path: root/app/validators/certificate_validator.rb
blob: de8bb179dfb0494a104a6f9d706a83c8a69eef5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# frozen_string_literal: true

# UrlValidator
#
# Custom validator for private keys.
#
#   class Project < ActiveRecord::Base
#     validates :certificate_key, certificate: true
#   end
#
class CertificateValidator < ActiveModel::EachValidator
  def validate_each(record, attribute, value)
    unless valid_certificate_pem?(value)
      record.errors.add(attribute, "must be a valid PEM certificate")
    end
  end

  private

  def valid_certificate_pem?(value)
    OpenSSL::X509::Certificate.new(value).present?
  rescue OpenSSL::X509::CertificateError
    false
  end
end