File: formats_helper.rb

package info (click to toggle)
ruby-fog-aws 3.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,140 kB
  • sloc: ruby: 73,328; javascript: 14; makefile: 9; sh: 4
file content (100 lines) | stat: -rw-r--r-- 3,579 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# frozen_string_literal: true

require 'fog/schema/data_validator'

# format related hackery
# allows both true.is_a?(Fog::Boolean) and false.is_a?(Fog::Boolean)
# allows both nil.is_a?(Fog::Nullable::String) and ''.is_a?(Fog::Nullable::String)
module Fog
  module Boolean; end
  module Nullable
    module Boolean; end
    module Integer; end
    module String; end
    module Time; end
    module Float; end
    module Hash; end
    module Array; end
  end
end
[FalseClass, TrueClass].each { |klass| klass.send(:include, Fog::Boolean) }
[FalseClass, TrueClass, NilClass, Fog::Boolean].each { |klass| klass.send(:include, Fog::Nullable::Boolean) }
[NilClass, String].each { |klass| klass.send(:include, Fog::Nullable::String) }
[NilClass, Time].each { |klass| klass.send(:include, Fog::Nullable::Time) }
[Integer, NilClass].each { |klass| klass.send(:include, Fog::Nullable::Integer) }
[Float, NilClass].each { |klass| klass.send(:include, Fog::Nullable::Float) }
[Hash, NilClass].each { |klass| klass.send(:include, Fog::Nullable::Hash) }
[Array, NilClass].each { |klass| klass.send(:include, Fog::Nullable::Array) }

module Shindo
  # Generates a Shindo test that compares a hash schema to the result
  # of the passed in block returning true if they match.
  #
  # The schema that is passed in is a Hash or Array of hashes that
  # have Classes in place of values. When checking the schema the
  # value should match the Class.
  #
  # Strict mode will fail if the data has additional keys. Setting
  # +strict+ to +false+ will allow additional keys to appear.
  #
  # @param [Hash] schema A Hash schema
  # @param [Hash] options Options to change validation rules
  # @option options [Boolean] :allow_extra_keys
  #     If +true+ does not fail when keys are in the data that are
  #     not specified in the schema. This allows new values to
  #     appear in API output without breaking the check.
  # @option options [Boolean] :allow_optional_rules
  #     If +true+ does not fail if extra keys are in the schema
  #     that do not match the data. Not recommended!
  # @yield Data to check with schema
  #
  # @example Using in a test
  #     Shindo.tests("comparing welcome data against schema") do
  #       data = {:welcome => "Hello" }
  #       data_matches_schema(:welcome => String) { data }
  #     end
  #
  #     comparing welcome data against schema
  #     + data matches schema
  #
  # @example Example schema
  #     {
  #       "id" => String,
  #       "ram" => Integer,
  #       "disks" => [
  #         {
  #           "size" => Float
  #         }
  #       ],
  #       "dns_name" => Fog::Nullable::String,
  #       "active" => Fog::Boolean,
  #       "created" => DateTime
  #     }
  #
  # @return [Boolean]
  class Tests
    def data_matches_schema(schema, options = {})
      test('data matches schema') do
        validator = Fog::Schema::DataValidator.new
        valid = validator.validate(yield, schema, options)
        @message = validator.message unless valid
        valid
      end
    end

    # @deprecated #formats is deprecated. Use #data_matches_schema instead
    def formats(format, strict = true)
      test('has proper format') do
        if strict
          options = { allow_extra_keys: false, allow_optional_rules: true }
        else
          options = { allow_extra_keys: true, allow_optional_rules: true }
        end
        validator = Fog::Schema::DataValidator.new
        valid = validator.validate(yield, format, options)
        @message = validator.message unless valid
        valid
      end
    end
  end
end