0

I have a model, User:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, :trackable
  include DeviseTokenAuth::Concerns::User
  extend EncryptedField

  validates :email, uniqueness: true, allow_nil: true
  validates :mobile,
    presence: true,
    uniqueness: true,
    phone: { possible: false, types: [:mobile], allow_blank: false }
end

On sign_up on postman:

At route localhost:3000/auth with body:

{
    "first_name": "RaghJhu",
    "last_name": "Deesp",
    "mobile": "+91 9726544141",
    "answer": "Yes1",
    "email": "apoorewewewewdedgcom",
    "password": "Password@123#",
    "provider": "mobile",
    "password_confirmation": "Password@123#"
}

returns response User created.

email is not a required field.

I have also defined email_regexp in config/initializers/devise.rb:

config.email_regexp = /\A[^@\s]+@[^@\s]+\z/

Can you tell me why it is not validating email?

Anas Ansari
  • 143
  • 2
  • 10
  • remove , allow_nil: true – Divyarajsinh Jul 29 '22 at 08:43
  • I am unable to recreate this on Rails 7. From the code, it looks like this Rails app is quite old, and I am gonna guess that some behaviour of devise has changed. However, you can try writing a custom validation https://guides.rubyonrails.org/active_record_validations.html – Ankit Aug 01 '22 at 20:52

1 Answers1

1

please set below email validation:

validates_format_of :email, with: URI::MailTo::EMAIL_REGEXP