Class: Validation

Inherits:
Object
  • Object
show all
Includes:
DeepEquality, HashInitialized, Inspectable
Defined in:
lib/json_patterns.rb

Constant Summary

Constants included from Inspectable

Inspectable::INSPECTING_KEY

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HashInitialized

#initialize

Methods included from DeepEquality

#==

Methods included from Inspectable

#inspect

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_membersObject



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

Returns:



331
332
333
# File 'lib/json_patterns.rb', line 331

def expects_an_object?
  false
end

#shallow_describeObject



254
255
256
# File 'lib/json_patterns.rb', line 254

def shallow_describe
  Set[to_s]
end

#shallow_match?(data) ⇒ Boolean

Returns:



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