File: string_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 (154 lines) | stat: -rw-r--r-- 4,117 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
module StringValidation
  module ValueTests
    def test_minlength
      schema = {
        'properties' => {
          'a' => { 'minLength' => 1 }
        }
      }

      assert_valid schema, {'a' => 't'}
      refute_valid schema, {'a' => ''}

      # other types are disregarded
      assert_valid schema, {'a' => 5}
    end

    def test_maxlength
      schema = {
        'properties' => {
          'a' => { 'maxLength' => 2 }
        }
      }

      assert_valid schema, {'a' => 'tt'}
      assert_valid schema, {'a' => ''}
      refute_valid schema, {'a' => 'ttt'}

      # other types are disregarded
      assert_valid schema, {'a' => 5}
    end

    def test_pattern
      schema = {
        'properties' => {
          'a' => { 'pattern' => "\\d+ taco" }
        }
      }

      assert_valid schema, {'a' => '156 taco bell'}
      refute_valid schema, {'a' => 'x taco'}

      # other types are disregarded
      assert_valid schema, {'a' => 5}
    end
  end

  module FormatTests
    # Draft1..3 use the format name `ip-address`; draft4 changed it to `ipv4`.
    def ipv4_format
      'ip-address'
    end

    def test_format_unknown
      schema = {
        'properties' => {
          'a' => { 'format' => 'unknown' }
        }
      }

      assert_valid schema, {'a' => 'absolutely anything!'}
      assert_valid schema, {'a' => ''}
    end

    def test_format_union
      schema = {
        'properties' => {
          'a' => {
            'type'   => ['string', 'null'],
            'format' => 'date-time'
          }
        }
      }

      assert_valid schema, {'a' => nil}
      refute_valid schema, {'a' => 'wrong'}
    end

    def test_format_ipv4
      schema = {
        'properties' => {
          'a' => { 'format' => ipv4_format }
        }
      }

      assert_valid schema, {"a" => "1.1.1.1"}
      refute_valid schema, {"a" => "1.1.1"}
      refute_valid schema, {"a" => "1.1.1.300"}
      refute_valid schema, {"a" => "1.1.1"}
      refute_valid schema, {"a" => "1.1.1.1b"}

      # other types are disregarded
      assert_valid schema, {'a' => 5}
    end

    def test_format_ipv6
      schema = {
        'properties' => {
          'a' => { 'format' => 'ipv6' }
        }
      }

      assert_valid schema, {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff"}
      assert_valid schema, {"a" => "1111:0:8888:0:0:0:eeee:ffff"}
      assert_valid schema, {"a" => "1111:2222:8888::eeee:ffff"}
      assert_valid schema, {"a" => "::1"}

      refute_valid schema, {"a" => "1111:2222:8888:99999:aaaa:cccc:eeee:ffff"}
      refute_valid schema, {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:gggg"}
      refute_valid schema, {"a" => "1111:2222::9999::cccc:eeee:ffff"}
      refute_valid schema, {"a" => "1111:2222:8888:9999:aaaa:cccc:eeee:ffff:bbbb"}
      refute_valid schema, {"a" => "42"}
      refute_valid schema, {"a" => "b"}
    end
  end

  # Draft1..3 explicitly support `date`, `time` formats in addition to
  # the `date-time` format.
  module DateAndTimeFormatTests
    def test_format_time
      schema = {
        'properties' => {
          'a' => { 'format' => 'time' }
        }
      }

      assert_valid schema, {"a" => "12:00:00"}
      refute_valid schema, {"a" => "12:00"}
      refute_valid schema, {"a" => "12:00:60"}
      refute_valid schema, {"a" => "12:60:00"}
      refute_valid schema, {"a" => "24:00:00"}
      refute_valid schema, {"a" => "0:00:00"}
      refute_valid schema, {"a" => "-12:00:00"}
      refute_valid schema, {"a" => "12:00:00b"}
      assert_valid schema, {"a" => "12:00:00"}
      refute_valid schema, {"a" => "12:00:00\nabc"}
    end

    def test_format_date
      schema = {
        'properties' => {
          'a' => { 'format' => 'date' }
        }
      }

      assert_valid schema, {"a" => "2010-01-01"}
      refute_valid schema, {"a" => "2010-01-32"}
      refute_valid schema, {"a" => "n2010-01-01"}
      refute_valid schema, {"a" => "2010-1-01"}
      refute_valid schema, {"a" => "2010-01-1"}
      refute_valid schema, {"a" => "2010-01-01n"}
      refute_valid schema, {"a" => "2010-01-01\nabc"}
    end
  end
end