feat: add field validations to Student class and modify constructor

- Added class methods to validate the format of strings for each field:
  - `valid_name?` for surname, name, and patronymic (only letters allowed).
  - `valid_phone_number?` for phone number (validates 10-15 digits, optional '+' sign).
  - `valid_telegram?` for telegram handle (starts with '@' and allows letters, digits, and underscores).
  - `valid_email?` for email (standard email format validation).
  - `valid_git?` for Git link (validates GitHub URL format).

- Modified the `initialize` constructor:
  - Integrated validations for all fields with descriptive error messages.
  - Ensured objects cannot be created with invalid data formats.
pull/4/head
Artem-Darius Weber 4 months ago
parent 10362d3aaf
commit fca36ccd9d

@ -5,10 +5,31 @@ class Student
phone.match?(/\A\+?[0-9]{10,15}\z/)
end
def self.valid_name?(name)
name.match?(/\A[А-Яа-яЁёA-Za-z\-]+\z/)
end
def self.valid_telegram?(telegram)
telegram.nil? || telegram.match?(/\A@[A-Za-z0-9_]{5,32}\z/)
end
def self.valid_email?(email)
email.nil? || email.match?(/\A[^@\s]+@[^@\s]+\.[^@\s]+\z/)
end
def self.valid_git?(git)
git.nil? || git.match?(/\Ahttps:\/\/github\.com\/[A-Za-z0-9_\-]+\z/)
end
def initialize(args = {})
@surname = args.fetch(:surname)
raise ArgumentError, "Invalid surname format: #{@surname}" unless Student.valid_name?(@surname)
@name = args.fetch(:name)
raise ArgumentError, "Invalid name format: #{@name}" unless Student.valid_name?(@name)
@patronymic = args.fetch(:patronymic)
raise ArgumentError, "Invalid patronymic format: #{@patronymic}" unless Student.valid_name?(@patronymic)
@id = args[:id] || nil
@ -17,9 +38,14 @@ class Student
raise ArgumentError, "Invalid phone number format: #{@phone}"
end
@telegram = args[:telegram] || nil
@email = args[:email] || nil
@git = args[:git] || nil
@telegram = args[:telegram]
raise ArgumentError, "Invalid telegram format: #{@telegram}" unless Student.valid_telegram?(@telegram)
@email = args[:email]
raise ArgumentError, "Invalid email format: #{@email}" unless Student.valid_email?(@email)
@git = args[:git]
raise ArgumentError, "Invalid git format: #{@git}" unless Student.valid_git?(@git)
end
def to_s

Loading…
Cancel
Save