Class: Canon::Validators::BaseValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/canon/validators/base_validator.rb

Overview

Base class for all input validators

This abstract base class defines the interface that all format-specific validators must implement. Each validator is responsible for validating input in a specific format and raising detailed ValidationError when issues are found.

Class Method Summary collapse

Class Method Details

.extract_location(error) ⇒ Hash

Extract line and column information from an error

Parameters:

  • error (Exception)

    The error containing location information

Returns:

  • (Hash)

    Hash with :line and :column keys



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/canon/validators/base_validator.rb', line 28

def self.extract_location(error)
  line = nil
  column = nil

  # Try to extract line/column from error message
  if error.respond_to?(:line)
    line = error.line
  elsif error.message =~ /line[:\s]+(\d+)/i
    line = ::Regexp.last_match(1).to_i
  end

  if error.respond_to?(:column)
    column = error.column
  elsif error.message =~ /column[:\s]+(\d+)/i
    column = ::Regexp.last_match(1).to_i
  end

  { line: line, column: column }
end

.validate!(input) ⇒ void

This method returns an undefined value.

Validate input and raise ValidationError if invalid

Parameters:

  • input (String)

    The input to validate

Raises:



19
20
21
22
# File 'lib/canon/validators/base_validator.rb', line 19

def self.validate!(input)
  raise NotImplementedError,
        "#{name} must implement validate! method"
end