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 current IR documents.
Constant Summary
collapse
- POSITION =
"(ir):1:1"
Instance Method Summary
collapse
-
#array(value, path) ⇒ Array[json_value]
-
#boolean(value, path) ⇒ Boolean
-
#child_path(path, key) ⇒ String
-
#enum(value, path, values) ⇒ String
-
#field(value, key, path) ⇒ json_value
-
#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) ⇒ json_object
-
#positive_integer(value, path) ⇒ Integer
-
#record(value, path, required, optional = []) ⇒ json_object
-
#string(value, path) ⇒ String
Instance Method Details
#array(value, path) ⇒ Array[json_value]
37
38
39
40
41
42
43
44
|
# File 'lib/ibex/ir/validator/base.rb', line 37
def array(value, path)
if value.is_a?(Array)
array = value return array
end
invalid(path, "must be an array")
end
|
#boolean(value, path) ⇒ Boolean
75
76
77
78
79
80
81
82
|
# File 'lib/ibex/ir/validator/base.rb', line 75
def boolean(value, path)
if [true, false].include?(value)
result = value return result
end
invalid(path, "must be a boolean")
end
|
#child_path(path, key) ⇒ String
136
137
138
|
# File 'lib/ibex/ir/validator/base.rb', line 136
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
90
91
92
93
94
|
# File 'lib/ibex/ir/validator/base.rb', line 90
def enum(value, path, values)
value = string(value, path)
invalid(path, "must be one of #{values.join(', ')}") unless values.include?(value)
value
end
|
#field(value, key, path) ⇒ json_value
131
132
133
|
# File 'lib/ibex/ir/validator/base.rb', line 131
def field(value, key, path)
value.fetch(key) { invalid(path, "is missing required field #{key.inspect}") }
end
|
#integer(value, path) ⇒ Integer
61
62
63
64
65
|
# File 'lib/ibex/ir/validator/base.rb', line 61
def integer(value, path)
return value if value.is_a?(Integer)
invalid(path, "must be an integer")
end
|
#invalid(path, message) ⇒ bot
141
142
143
|
# File 'lib/ibex/ir/validator/base.rb', line 141
def invalid(path, message)
raise Ibex::Error, "#{POSITION}: #{path} #{message}"
end
|
#literal(value, path, expected) ⇒ void
This method returns an undefined value.
85
86
87
|
# File 'lib/ibex/ir/validator/base.rb', line 85
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.
97
98
99
100
101
102
103
104
|
# File 'lib/ibex/ir/validator/base.rb', line 97
def location(value, path, nullable: true)
return if nullable && value.nil?
value = 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.
121
122
123
124
125
126
127
128
|
# File 'lib/ibex/ir/validator/base.rb', line 121
def metadata(value, path)
return if value.nil?
value = 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
54
55
56
57
58
|
# File 'lib/ibex/ir/validator/base.rb', line 54
def nonempty_string(value, path)
value = string(value, path)
invalid(path, "must not be empty") if value.empty?
value
end
|
#nonnegative_integer(value, path) ⇒ Integer
68
69
70
71
72
|
# File 'lib/ibex/ir/validator/base.rb', line 68
def nonnegative_integer(value, path)
value = integer(value, path)
invalid(path, "must be greater than or equal to 0") if value.negative?
value
end
|
#nullable_string(value, path) ⇒ String?
114
115
116
117
118
|
# File 'lib/ibex/ir/validator/base.rb', line 114
def nullable_string(value, path)
return nil if value.nil?
string(value, path)
end
|
#object(value, path) ⇒ json_object
27
28
29
30
31
32
33
34
|
# File 'lib/ibex/ir/validator/base.rb', line 27
def object(value, path)
if value.is_a?(Hash)
object = value return object
end
invalid(path, "must be an object")
end
|
#positive_integer(value, path) ⇒ Integer
107
108
109
110
111
|
# File 'lib/ibex/ir/validator/base.rb', line 107
def positive_integer(value, path)
value = integer(value, path)
invalid(path, "must be greater than or equal to 1") unless value.positive?
value
end
|
#record(value, path, required, optional = []) ⇒ json_object
17
18
19
20
21
22
23
24
|
# File 'lib/ibex/ir/validator/base.rb', line 17
def record(value, path, required, optional = [])
value = 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
47
48
49
50
51
|
# File 'lib/ibex/ir/validator/base.rb', line 47
def string(value, path)
return value if value.is_a?(String)
invalid(path, "must be a string")
end
|