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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
mathlearner
  • 7,509
  • 31
  • 126
  • 189
  • 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. – the Tin Man Jan 30 '13 at 14:12

1 Answers1

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
taro
  • 5,772
  • 2
  • 30
  • 34