Class: SpecForge::Normalizer::Validators
- Inherits:
-
Object
- Object
- SpecForge::Normalizer::Validators
- 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.
Class Method Summary collapse
-
.call(method_name, value, label:) ⇒ void
Calls a validator method with the provided value and context.
Instance Method Summary collapse
-
#callback(value) ⇒ Object
Validates a callback definition.
-
#http_verb(value) ⇒ Object
Validates that a value is a supported HTTP verb.
-
#initialize(label) ⇒ Validators
constructor
Initializes a new validator instance with a context label.
-
#json_expectation(value) ⇒ Object
Validates that shape and schema are not both defined.
-
#json_schema(value) ⇒ Object
Validates a JSON schema structure recursively.
-
#present?(value) ⇒ Boolean
Validates that a value is not blank.
Constructor Details
#initialize(label) ⇒ Validators
Initializes a new validator instance with a context label
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
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.
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.
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.
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.
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.
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 |