Skip to content

Commit 0845256

Browse files
committed
fix regression braking required in v3 schemas
1 parent 304f3e7 commit 0845256

7 files changed

Lines changed: 200 additions & 1 deletion

File tree

src/JsonSchema/Constraints/Undefined.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ protected function validateCommonProperties($value, $schema = null, $path = null
109109
if (isset($schema->required) && $schema->required) {
110110
$this->addError($path, "is missing and it is required");
111111
}
112-
} else if (isset($schema->required)) {
112+
} else if (isset($schema->required) && is_array($schema->required)) {
113113
// Draft 4 - Required is an array of strings - e.g. "required": ["foo", ...]
114114
foreach ($schema->required as $required) {
115115
if (!property_exists($value, $required)) {

tests/JsonSchema/ValidatorTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/**
4+
* integration test for JsonSchema\Validator
5+
*
6+
* run tests against the example schema hierarchy in /tests/fixtures/
7+
*
8+
* This file is part of the JsonSchema package.
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace JsonSchema;
15+
16+
class ValidatorTest extends \PHPUnit_Framework_TestCase
17+
{
18+
function fixturePath($fileName) {
19+
return dirname(__DIR__) .'/fixtures/'. $fileName .'.json';
20+
}
21+
function fixture($fileName) {
22+
return json_decode(file_get_contents($this->fixturePath($fileName)));
23+
}
24+
25+
function setUp(){
26+
$resolver = new RefResolver();
27+
$this->schemaUri = 'file://'. $this->fixturePath('Employee');
28+
$retriever = new Uri\UriRetriever;
29+
$this->schema = $retriever->retrieve($this->schemaUri);
30+
31+
$refResolver = new RefResolver($retriever);
32+
$refResolver->resolve($this->schema, $this->schemaUri);
33+
34+
$this->data = $this->fixture('employee_instance');
35+
}
36+
37+
function assertValid($data) {
38+
$validator = new Validator();
39+
$validator->check($data, $this->schema);
40+
$this->assertEquals(array(), $validator->getErrors(), print_r($validator->getErrors(), true));
41+
$this->assertTrue($validator->isValid());
42+
}
43+
44+
function assertNotValid($data, $errors) {
45+
$validator = new Validator();
46+
$validator->check($data, $this->schema);
47+
$this->assertEquals($errors, $validator->getErrors(), print_r($validator->getErrors(), true));
48+
$this->assertFalse($validator->isValid());
49+
}
50+
51+
function testResolvedSchema() {
52+
$expectedResolvedSchema = $this->fixture('resolved_Employee_schema');
53+
$expectedResolvedSchema->id = $this->schemaUri;
54+
$this->assertEquals($expectedResolvedSchema, $this->schema, 'expected resolved schema');
55+
}
56+
57+
function testEmployeeShouldBeValidate() {
58+
$this->assertValid($this->data);
59+
}
60+
61+
function testEmployeeMissingOfficeAddress() {
62+
unset($this->data->person->office_address);
63+
64+
$this->assertNotValid(
65+
$this->data
66+
,array(array(
67+
'property' => 'person.office_address'
68+
,'message' => 'is missing and it is required'
69+
))
70+
);
71+
}
72+
}

tests/fixtures/Employee.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"description": "Employee schema inherits from Person.json; Uses custom types from types.json",
3+
"extends": "Person.json",
4+
"properties": {
5+
"person": {
6+
"type":"object",
7+
"additionalProperties":false,
8+
"properties": {
9+
"type" :{
10+
"enum": ["Employee"]
11+
},
12+
"department": {
13+
"type": "string",
14+
"minLength": 1,
15+
"maxLength": 255
16+
},
17+
"office_address": {
18+
"extends" : "types.json#/postal_address",
19+
"required": true
20+
}
21+
}
22+
}
23+
}
24+
}

tests/fixtures/Person.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"description" : "Person schema is abstract base schema for Employee and others; Uses custom types from types.json",
3+
"type":"object",
4+
"additionalProperties":false,
5+
"$schema": "http://json-schema.org/draft-03/schema",
6+
"properties": {
7+
"person": {
8+
"type":"object",
9+
"required":true,
10+
"properties": {
11+
"id": {
12+
"extends": "types.json#/id_type",
13+
"required": true
14+
},
15+
"type": {
16+
"type": "string",
17+
"required": true
18+
}
19+
}
20+
}
21+
}
22+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"person": {
3+
"id": 123456
4+
,"type": "Employee"
5+
,"department": "Business Intelligence"
6+
,"office_address": {
7+
"street":"Main Avenue 3",
8+
"city": "Cubertino"
9+
}
10+
}
11+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"description" : "Employee schema inherits from Person.json; Uses custom types from types.json",
3+
"type":"object",
4+
"$schema": "http://json-schema.org/draft-03/schema",
5+
"id": "< should be the same absolute URI as Employee.json. Set it manually in your test! >",
6+
"additionalProperties":false,
7+
"properties": {
8+
"person": {
9+
"type":"object",
10+
"additionalProperties":false,
11+
"required":true,
12+
"properties": {
13+
"id": {
14+
"description" : "ID",
15+
"type" : "integer",
16+
"minimum": 1,
17+
"exclusiveMinimum": false,
18+
"required": true
19+
},
20+
"type" :{
21+
"type": "string",
22+
"required": true,
23+
"enum": ["Employee"]
24+
},
25+
"department": {
26+
"type": "string",
27+
"minLength": 1,
28+
"maxLength": 255
29+
},
30+
"office_address": {
31+
"type": "object",
32+
"required": true,
33+
"properties": {
34+
"street": {
35+
"type":"string",
36+
"required": true
37+
},
38+
"city" : {
39+
"type":"string",
40+
"required": true
41+
}
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}

tests/fixtures/types.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"description" : "custom type definitions",
3+
"type":"object",
4+
"id_type": {
5+
"description" : "ID",
6+
"type" : "integer",
7+
"minimum": 1,
8+
"exclusiveMinimum": false
9+
},
10+
"postal_address" : {
11+
"type": "object",
12+
"properties": {
13+
"street": {
14+
"type":"string",
15+
"required": true
16+
},
17+
"city" : {
18+
"type":"string",
19+
"required": true
20+
}
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)