Class: Ibex::IR::Validator::Base
- Inherits:
-
Object
- Object
- Ibex::IR::Validator::Base
show all
- Defined in:
- lib/ibex/ir/validator/base.rb,
sig/ibex/ir/validator/base.rbs
Overview
Shared JSON-shape checks for the two version-1 IR documents.
Constant Summary
collapse
- POSITION =
"(ir):1:1"
Instance Method Summary
collapse
-
#array(value, path) ⇒ Array[untyped]
-
#boolean(value, path) ⇒ Boolean
-
#child_path(path, key) ⇒ String
-
#enum(value, path, values) ⇒ String
-
#field(value, key, path) ⇒ Object
-
#integer(value, path) ⇒ Integer
-
#invalid(path, message) ⇒ bot
-
#literal(value, path, expected) ⇒ void
-
#location(value, path, nullable: true) ⇒ void
-
#metadata(value, path) ⇒ void
-
#nonempty_string(value, path) ⇒ String
-
#nonnegative_integer(value, path) ⇒ Integer
-
#nullable_string(value, path) ⇒ String?
-
#object(value, path) ⇒ Hash[String, untyped]
-
#positive_integer(value, path) ⇒ Integer
-
#record(value, path, required, optional = []) ⇒ Hash[String, untyped]
-
#string(value, path) ⇒ String
Instance Method Details
#array(value, path) ⇒ Array[untyped]
29
30
31
32
|
# File 'lib/ibex/ir/validator/base.rb', line 29
def array(value, path)
invalid(path, "must be an array") unless value.is_a?(Array)
value
end
|
#boolean(value, path) ⇒ Boolean
61
62
63
64
|
# File 'lib/ibex/ir/validator/base.rb', line 61
def boolean(value, path)
invalid(path, "must be a boolean") unless [true, false].include?(value)
value
end
|
#child_path(path, key) ⇒ String
118
119
120
|
# File 'lib/ibex/ir/validator/base.rb', line 118
def child_path(path, key)
key.match?(/\A[$A-Za-z_][$A-Za-z0-9_]*\z/) ? "#{path}.#{key}" : "#{path}[#{key.inspect}]"
end
|
#enum(value, path, values) ⇒ String
72
73
74
75
76
|
# File 'lib/ibex/ir/validator/base.rb', line 72
def enum(value, path, values)
string(value, path)
invalid(path, "must be one of #{values.join(', ')}") unless values.include?(value)
value
end
|
#field(value, key, path) ⇒ Object
113
114
115
|
# File 'lib/ibex/ir/validator/base.rb', line 113
def field(value, key, path)
value.fetch(key) { invalid(path, "is missing required field #{key.inspect}") }
end
|
#integer(value, path) ⇒ Integer
48
49
50
51
|
# File 'lib/ibex/ir/validator/base.rb', line 48
def integer(value, path)
invalid(path, "must be an integer") unless value.is_a?(Integer)
value
end
|
#invalid(path, message) ⇒ bot
123
124
125
|
# File 'lib/ibex/ir/validator/base.rb', line 123
def invalid(path, message)
raise Ibex::Error, "#{POSITION}: #{path} #{message}"
end
|
#literal(value, path, expected) ⇒ void
This method returns an undefined value.
67
68
69
|
# File 'lib/ibex/ir/validator/base.rb', line 67
def literal(value, path, expected)
invalid(path, "must be #{expected.inspect}") unless value == expected
end
|
#location(value, path, nullable: true) ⇒ void
This method returns an undefined value.
79
80
81
82
83
84
85
86
|
# File 'lib/ibex/ir/validator/base.rb', line 79
def location(value, path, nullable: true)
return if nullable && value.nil?
record(value, path, %w[file line column])
string(value["file"], "#{path}.file")
positive_integer(value["line"], "#{path}.line")
positive_integer(value["column"], "#{path}.column")
end
|
This method returns an undefined value.
103
104
105
106
107
108
109
110
|
# File 'lib/ibex/ir/validator/base.rb', line 103
def metadata(value, path)
return if value.nil?
string(value, path)
invalid(path, "must not be empty") if value.strip.empty?
invalid(path, "must be a single line") if value.match?(/[\r\n]/)
invalid(path, "must not contain control characters") if value.match?(/[[:cntrl:]]/)
end
|
#nonempty_string(value, path) ⇒ String
41
42
43
44
45
|
# File 'lib/ibex/ir/validator/base.rb', line 41
def nonempty_string(value, path)
string(value, path)
invalid(path, "must not be empty") if value.empty?
value
end
|
#nonnegative_integer(value, path) ⇒ Integer
54
55
56
57
58
|
# File 'lib/ibex/ir/validator/base.rb', line 54
def nonnegative_integer(value, path)
integer(value, path)
invalid(path, "must be greater than or equal to 0") if value.negative?
value
end
|
#nullable_string(value, path) ⇒ String?
96
97
98
99
100
|
# File 'lib/ibex/ir/validator/base.rb', line 96
def nullable_string(value, path)
return nil if value.nil?
string(value, path)
end
|
#object(value, path) ⇒ Hash[String, untyped]
23
24
25
26
|
# File 'lib/ibex/ir/validator/base.rb', line 23
def object(value, path)
invalid(path, "must be an object") unless value.is_a?(Hash)
value
end
|
#positive_integer(value, path) ⇒ Integer
89
90
91
92
93
|
# File 'lib/ibex/ir/validator/base.rb', line 89
def positive_integer(value, path)
integer(value, path)
invalid(path, "must be greater than or equal to 1") unless value.positive?
value
end
|
#record(value, path, required, optional = []) ⇒ Hash[String, untyped]
13
14
15
16
17
18
19
20
|
# File 'lib/ibex/ir/validator/base.rb', line 13
def record(value, path, required, optional = [])
object(value, path)
missing = required.reject { |key| value.key?(key) }
invalid(path, "is missing required field #{missing.first.inspect}") unless missing.empty?
unknown = value.keys - required - optional
invalid(path, "has unsupported field #{unknown.first.inspect}") unless unknown.empty?
value
end
|
#string(value, path) ⇒ String
35
36
37
38
|
# File 'lib/ibex/ir/validator/base.rb', line 35
def string(value, path)
invalid(path, "must be a string") unless value.is_a?(String)
value
end
|