Class: GrapeOAS::Introspectors::DryIntrospectorSupport::AstWalker
- Inherits:
-
Object
- Object
- GrapeOAS::Introspectors::DryIntrospectorSupport::AstWalker
- 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.
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
-
#initialize(constraint_set_class) ⇒ AstWalker
constructor
Creates a new AST walker.
-
#intersect_branches(branches) ⇒ ConstraintSet
Intersects multiple constraint branches (for OR logic).
-
#walk(ast) ⇒ ConstraintSet
Walks an AST and extracts all constraints.
Constructor Details
#initialize(constraint_set_class) ⇒ AstWalker
Creates a new AST walker.
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.
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.
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 |