### Install ValidEmail2 Source: https://context7.com/micke/valid_email2/llms.txt Add the gem to your Gemfile and install it via bundle or direct command. ```ruby # Gemfile gem "valid_email2" # Then run: bundle install # Or install directly: gem install valid_email2 ``` -------------------------------- ### Install valid_email2 Source: https://github.com/micke/valid_email2/blob/main/README.md Add the gem to your Gemfile or install it directly via command line. ```ruby gem "valid_email2" ``` ```bash $ bundle ``` ```bash $ gem install valid_email2 ``` -------------------------------- ### YAML Configuration for Allow/Deny Lists Source: https://context7.com/micke/valid_email2/llms.txt Configure allow and deny lists for email domains using YAML files. These lists help bypass disposable email checks or always reject specific domains. ```yaml # config/allow_listed_email_domains.yml # Domains that should bypass disposable email checks - example.com - trusted-temp-service.org - company-testing.com # config/deny_listed_email_domains.yml # Domains that should always be rejected - competitor.com - known-spam-domain.org - blocked-service.net ``` -------------------------------- ### Configure Deny List Source: https://github.com/micke/valid_email2/blob/main/README.md Define domains to be blocked in a YAML file. ```yaml # config/deny_listed_email_domains.yml - denied1.example.com - denied2.example.com ``` -------------------------------- ### Combine Validation Options Source: https://context7.com/micke/valid_email2/llms.txt Demonstrates applying multiple validation rules simultaneously for comprehensive email verification. ```ruby class User < ActiveRecord::Base validates :email, presence: true, 'valid_email_2/email': { mx: true, # Verify MX records exist disposable: true, # Block disposable emails disallow_subaddressing: true, # Block plus addressing message: "must be a valid, non-disposable email" } end class StrictUser < ActiveRecord::Base validates :email, presence: true, 'valid_email_2/email': { strict_mx: true, disposable: true, disallow_subaddressing: true, disallow_dotted: true, dns_timeout: 10, dns_nameserver: ['8.8.8.8', '8.8.4.4'] } end ``` -------------------------------- ### Disposable Validation with Allow List Source: https://context7.com/micke/valid_email2/llms.txt Permit specific disposable domains by configuring an allow list in a YAML file. ```ruby class User < ActiveRecord::Base # Block disposable emails except those in allow list validates :email, 'valid_email_2/email': { disposable_with_allow_list: true } # Or domain-only check with allow list validates :email, 'valid_email_2/email': { disposable_domain_with_allow_list: true } end # config/allow_listed_email_domains.yml # - allowed-disposable.com # - special-temp-domain.org ``` -------------------------------- ### Git Branching and Committing Source: https://github.com/micke/valid_email2/blob/main/README.md Standard Git commands for creating a new feature branch, committing changes, and pushing to a remote repository. ```git git checkout -b my-new-feature ``` ```git git commit -am 'Add some feature' ``` ```git git push origin my-new-feature ``` -------------------------------- ### Configure DNS Timeout Source: https://context7.com/micke/valid_email2/llms.txt Adjust the DNS lookup timeout duration for MX record verification. ```ruby class User < ActiveRecord::Base # Set 10 second timeout for DNS lookups validates :email, 'valid_email_2/email': { mx: true, dns_timeout: 10 } # Or with strict MX validates :email, 'valid_email_2/email': { strict_mx: true, dns_timeout: 15 } end ``` -------------------------------- ### Override DNS Nameservers Source: https://context7.com/micke/valid_email2/llms.txt Specify custom DNS nameservers for performing MX lookups. ```ruby class User < ActiveRecord::Base # Use Google's public DNS servers validates :email, 'valid_email_2/email': { mx: true, dns_nameserver: ['8.8.8.8', '8.8.4.4'] } end ``` -------------------------------- ### Validate Disposable Domains Only Source: https://context7.com/micke/valid_email2/llms.txt Perform a faster check by validating only the domain against the disposable list, skipping MX lookups. ```ruby class User < ActiveRecord::Base # Only check domain, skip MX server lookup validates :email, 'valid_email_2/email': { disposable_domain: true } end # Usage - faster validation without DNS lookup user = User.new(email: "user@tempmail.com") user.valid? # => false if domain is in disposable list ``` -------------------------------- ### Configure Custom Error Messages Source: https://context7.com/micke/valid_email2/llms.txt Allows customization of validation error messages using static strings, lambdas, or procs. ```ruby class User < ActiveRecord::Base # Static message validates :email, 'valid_email_2/email': { message: "is not a valid email address" } end class Contact < ActiveRecord::Base # Dynamic message with lambda validates :email, 'valid_email_2/email': { message: -> { "must be a valid email (entered at #{Time.now})" } } end class Subscriber < ActiveRecord::Base # Dynamic message with proc validates :email, 'valid_email_2/email': { message: Proc.new { "please enter a properly formatted email" } } end # Usage user = User.new(email: "invalid") user.valid? user.errors.full_messages # => ["Email is not a valid email address"] ``` -------------------------------- ### Configure Permitted Multibyte Characters Source: https://context7.com/micke/valid_email2/llms.txt Globally configure allowed multibyte characters for email addresses. Set to nil to disallow all multibyte characters. ```ruby # Configure allowed multibyte characters globally ValidEmail2::Address.permitted_multibyte_characters_regex = /[ÆæØøÅåÄäÖöÞþÐð]/ # Now addresses with these characters are valid address = ValidEmail2::Address.new("jöhn@example.com") address.valid? # => true # Without configuration, multibyte characters cause validation failure ValidEmail2::Address.permitted_multibyte_characters_regex = nil address = ValidEmail2::Address.new("jöhn@example.com") address.valid? # => false ``` -------------------------------- ### Validate Disposable Emails Source: https://context7.com/micke/valid_email2/llms.txt Block temporary email services by checking both the domain and the MX server. ```ruby class User < ActiveRecord::Base # Check domain AND MX server against disposable list validates :email, 'valid_email_2/email': { disposable: true } end # Usage user = User.new(email: "user@gmail.com") user.valid? # => true user = User.new(email: "user@mailinator.com") user.valid? # => false (known disposable domain) user = User.new(email: "user@subdomain.mailinator.com") user.valid? # => false (subdomain of disposable domain) ``` -------------------------------- ### ActiveModel Email Validation Source: https://context7.com/micke/valid_email2/llms.txt Documentation for integrating email validation into ActiveRecord models using various options like deny lists, subaddressing, and dotted local parts. ```APIDOC ## ActiveModel Email Validation ### Description Use the 'valid_email_2/email' validator within ActiveRecord models to enforce email formatting and business rules. ### Options - **deny_list** (boolean) - Blocks domains specified in config/deny_listed_email_domains.yml. - **disallow_subaddressing** (boolean) - Prevents RFC5233 plus-addressing (e.g., user+tag@example.com). - **disallow_dotted** (boolean) - Prevents dots in the local part of the email address. - **multiple** (boolean) - Allows validation of comma-separated strings or arrays of email addresses. - **message** (string/lambda/proc) - Custom error message for validation failures. - **mx** (boolean) - Verifies that the domain has valid MX records. - **disposable** (boolean) - Blocks known disposable email domains. ### Example ```ruby validates :email, 'valid_email_2/email': { mx: true, disposable: true, disallow_subaddressing: true } ``` ``` -------------------------------- ### Validate MX Records Source: https://context7.com/micke/valid_email2/llms.txt Verify that the email domain has valid MX or A records to ensure deliverability. ```ruby class User < ActiveRecord::Base # Validates MX or A records exist validates :email, 'valid_email_2/email': { mx: true } end # Usage user = User.new(email: "user@gmail.com") user.valid? # => true (gmail.com has MX records) user = User.new(email: "user@nonexistent-domain-xyz.com") user.valid? # => false (no MX/A records found) ``` -------------------------------- ### Deny List Email Validation Source: https://context7.com/micke/valid_email2/llms.txt Blocks specific email domains by referencing a configuration file. Requires a YAML file at config/deny_listed_email_domains.yml. ```ruby class User < ActiveRecord::Base validates :email, 'valid_email_2/email': { deny_list: true } end # config/deny_listed_email_domains.yml # - competitor.com # - blocked-domain.org # - spam-source.net # Usage user = User.new(email: "user@competitor.com") user.valid? # => false (domain is deny-listed) user = User.new(email: "user@gmail.com") user.valid? # => true ``` -------------------------------- ### Clear DNS Cache Source: https://context7.com/micke/valid_email2/llms.txt Clear the built-in DNS cache for MX lookups. This is useful in testing scenarios or after configuration changes. ```ruby # Clear the DNS cache (useful in tests or after configuration changes) ValidEmail2::Dns.clear_cache ``` -------------------------------- ### Strict MX Record Validation Source: https://context7.com/micke/valid_email2/llms.txt Require specific MX records, rejecting domains that only provide A records. ```ruby class User < ActiveRecord::Base # Strictly requires MX records (A records not sufficient) validates :email, 'valid_email_2/email': { strict_mx: true } end # Usage user = User.new(email: "user@gmail.com") user.valid? # => true (has MX records) user = User.new(email: "user@ghs.google.com") user.valid? # => false (only has A records, no MX) ``` -------------------------------- ### Customize Prohibited Domain Characters Source: https://context7.com/micke/valid_email2/llms.txt Customize characters that are prohibited in domain names. The default set includes space, '#', and '+'. ```ruby # Default prohibited characters: + ! _ / space ' # ` ValidEmail2::Address.prohibited_domain_characters_regex # => /[+!_\/\s'#`]/ # Customize prohibited characters ValidEmail2::Address.prohibited_domain_characters_regex = /[+!_\/\s'#`@]/ # Now domains with @ are also rejected address = ValidEmail2::Address.new("user@bad@domain.com") address.valid_domain? # => false ``` -------------------------------- ### Validate Email Format Source: https://context7.com/micke/valid_email2/llms.txt Use the ActiveModel validator to ensure email addresses are properly formatted. ```ruby # app/models/user.rb class User < ActiveRecord::Base validates :email, presence: true, 'valid_email_2/email': true end # Usage user = User.new(email: "john@example.com") user.valid? # => true user = User.new(email: "invalid-email") user.valid? # => false user = User.new(email: "foo@bar") # Missing TLD user.valid? # => false user = User.new(email: "foo@bar..com") # Consecutive dots user.valid? # => false user = User.new(email: ".foo@bar.com") # Starts with dot user.valid? # => false ``` -------------------------------- ### Stub Validation in Tests Source: https://github.com/micke/valid_email2/blob/main/README.md Prevent DNS lookups during testing by stubbing the validation methods in your spec helper. ```ruby config.before(:each) do allow_any_instance_of(ValidEmail2::Address).to receive_messages( valid_mx?: true, valid_strict_mx?: true, mx_server_is_in?: false ) end ``` -------------------------------- ### Validate Email with ActiveModel Source: https://github.com/micke/valid_email2/blob/main/README.md Use the 'valid_email_2/email' validator in your ActiveRecord models to enforce email format and domain rules. ```ruby class User < ActiveRecord::Base validates :email, presence: true, 'valid_email_2/email': true end ``` ```ruby validates :email, 'valid_email_2/email': { mx: true } ``` ```ruby validates :email, 'valid_email_2/email': { strict_mx: true } ``` ```ruby validates :email, 'valid_email_2/email': { strict_mx: true, dns_timeout: 10 } ``` ```ruby validates :email, 'valid_email_2/email': { mx: true, dns_nameserver: ['8.8.8.8', '8.8.4.4'] } ``` ```ruby validates :email, 'valid_email_2/email': { disposable: true } ``` ```ruby validates :email, 'valid_email_2/email': { disposable_domain: true } ``` ```ruby validates :email, 'valid_email_2/email': { disposable_with_allow_list: true } ``` ```ruby validates :email, 'valid_email_2/email': { disposable_domain_with_allow_list: true } ``` ```ruby validates :email, 'valid_email_2/email': { deny_list: true } ``` ```ruby validates :email, 'valid_email_2/email': { disallow_subaddressing: true } ``` ```ruby validates :email, 'valid_email_2/email': { disallow_dotted: true } ``` ```ruby validates :email, 'valid_email_2/email': { message: "is not a valid email" } ``` ```ruby validates :email, 'valid_email_2/email': { multiple: true } ``` ```ruby validates :email, 'valid_email_2/email': { mx: true, disposable: true, disallow_subaddressing: true} ``` -------------------------------- ### Validate Multiple Email Addresses Source: https://context7.com/micke/valid_email2/llms.txt Supports validation for comma-separated strings or arrays of email addresses. ```ruby class Newsletter < ActiveRecord::Base validates :recipients, 'valid_email_2/email': { multiple: true } end # Usage with comma-separated string newsletter = Newsletter.new(recipients: "user1@gmail.com, user2@example.com") newsletter.valid? # => true # Usage with array newsletter = Newsletter.new(recipients: ["user1@gmail.com", "user2@example.com"]) newsletter.valid? # => true # Fails if any address is invalid newsletter = Newsletter.new(recipients: "user1@gmail.com, invalid-email") newsletter.valid? # => false ``` -------------------------------- ### Disallow Dotted Local Parts Source: https://context7.com/micke/valid_email2/llms.txt Restricts email addresses that contain dots in the local part, often used to prevent duplicate account creation on services like Gmail. ```ruby class User < ActiveRecord::Base validates :email, 'valid_email_2/email': { disallow_dotted: true } end # Usage user = User.new(email: "johndoe@gmail.com") user.valid? # => true user = User.new(email: "john.doe@gmail.com") user.valid? # => false (contains dot in local part) ``` -------------------------------- ### Clear DNS Cache Between Tests in RSpec Source: https://context7.com/micke/valid_email2/llms.txt Ensure the DNS cache is cleared before each test in RSpec to prevent test pollution and ensure accurate MX lookups. ```ruby # Clear DNS cache between tests RSpec.configure do |config| config.before(:each) do ValidEmail2::Dns.clear_cache end end ``` -------------------------------- ### Standalone Address Validation Source: https://context7.com/micke/valid_email2/llms.txt Programmatic validation of email addresses outside of ActiveModel using the ValidEmail2::Address class. ```APIDOC ## ValidEmail2::Address ### Description Provides a standalone interface to check email validity, MX records, disposable status, and formatting constraints. ### Methods - **valid?** - Returns true if the email is syntactically valid. - **valid_mx?** - Checks for MX or A records. - **disposable?** - Checks if the domain is a known disposable email provider. - **subaddressed?** - Checks if the email contains a plus-address. - **dotted?** - Checks if the local part contains a dot. ### Example ```ruby address = ValidEmail2::Address.new("user@gmail.com") address.valid? # => true address.disposable? # => false ``` ``` -------------------------------- ### Stub MX Validation in RSpec Tests Source: https://context7.com/micke/valid_email2/llms.txt Stub MX validation methods in RSpec tests to avoid network dependencies. This ensures tests are fast and reliable. ```ruby # spec/spec_helper.rb or spec/rails_helper.rb RSpec.configure do |config| config.before(:each) do allow_any_instance_of(ValidEmail2::Address).to receive_messages( valid_mx?: true, valid_strict_mx?: true, mx_server_is_in?: false ) end end ``` -------------------------------- ### Standalone Address Validation Source: https://context7.com/micke/valid_email2/llms.txt Provides programmatic email validation outside of ActiveModel using the ValidEmail2::Address class. ```ruby require 'valid_email2' # Create an address object address = ValidEmail2::Address.new("user@gmail.com") # Basic validation address.valid? # => true address.valid_domain? # => true address.valid_address? # => true # MX record checks address.valid_mx? # => true (has MX or A records) address.valid_strict_mx? # => true (has MX records specifically) # Disposable email checks address.disposable? # => false address.disposable_domain? # => false # Subaddressing and formatting checks address.subaddressed? # => false address.dotted? # => false # Allow/Deny list checks address.allow_listed? # => depends on config/allow_listed_email_domains.yml address.deny_listed? # => depends on config/deny_listed_email_domains.yml # Example with subaddressed email subaddressed = ValidEmail2::Address.new("user+newsletter@gmail.com") subaddressed.valid? # => true subaddressed.subaddressed? # => true # Example with dotted email dotted = ValidEmail2::Address.new("john.doe@gmail.com") dotted.valid? # => true dotted.dotted? # => true # Example with disposable email disposable = ValidEmail2::Address.new("temp@mailinator.com") disposable.valid? # => true (syntactically valid) disposable.disposable? # => true (known disposable domain) ``` -------------------------------- ### Validate Email without ActiveModel Source: https://github.com/micke/valid_email2/blob/main/README.md Use the ValidEmail2::Address class for direct validation outside of Rails models. ```ruby address = ValidEmail2::Address.new("lisinge@gmail.com") address.valid? => true address.disposable? => false address.valid_mx? => true address.valid_strict_mx? => true address.subaddressed? => false ``` ```ruby ValidEmail2::Address.permitted_multibyte_characters_regex = /[ÆæØøÅåÄäÖöÞþÐð]/ ``` -------------------------------- ### Disallow Subaddressing Source: https://context7.com/micke/valid_email2/llms.txt Prevents RFC5233 subaddressed emails, such as those containing a plus sign. ```ruby class User < ActiveRecord::Base validates :email, 'valid_email_2/email': { disallow_subaddressing: true } end # Usage user = User.new(email: "john@gmail.com") user.valid? # => true user = User.new(email: "john+newsletter@gmail.com") user.valid? # => false (contains + subaddressing) user = User.new(email: "john+work@gmail.com") user.valid? # => false ``` -------------------------------- ### Update Email Validator Usage in Rails Source: https://github.com/micke/valid_email2/blob/main/README.md When upgrading to version 2.0.0, change the direct usage of the 'email' validator to 'valid_email_2/email' to ensure the correct validator is used and avoid potential conflicts with other gems. ```ruby validates :email, email: { mx: true, disposable: true } ``` ```ruby validates :email, 'valid_email_2/email': { mx: true, disposable: true } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.