File: ruby_schema_test.rb

package info (click to toggle)
ruby-json-schema 2.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 836 kB
  • sloc: ruby: 5,805; makefile: 10
file content (65 lines) | stat: -rw-r--r-- 1,288 bytes parent folder | download | duplicates (5)
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
require File.expand_path('../support/test_helper', __FILE__)

class RubySchemaTest < Minitest::Test
  def test_string_keys
    schema = {
      "type" => 'object',
      "required" => ["a"],
      "properties" => {
        "a" => {"type" => "integer", "default" => 42},
        "b" => {"type" => "integer"}
      }
    }

    assert_valid schema, { "a" => 5 }
  end

  def test_symbol_keys
    schema = {
      :type => 'object',
      :required => ["a"],
      :properties => {
        :a => {:type => "integer", :default => 42},
        :b => {:type => "integer"}
      }
    }

    assert_valid schema, { :a => 5 }
  end

  def test_symbol_keys_in_hash_within_array
    schema = {
      :type => 'object',
      :properties => {
        :a => {
          :type => "array",
          :items => [
            {
              :properties => {
                :b => {
                  :type => "integer"
                }
              }
            }
          ]
        }
      }
    }

    data = {
      :a => [
        {
          :b => 1
        }
      ]
    }

    assert_valid schema, data, :validate_schema => true
  end

  def test_schema_of_unrecognized_type
    assert_raises JSON::Schema::SchemaParseError do
      JSON::Validator.validate(Object.new, {})
    end
  end
end