Class: SpecForge::Normalizer::Validators

Inherits:
Object
  • Object
show all
Defined in:
lib/spec_forge/normalizer/validators.rb

Overview

Provides validation methods for Normalizer structures

Contains validation functions that can be referenced by name in structure definitions to perform custom validation logic.

Examples:

Validator in a structure definition

http_verb: {type: String, validator: :http_verb}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label) ⇒ Validators

Initializes a new validator instance with a context label

Parameters:

  • label (String)

    A descriptive label for error messages



37
38
39
# File 'lib/spec_forge/normalizer/validators.rb', line 37

def initialize(label)
  @label = label
end

Class Method Details

.call(method_name, value, label:) ⇒ void

This method returns an undefined value.

Calls a validator method with the provided value and context

Parameters:

  • method_name (Symbol, String)

    The validator method to call

  • value (Object)

    The value to validate

  • label (String)

    A descriptive label for error messages

Raises:

  • (Error)

    If validation fails



26
27
28
# File 'lib/spec_forge/normalizer/validators.rb', line 26

def self.call(method_name, value, label:)
  new(label).public_send(method_name, value)
end

Instance Method Details

#callback(value) ⇒ Object

Validates a callback definition

Ensures the callback has a valid structure with a required name and optional arguments. Handles both single callbacks and arrays of callbacks.

Examples:

Using the validator in a structure

call: {type: Hash, validator: :callback}

Parameters:

  • value (Array<Hash>)

    The callback definition(s) to validate

Raises:



134
135
136
137
138
# File 'lib/spec_forge/normalizer/validators.rb', line 134

def callback(value)
  return if value.blank?

  value.each { |v| Normalizer.validate!(v, using: :callback) }
end

#http_verb(value) ⇒ Object

Validates that a value is a supported HTTP verb

Ensures the provided value is one of the supported HTTP methods (GET, POST, PUT, PATCH, DELETE). Case-insensitive matching is used.

Examples:

Using the validator in a structure

http_verb: {type: String, validator: :http_verb}

Parameters:

  • value (String, Symbol, nil)

    The HTTP verb to validate, or nil

Raises:

  • (Error)

    If the value is not a supported HTTP verb



71
72
73
74
75
76
# File 'lib/spec_forge/normalizer/validators.rb', line 71

def http_verb(value)
  valid_verbs = HTTP::Verb::VERBS.values.map(&:to_s)
  return if value.blank? || valid_verbs.include?(value.to_s.upcase)

  raise Error, "Invalid HTTP verb #{value.in_quotes} for #{@label}. Valid values are: #{valid_verbs.join_map(", ", &:in_quotes)}"
end

#json_expectation(value) ⇒ Object

Validates that shape and schema are not both defined

Ensures only one of shape or schema is used for JSON validation, as they represent different validation approaches that cannot be combined.

Examples:

Using the validator in a structure

json: {type: Hash, validator: :json_expectation}

Parameters:

  • value (Hash)

    The expectation hash containing shape and/or schema keys

Raises:

  • (Error)

    If both shape and schema are defined



91
92
93
94
95
96
# File 'lib/spec_forge/normalizer/validators.rb', line 91

def json_expectation(value)
  # Both shape and schema cannot be defined at the same time
  return if value[:shape].blank? || value[:schema].blank?

  raise Error, "Cannot define both \"shape\" and \"schema\". Use \"shape\" for simple validation or \"schema\" for explicit control."
end

#json_schema(value) ⇒ Object

Validates a JSON schema structure recursively

Ensures the schema definition follows the expected format, validating nested structures and patterns.

Parameters:

  • value (Hash)

    The schema definition to validate

Raises:



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/spec_forge/normalizer/validators.rb', line 108

def json_schema(value)
  Normalizer.validate!(value, using: :json_schema)
  json_schema(value[:pattern]) if value[:pattern]

  case value[:structure]
  when Array
    value[:structure].each { |v| json_schema(v) }
  when Hash
    value[:structure].each_value { |v| json_schema(v) }
  end
end

#present?(value) ⇒ Boolean

Validates that a value is not blank

Ensures the provided value is not nil, empty, or contains only whitespace. This validator is useful for required fields that must have meaningful content.

Examples:

Using the validator in a structure

name: {type: String, validator: :present?}

Parameters:

  • value (Object)

    The value to validate

Returns:

  • (Boolean)

Raises:

  • (Error)

    If the value is blank



54
55
56
# File 'lib/spec_forge/normalizer/validators.rb', line 54

def present?(value)
  raise Error, "Value cannot be blank for #{@label}" if value.blank?
end