0

I am new to Sequel, using it for the first time.

I created a table:

# connect to an in-memory database
DB = Sequel.connect('postgres://ritesh:newpassword@localhost')

# create an items table
DB.create_table :items do
  primary_key :id
  String :first_name
  String :last_name
  String :email
  String : zipcode
  String : company_name
  String : google
  String :skype
  String :phone 
  String :about
  String :linkedin_profile_url
end

end

I want to put a regular expression constraint on the email field:

VALID EMAIL REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

and similar validations I have to put on other columns when we use the model.

I found "Model Validations" for adding validation, but I am creating a table.

How do I put validation in the create_table method? If I have to use this table for a model, how do I convert from the table to a model, or use Model?

I am using grape only, no Rails is used. It's a simple Rake app.

1
  • 1
    Be cautious using that regex to validate the address. It can only show that the address string conforms to a small subset of the email address spec. The true spec requires a very complex pattern that is much, much, longer. And, if a string passes that test, it still doesn't mean it is valid, i.e., that there is a user at the other end. Only that can be determined by sending a message to that address and getting a response back from that person. Commented Jan 30, 2013 at 14:12

1 Answer 1

0

You can put into your model something like:

class Model < Sequel::Model
  plugin :validation_helpers

  def validate
    validates_format /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i, :email
  end
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.