File: fragment_resolution_test.rb

package info (click to toggle)
ruby-json-schema 2.8.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 896 kB
  • sloc: ruby: 5,806; makefile: 6
file content (83 lines) | stat: -rw-r--r-- 2,077 bytes parent folder | download | duplicates (3)
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
require File.expand_path('../support/test_helper', __FILE__)

class FragmentResolutionTest < Minitest::Test
  def test_fragment_resolution
    schema = {
      "$schema" => "http://json-schema.org/draft-04/schema#",
      "required" => ["a"],
      "properties" => {
        "a" => {
          "type" => "object",
          "properties" => {
            "b" => {"type" => "integer" }
          }
        }
      }
    }

    data = {"b" => 5}
    refute_valid schema, data
    assert_valid schema, data, :fragment => "#/properties/a"

    assert_raises JSON::Schema::SchemaError do
      JSON::Validator.validate!(schema,data,:fragment => "/properties/a")
    end

    assert_raises JSON::Schema::SchemaError do
      JSON::Validator.validate!(schema,data,:fragment => "#/properties/b")
    end
  end

  def test_odd_level_fragment_resolution
    schema = {
      "foo" => {
        "type" => "object",
        "required" => ["a"],
        "properties" => {
          "a" => {"type" => "integer"}
        }
      }
    }

    assert_valid schema, {"a" => 1}, :fragment => "#/foo"
    refute_valid schema, {}, :fragment => "#/foo"
  end

  def test_even_level_fragment_resolution
    schema = {
      "foo" => {
        "bar" => {
          "type" => "object",
          "required" => ["a"],
          "properties" => {
            "a" => {"type" => "integer"}
          }
        }
      }
    }

    assert_valid schema, {"a" => 1}, :fragment => "#/foo/bar"
    refute_valid schema, {}, :fragment => "#/foo/bar"
  end

  def test_array_fragment_resolution
    schema = {
      "type" => "object",
      "required" => ["a"],
      "properties" => {
        "a" => {
          "anyOf" => [
            {"type" => "integer"},
            {"type" => "string"}
          ]
        }
      }
    }

    refute_valid schema, "foo", :fragment => "#/properties/a/anyOf/0"
    assert_valid schema, "foo", :fragment => "#/properties/a/anyOf/1"

    assert_valid schema, 5, :fragment => "#/properties/a/anyOf/0"
    refute_valid schema, 5, :fragment => "#/properties/a/anyOf/1"
  end
end