Module: Pike13::Validators
- Defined in:
- lib/pike13/validators.rb
Overview
Validation helpers for Pike13 API parameters
Defined Under Namespace
Classes: ValidationError
Class Method Summary collapse
-
.validate_booking_params!(params) ⇒ Boolean
Validates booking parameters.
-
.validate_form_of_payment_type!(type) ⇒ Boolean
Validates form of payment type.
-
.validate_idempotency_token!(token) ⇒ Boolean
Validates idempotency token format (should be a non-empty string).
-
.validate_note_attributes!(attributes) ⇒ Boolean
Validates note attributes (must have ‘note’ key, not ‘body’).
-
.validate_person_attributes!(attributes) ⇒ Boolean
Validates person attributes for creation.
Class Method Details
.validate_booking_params!(params) ⇒ Boolean
Validates booking parameters
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/pike13/validators.rb', line 72 def self.validate_booking_params!(params) raise ValidationError, "Booking parameters must be a hash, got #{params.class}" unless params.is_a?(Hash) # Check for idempotency token token = params[:idempotency_token] || params["idempotency_token"] validate_idempotency_token!(token) if token # Require either event_occurrence_id or leases array has_event = params[:event_occurrence_id] || params["event_occurrence_id"] has_leases = params[:leases] || params["leases"] unless has_event || has_leases raise ValidationError, "Booking must have either 'event_occurrence_id' or 'leases' parameter" end true end |
.validate_form_of_payment_type!(type) ⇒ Boolean
Validates form of payment type
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/pike13/validators.rb', line 54 def self.validate_form_of_payment_type!(type) valid_types = %w[creditcard ach] raise ValidationError, "Form of payment type must be a string, got #{type.class}" unless type.is_a?(String) unless valid_types.include?(type.downcase) raise ValidationError, "Form of payment type must be one of: #{valid_types.join(", ")}. Got: '#{type}'" end true end |
.validate_idempotency_token!(token) ⇒ Boolean
Validates idempotency token format (should be a non-empty string)
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/pike13/validators.rb', line 13 def self.validate_idempotency_token!(token) raise ValidationError, "Idempotency token cannot be blank" if token.nil? || token.to_s.strip.empty? raise ValidationError, "Idempotency token must be a string, got #{token.class}" unless token.is_a?(String) if token.length > 255 raise ValidationError, "Idempotency token must be 255 characters or less, got #{token.length}" end true end |
.validate_note_attributes!(attributes) ⇒ Boolean
Validates note attributes (must have ‘note’ key, not ‘body’)
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/pike13/validators.rb', line 30 def self.validate_note_attributes!(attributes) raise ValidationError, "Note attributes must be a hash, got #{attributes.class}" unless attributes.is_a?(Hash) if attributes.key?(:body) || attributes.key?("body") raise ValidationError, "Note attributes should use 'note' key, not 'body'. " \ "Example: { note: 'text', subject: 'optional' }" end if !attributes.key?(:note) && !attributes.key?("note") raise ValidationError, "Note attributes must include 'note' key" end note_value = attributes[:note] || attributes["note"] raise ValidationError, "Note text cannot be blank" if note_value.to_s.strip.empty? true end |
.validate_person_attributes!(attributes) ⇒ Boolean
Validates person attributes for creation
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/pike13/validators.rb', line 96 def self.validate_person_attributes!(attributes) raise ValidationError, "Person attributes must be a hash, got #{attributes.class}" unless attributes.is_a?(Hash) # Check required fields first_name = attributes[:first_name] || attributes["first_name"] last_name = attributes[:last_name] || attributes["last_name"] raise ValidationError, "Person must have a first_name" if first_name.to_s.strip.empty? raise ValidationError, "Person must have a last_name" if last_name.to_s.strip.empty? # Validate email format if provided email = attributes[:email] || attributes["email"] raise ValidationError, "Invalid email format: #{email}" if email && !email.to_s.match?(/\A[^@\s]+@[^@\s]+\z/) true end |