Class: Validation
- Inherits:
-
Object
- Object
- Validation
- Includes:
- DeepEquality, HashInitialized, Inspectable
- Defined in:
- lib/json_patterns.rb
Direct Known Subclasses
AnythingValidation, CyclicValueValidation, DisjunctionValidation, EmailValidation, ObjectValidation, PrimitiveTypeValidation, PrimitiveValueValidation, RegexpValidation, URLValidation, UniformArrayValidation, ValueDisjunctionValidation
Constant Summary
Constants included from Inspectable
Class Method Summary collapse
Instance Method Summary collapse
- #as_object_members ⇒ Object
- #expects_an_object? ⇒ Boolean
- #shallow_describe ⇒ Object
- #shallow_match?(data) ⇒ Boolean
- #validate_from_root(data) ⇒ Object
Methods included from HashInitialized
Methods included from DeepEquality
Methods included from Inspectable
Class Method Details
.memoized_new_from_pattern(translation, pattern) ⇒ Object
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/json_patterns.rb', line 266 def self.memoized_new_from_pattern(translation, pattern) return translation[pattern.object_id] if translation[pattern.object_id] # The below could get stuck in an infinite loop, but only if the pattern # writer is particularly mischievous, doing a deliberate assigment of the # interior of a pattern back to itself. if pattern.is_a?(CyclicPattern) return memoized_new_from_pattern(translation, pattern.interior) end case pattern when {} v = ObjectValidation.new(members: EmptyObjectMembersValidation.new) when Hash v = ObjectValidation.new(members: nil) when UniformArrayPattern v = UniformArrayValidation.new when DisjunctionPattern v = DisjunctionValidation.new when Regexp v = RegexpValidation.new(regexp: pattern.dup) when JsonType v = PrimitiveTypeValidation.new(type: pattern.dup) when Symbol v = PrimitiveTypeValidation.new(type: JsonType.new(pattern)) when Class if pattern == Email v = EmailValidation.new elsif pattern == URL v = URLValidation.new else v = PrimitiveTypeValidation.new(type: JsonType.new_from_class(pattern)) end when String v = PrimitiveValueValidation.new(value: pattern.dup) when Numeric v = PrimitiveValueValidation.new(value: pattern) when TrueClass v = PrimitiveValueValidation.new(value: pattern) when FalseClass v = PrimitiveValueValidation.new(value: pattern) when NilClass v = PrimitiveValueValidation.new(value: pattern) when AnythingPattern v = AnythingValidation.new else raise "Unrecognized type in pattern: #{pattern.class}" end translation[pattern.object_id] = v case pattern when {} when Hash v.members = ObjectMembersValidation.memoized_new_from_pattern(translation, pattern) when UniformArrayPattern v.value_validation = memoized_new_from_pattern(translation, pattern.value) when DisjunctionPattern v.alternatives = pattern.alternatives.map { |a| memoized_new_from_pattern(translation, a) } end return v end |
.new_from_pattern(pattern) ⇒ Object
262 263 264 |
# File 'lib/json_patterns.rb', line 262 def self.new_from_pattern(pattern) self.memoized_new_from_pattern({}, pattern) end |
Instance Method Details
#as_object_members ⇒ Object
335 336 337 |
# File 'lib/json_patterns.rb', line 335 def as_object_members raise "attempted to treat non-object validation as object members: #{self}" end |
#expects_an_object? ⇒ Boolean
331 332 333 |
# File 'lib/json_patterns.rb', line 331 def expects_an_object? false end |
#shallow_describe ⇒ Object
254 255 256 |
# File 'lib/json_patterns.rb', line 254 def shallow_describe Set[to_s] end |
#shallow_match?(data) ⇒ Boolean
250 251 252 |
# File 'lib/json_patterns.rb', line 250 def shallow_match?(data) validate([], data).empty? end |
#validate_from_root(data) ⇒ Object
258 259 260 |
# File 'lib/json_patterns.rb', line 258 def validate_from_root(data) validate([], data) end |