class DefraRuby::Validators::CompaniesHouseNumberValidator

Constants

VALID_COMPANIES_HOUSE_REGISTRATION_NUMBER_REGEX

Examples we need to validate are 10997904, 09764739 SC534714, CE000958 IP00141R, IP27702R, SP02252R assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/426891/uniformResourceIdentifiersCustomerGuide.pdf

Public Instance Methods

validate_each(record, attribute, value) click to toggle source
# File lib/defra_ruby/validators/companies_house_number_validator.rb, line 16
def validate_each(record, attribute, value)
  return false unless value_is_present?(record, attribute, value)
  return false unless format_is_valid?(record, attribute, value)

  validate_with_companies_house(record, attribute, value)
end

Private Instance Methods

format_is_valid?(record, attribute, value) click to toggle source
# File lib/defra_ruby/validators/companies_house_number_validator.rb, line 32
def format_is_valid?(record, attribute, value)
  return true if value.match?(VALID_COMPANIES_HOUSE_REGISTRATION_NUMBER_REGEX)

  add_validation_error(record, attribute, :invalid_format)
  false
end
validate_with_companies_house(record, attribute, value) click to toggle source
# File lib/defra_ruby/validators/companies_house_number_validator.rb, line 39
def validate_with_companies_house(record, attribute, value)
  case CompaniesHouseService.new(value).status
  when :active
    true
  when :inactive
    add_validation_error(record, attribute, :inactive)
  when :not_found
    add_validation_error(record, attribute, :not_found)
  end
rescue StandardError
  add_validation_error(record, attribute, :error)
end
value_is_present?(record, attribute, value) click to toggle source
# File lib/defra_ruby/validators/companies_house_number_validator.rb, line 25
def value_is_present?(record, attribute, value)
  return true if value.present?

  add_validation_error(record, attribute, :blank)
  false
end