Class: DryValidationParser::ValidationSchemaParser

Inherits:
Object
  • Object
show all
Defined in:
lib/dry_validation_parser/validation_schema_parser.rb

Constant Summary collapse

PREDICATE_TO_TYPE =
{
  array?: "array",
  bool?: "boolean",
  date?: "date",
  date_time?: "datetime",
  decimal?: "decimal",
  float?: "float",
  hash?: "hash",
  int?: "integer",
  nil?: "nil",
  str?: "string",
  time?: "time",
  uuid_v1?: "uuid",
  uuid_v2?: "uuid",
  uuid_v3?: "uuid",
  uuid_v4?: "uuid",
  uuid_v5?: "uuid"
}.freeze
DESCRIPTION_MAPPING =
{
  eql?: "Must be equal to %<value>s",
  max_size?: "Maximum size: %<value>s",
  min_size?: "Minimum size: %<value>s",
  gteq?: "Greater than or equal to %<value>s",
  gt?: "Greater than %<value>s",
  lt?: "Lower than %<value>s",
  lteq?: "Lower than or equal to %<value>s"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeValidationSchemaParser

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ValidationSchemaParser.



38
39
40
# File 'lib/dry_validation_parser/validation_schema_parser.rb', line 38

def initialize
  @keys = {}
end

Instance Attribute Details

#keysObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



35
36
37
# File 'lib/dry_validation_parser/validation_schema_parser.rb', line 35

def keys
  @keys
end

Instance Method Details

#call(contract, &block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



48
49
50
51
52
53
# File 'lib/dry_validation_parser/validation_schema_parser.rb', line 48

def call(contract, &block)
  @keys = {}
  visit(contract.schema.to_ast)
  instance_eval(&block) if block_given?
  self
end

#predicate_description(name, value) ⇒ Object



138
139
140
141
# File 'lib/dry_validation_parser/validation_schema_parser.rb', line 138

def predicate_description(name, value)
  description = DESCRIPTION_MAPPING[name]
  description ? format(description, value: value) : ""
end

#to_hObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



43
44
45
# File 'lib/dry_validation_parser/validation_schema_parser.rb', line 43

def to_h
  { keys: keys }
end

#visit(node, opts = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



56
57
58
59
# File 'lib/dry_validation_parser/validation_schema_parser.rb', line 56

def visit(node, opts = {})
  meth, rest = node
  public_send(:"visit_#{meth}", rest, opts)
end

#visit_and(node, opts = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



80
81
82
83
84
85
# File 'lib/dry_validation_parser/validation_schema_parser.rb', line 80

def visit_and(node, opts = {})
  left, right = node

  visit(left, opts)
  visit(right, opts)
end

#visit_each(node, opts = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



103
104
105
# File 'lib/dry_validation_parser/validation_schema_parser.rb', line 103

def visit_each(node, opts = {})
  visit(node, opts.merge(member: true))
end

#visit_implication(node, opts = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



95
96
97
98
99
100
# File 'lib/dry_validation_parser/validation_schema_parser.rb', line 95

def visit_implication(node, opts = {})
  node.each do |el|
    opts = opts.merge(required: false)
    visit(el, opts)
  end
end

#visit_key(node, opts = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



108
109
110
111
112
113
# File 'lib/dry_validation_parser/validation_schema_parser.rb', line 108

def visit_key(node, opts = {})
  name, rest = node
  opts = opts.merge(key: name)
  opts = opts.merge(required: true)
  visit(rest, opts)
end

#visit_namespace(node, opts = {}) ⇒ Object



61
62
63
# File 'lib/dry_validation_parser/validation_schema_parser.rb', line 61

def visit_namespace(node, opts = {})
  visit(node[1], opts)
end

#visit_not(_node, opts = {}) ⇒ Object



90
91
92
# File 'lib/dry_validation_parser/validation_schema_parser.rb', line 90

def visit_not(_node, opts = {})
  keys[opts[:key]][:nullable] = true
end

#visit_or(_, _) ⇒ Object

Skip or predicate



88
# File 'lib/dry_validation_parser/validation_schema_parser.rb', line 88

def visit_or(_, _); end

#visit_predicate(node, opts = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/dry_validation_parser/validation_schema_parser.rb', line 116

def visit_predicate(node, opts = {})
  name, rest = node
  key = opts[:key]
  if name.equal?(:key?)
    keys[rest[0][1]] = { required: opts.fetch(:required, true) }
  elsif name.equal?(:array?)
    keys[key][:array] = true
  elsif name.equal?(:included_in?)
    keys[key][:enum] = rest[0][1]
    keys[key][:enum] += [nil] if opts.fetch(:nullable, false)
  elsif PREDICATE_TO_TYPE[name]
    keys[key][:type] = PREDICATE_TO_TYPE[name]
  else
    description = predicate_description(name, rest[0][1].to_s)
    if keys[key][:description].to_s.empty?
      keys[key][:description] = description unless description.to_s.empty?
    else
      keys[key][:description] += ", #{description}" unless description.to_s.empty?
    end
  end
end

#visit_set(node, opts = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/dry_validation_parser/validation_schema_parser.rb', line 66

def visit_set(node, opts = {})
  target = (key = opts[:key]) ? self.class.new : self

  node.map { |child| target.visit(child, opts) }

  return unless key

  target_info = target.to_h
  type = keys[key][:array] ? "array" : "hash"

  keys.update(key => { **keys[key], type: type, **target_info })
end