Class: TOOL_INFO::VALIDATOR
Overview
simple JSON validator
this validator use simple schema. array length cannot be checked. you have to check array length in your code, if necessary.
Instance Method Summary collapse
-
#error(msg) ⇒ Object
@b_ok::Bool.
- #get_object_type(obj) ⇒ Object
-
#initialize(name, schema) ⇒ VALIDATOR
constructor
A new instance of VALIDATOR.
-
#validate ⇒ Object
VALIDATOR#validate.
- #validate_array_member(array, val_types, path) ⇒ Object
- #validate_object(object, require_type, path) ⇒ Object
-
#validate_types(obj, val_type, path) ⇒ Object
TOOL_INFO::VALIDATOR#validate_types obj::Object (Integer, Floating, String, Hash) val_type::Symbol : required object type.
Constructor Details
#initialize(name, schema) ⇒ VALIDATOR
Returns a new instance of VALIDATOR.
126 127 128 129 130 |
# File 'lib/tecsgen/core/tool_info.rb', line 126 def initialize(name, schema) @name = name @schema = schema @b_ok = true end |
Instance Method Details
#error(msg) ⇒ Object
@b_ok::Bool
121 122 123 124 |
# File 'lib/tecsgen/core/tool_info.rb', line 121 def error(msg) STDERR.print("__tool_info__: " + msg) @b_ok = false end |
#get_object_type(obj) ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/tecsgen/core/tool_info.rb', line 244 def get_object_type(obj) # p "#{obj.class} #{obj.to_s}" if obj.is_a? Integer return :integer elsif obj.is_a? Float return :number elsif obj.is_a? String return :string elsif obj.is_a? Hash return obj[:type].to_sym end return nil end |
#validate ⇒ Object
VALIDATOR#validate
133 134 135 136 137 138 139 140 141 |
# File 'lib/tecsgen/core/tool_info.rb', line 133 def validate info = TOOL_INFO.get_tool_info @name if info.nil? error("\"#{@name}\" not found\n") return @b_ok end validate_object info, @name, @name return @b_ok end |
#validate_array_member(array, val_types, path) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/tecsgen/core/tool_info.rb', line 182 def validate_array_member(array, val_types, path) if !array.is_a? Array error("#{path2}: array required as value\n") return end index = 0 array.each{|member| type = get_object_type member i = val_types.find_index type if i.nil? if type == :integer i = val_types.find_index :number end end if i.nil? error("#{path}: array member type mismatch, #{type} for #{val_types}\n") next end val_type = val_types[i] validate_types member, val_type, (path.to_s + "[" + index.to_s + "].") index += 1 } end |
#validate_object(object, require_type, path) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/tecsgen/core/tool_info.rb', line 143 def validate_object(object, require_type, path) obj_type = @schema[require_type] dbgPrint "validate_object: #{path} object=#{obj_type[:type]} required=#{object[:type]}\n" obj_type.each{|name, val_type| val = object[name] path2 = path.to_s + "." + name.to_s if val.nil? error("#{path}: required property not found '#{name}'\n") next end if val_type.is_a? Array validate_array_member val, val_type, path2 else validate_types val, val_type, path2 end } optional = @schema[("__" + require_type.to_s).to_sym] if optional dbgPrint "#{require_type} has optional\n" optional.each{|name, val_type| val = object[name] path2 = path.to_s + "." + name.to_s if val.nil? # no need to occur error # error( "#{path}: required property not found '#{name}'\n" ) next end if val_type.is_a? Array validate_array_member val, val_type, path2 else validate_types val, val_type, path2 end } end end |
#validate_types(obj, val_type, path) ⇒ Object
TOOL_INFO::VALIDATOR#validate_types
obj::Object (Integer, Floating, String, Hash) val_type::Symbol : required object type
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/tecsgen/core/tool_info.rb', line 209 def validate_types(obj, val_type, path) type = get_object_type obj case val_type when :integer if type == :integer return end when :number if type == :integer || type == :number return end when :string if type == :string return end # when :nil # mikan # when :bool # mikan else # object or fixed string if val_type.is_a? String # val is specified by String if type == :string if obj == val_type return end end else if type.to_sym == val_type validate_object(obj, val_type, path + val_type.to_s) return end end end error("#{path}: type mismatch, #{type} for #{val_type}\n") end |