File: PointerTest.php

package info (click to toggle)
php-json-schema 6.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,248 kB
  • sloc: php: 9,638; makefile: 153; python: 28; sh: 13
file content (137 lines) | stat: -rw-r--r-- 4,545 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
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
<?php

declare(strict_types=1);

namespace JsonSchema\Tests\Constraints;

use JsonSchema\Validator;
use PHPUnit\Framework\TestCase;

class PointerTest extends TestCase
{
    protected $validateSchema = true;

    public function testVariousPointers(): void
    {
        $schema = [
            'type' => 'object',
            'required' => ['prop1', 'prop2', 'prop3', 'prop4'],
            'properties' => [
                'prop1' => [
                    'type' => 'string'
                ],
                'prop2' => [
                    'type' => 'object',
                    'required' => ['prop2.1'],
                    'properties' => [
                        'prop2.1' => [
                            'type' => 'string'
                        ]
                    ]
                ],
                'prop3' => [
                    'type' => 'object',
                    'required' => ['prop3/1'],
                    'properties' => [
                        'prop3/1' => [
                            'type' => 'object',
                            'required' => ['prop3/1.1'],
                            'properties' => [
                                'prop3/1.1' => [
                                    'type' => 'string'
                                ]
                            ]
                        ]
                    ]
                ],
                'prop4' => [
                    'type' => 'array',
                    'minItems' => 1,
                    'items' => [
                        'type' => 'object',
                        'required' => ['prop4-child'],
                        'properties' => [
                            'prop4-child' => [
                                'type' => 'string'
                            ]
                        ]
                    ]
                ]
            ]
        ];

        $value = [
            'prop2' => [
                'foo' => 'bar'
            ],
            'prop3' => [
                'prop3/1' => [
                    'foo' => 'bar'
                ]
            ],
            'prop4' => [
                [
                    'foo' => 'bar'
                ]
            ]
        ];

        $validator = new Validator();
        $checkValue = json_decode(json_encode($value));
        $validator->validate($checkValue, json_decode(json_encode($schema)));

        $this->assertEquals(
            [
                [
                    'property' => 'prop1',
                    'pointer' => '/prop1',
                    'message' => 'The property prop1 is required',
                    'constraint' => [
                        'name' => 'required',
                        'params' => [
                            'property' => 'prop1'
                        ]
                    ],
                    'context'    => Validator::ERROR_DOCUMENT_VALIDATION
                ],
                [
                    'property' => 'prop2.prop2.1',
                    'pointer' => '/prop2/prop2.1',
                    'message' => 'The property prop2.1 is required',
                    'constraint' => [
                        'name' => 'required',
                        'params' => [
                            'property' => 'prop2.1'
                        ]
                    ],
                    'context'    => Validator::ERROR_DOCUMENT_VALIDATION
                ],
                [
                    'property' => 'prop3.prop3/1.prop3/1.1',
                    'pointer' => '/prop3/prop3~11/prop3~11.1',
                    'message' => 'The property prop3/1.1 is required',
                    'constraint' => [
                        'name' => 'required',
                        'params' => [
                            'property' => 'prop3/1.1'
                        ]
                    ],
                    'context'    => Validator::ERROR_DOCUMENT_VALIDATION
                ],
                [
                    'property' => 'prop4[0].prop4-child',
                    'pointer' => '/prop4/0/prop4-child',
                    'message' => 'The property prop4-child is required',
                    'constraint' => [
                        'name' => 'required',
                        'params' => [
                            'property' => 'prop4-child'
                        ]
                    ],
                    'context'    => Validator::ERROR_DOCUMENT_VALIDATION
                ]
            ],
            $validator->getErrors()
        );
    }
}