Class: GrapeOAS::Introspectors::DryIntrospectorSupport::AstWalker

Inherits:
Object
  • Object
show all
Defined in:
lib/grape_oas/introspectors/dry_introspector_support/ast_walker.rb

Overview

Walks Dry::Schema AST nodes and extracts validation constraints.

Dry::Schema and Dry::Validation use an AST representation for their rules. This walker traverses the AST and extracts constraints (enum values, min/max, nullable, etc.) into a ConstraintSet that can be applied to OpenAPI schemas.

Examples:

Walking a simple AST

walker = AstWalker.new(ConstraintSet)
constraints = walker.walk([:predicate, [:type?, [String]]])

Constant Summary collapse

LOGIC_TAGS =

AST tags that represent logical/structural nodes vs predicates

%i[predicate rule and or implication not key each].freeze

Instance Method Summary collapse

Constructor Details

#initialize(constraint_set_class) ⇒ AstWalker

Creates a new AST walker.

Parameters:

  • constraint_set_class (Class)

    the class to use for constraint aggregation



23
24
25
# File 'lib/grape_oas/introspectors/dry_introspector_support/ast_walker.rb', line 23

def initialize(constraint_set_class)
  @constraint_set_class = constraint_set_class
end

Instance Method Details

#intersect_branches(branches) ⇒ ConstraintSet

Intersects multiple constraint branches (for OR logic).

When handling OR branches, only constraints that apply to ALL branches should be included in the output. This method computes the intersection.

Parameters:

  • branches (Array<ConstraintSet>)

    the branches to intersect

Returns:

  • (ConstraintSet)

    the intersected constraints



44
45
46
47
48
49
50
51
52
# File 'lib/grape_oas/introspectors/dry_introspector_support/ast_walker.rb', line 44

def intersect_branches(branches)
  return branches.first if branches.size <= 1

  base = branches.first
  branches[1..].each do |b|
    intersect_branch(base, b)
  end
  base
end

#walk(ast) ⇒ ConstraintSet

Walks an AST and extracts all constraints.

Parameters:

  • ast (Array)

    the Dry::Schema AST node

Returns:

  • (ConstraintSet)

    the extracted constraints



31
32
33
34
35
# File 'lib/grape_oas/introspectors/dry_introspector_support/ast_walker.rb', line 31

def walk(ast)
  constraints = constraint_set_class.new(unhandled_predicates: [])
  visit(ast, constraints)
  constraints
end