File: object_validation.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 (68 lines) | stat: -rw-r--r-- 1,838 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
module ObjectValidation
  module AdditionalPropertiesTests
    def test_additional_properties_false
      schema = {
        'properties' => {
          'a' => { 'type' => 'integer' }
        },
        'additionalProperties' => false
      }

      assert_valid schema, {'a' => 1}
      refute_valid schema, {'a' => 1, 'b' => 2}
    end

    def test_additional_properties_schema
      schema = {
        'properties' => {
          'a' => { 'type' => 'integer' }
        },
        'additionalProperties' => { 'type' => 'string' }
      }

      assert_valid schema, {'a' => 1}
      assert_valid schema, {'a' => 1, 'b' => 'hi'}
      refute_valid schema, {'a' => 1, 'b' => 2}
    end
  end

  module PatternPropertiesTests
    def test_pattern_properties
      schema = {
        'patternProperties' => {
          "\\d+ taco" => { 'type' => 'integer' }
        }
      }

      assert_valid schema, {'1 taco' => 1, '20 taco' => 20}
      assert_valid schema, {'foo' => true, '1 taco' => 1}
      refute_valid schema, {'1 taco' => 'yum'}
    end

    def test_pattern_properties_additional_properties_false
      schema = {
        'patternProperties' => {
          "\\d+ taco" => { 'type' => 'integer' }
        },
        'additionalProperties' => false
      }

      assert_valid schema, {'1 taco' => 1}
      refute_valid schema, {'1 taco' => 'yum'}
      refute_valid schema, {'1 taco' => 1, 'foo' => true}
    end

    def test_pattern_properties_additional_properties_schema
      schema = {
        'patternProperties' => {
          "\\d+ taco" => { 'type' => 'integer' }
        },
        'additionalProperties' => { 'type' => 'string' }
      }

      assert_valid schema, {'1 taco' => 1}
      assert_valid schema, {'1 taco' => 1, 'foo' => 'bar'}
      refute_valid schema, {'1 taco' => 1, 'foo' => 2}
    end
  end
end