File: fragment_resolution_test.rb

package info (click to toggle)
ruby-json-schema 6.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 960 kB
  • sloc: ruby: 6,373; makefile: 4
file content (101 lines) | stat: -rw-r--r-- 2,524 bytes parent folder | download
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
101
require File.expand_path('support/test_helper', __dir__)

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

  def test_fragment_with_escape_sequences_resolution
    schema = {
      'content' => {
        'application/json' => {
          'type' => 'object',
          'required' => ['a'],
          'properties' => {
            'a' => { 'type' => 'integer' },
          },
        },
      },
    }

    assert_valid schema, { 'a' => 1 }, fragment: '#/content/application~1json'
    refute_valid schema, {}, fragment: '#/content/application~1json'
  end
end