Skip to content

Commit

Permalink
Merge pull request #591 from erayd/backport-529
Browse files Browse the repository at this point in the history
## Backported PRs
 * #559 ArraysTest for array items with enum validation
 * #567 Don't run checks which assume a defined instance against undefined
 * #575 Tests on PHP 7.3
 * #587 Fixed PHPDoc of Validator::validate() method
 * #583 Fix travis PHP 5.4 and 5.5 config

## Additional PRs (5.x.x only)
These PRs are only applicable to the 5.x.x branch, and have been merged individually.
 * #589 Update validate-json to use spl_autoload_register

## Skipped PRs
 * #464 marc-mabe/php-enum versions (dependency not present in 5.x.x)
  • Loading branch information
erayd authored Sep 25, 2019
2 parents e8b7614 + 5bc9862 commit 44c6787
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ $config
'phpdoc_order' => true,
'phpdoc_summary' => false,
'pre_increment' => false,
'increment_style' => false,
'simplified_null_return' => false,
'trailing_comma_in_multiline_array' => false,
'yoda_style' => false,
'phpdoc_types_order' => array('null_adjustment' => 'none', 'sort_algorithm' => 'none'),
))
->setFinder($finder)
;
Expand Down
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ matrix:
- php: 5.3
dist: precise
- php: 5.4
dist: trusty
- php: 5.5
dist: trusty
- php: 5.6
- php: 7.0
env: WITH_COVERAGE=true WITH_PHPCSFIXER=true
env: WITH_COVERAGE=true
- php: 7.0
env: WITH_PHPCSFIXER=true
- php: 7.1
- php: 7.2
- php: 7.3
- php: 'nightly'
- php: hhvm
dist: trusty
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"php": ">=5.3.3"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "~2.2.20",
"friendsofphp/php-cs-fixer": "~2.2.20||~2.15.1",
"json-schema/JSON-Schema-Test-Suite": "1.2.0",
"phpunit/phpunit": "^4.8.35"
},
Expand Down
6 changes: 6 additions & 0 deletions src/JsonSchema/Constraints/UndefinedConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ protected function validateCommonProperties(&$value, $schema = null, JsonPointer
'required'
);
}
} else {
// If the value is both undefined and not required, skip remaining checks
// in this method which assume an actual, defined instance when validating.
if ($value instanceof self) {
return;
}
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/JsonSchema/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ class Validator extends BaseConstraint
* Both the php object and the schema are supposed to be a result of a json_decode call.
* The validation works as defined by the schema proposal in http://json-schema.org.
*
* Note that the first argument is passwd by reference, so you must pass in a variable.
*
* {@inheritdoc}
* Note that the first argument is passed by reference, so you must pass in a variable.
*/
public function validate(&$value, $schema = null, $checkMode = null)
{
Expand Down
90 changes: 90 additions & 0 deletions tests/Constraints/ArraysTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,51 @@ public function getInvalidTests()
}
}
}'
),
array( // Test array items.enum where type string fail validation if value(s) is/are not in items.enum
'{"data": ["a", "b"]}',
'{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "string",
"enum": ["b", "c"]
}
}
}
}'
),
array( // Test array items.enum where type integer fail validation if value(s) is/are not in items.enum
'{"data": [1, 2]}',
'{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "integer",
"enum": [2, 3]
}
}
}
}'
),
array( // Test array items.enum where type number fail validation if value(s) is/are not in items.enum
'{"data": [1.25, 2.25]}',
'{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "number",
"enum": [1.25, 2]
}
}
}
}'
)
);
}
Expand Down Expand Up @@ -168,6 +213,51 @@ public function getValidTests()
}
}
}'
),
array( // Test array items.enum where type string passes validation if value(s) is/are in items.enum
'{"data": ["c", "c", "b"]}',
'{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "string",
"enum": ["b", "c"]
}
}
}
}'
),
array( // Test array items.enum where type integer passes validation if value(s) is/are in items.enum
'{"data": [1, 1, 2]}',
'{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "integer",
"enum": [1, 2]
}
}
}
}'
),
array( // Test array items.enum where type number passes validation if value(s) is/are in items.enum
'{"data": [1.25, 1.25, 2.25]}',
'{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "number",
"enum": [1.25, 2.25]
}
}
}
}'
)
);
}
Expand Down
27 changes: 27 additions & 0 deletions tests/Constraints/NotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ public function getInvalidTests()
}
}
}'
),
array( // check that a missing, required property is correctly validated
'{"y": "foo"}',
'{
"type": "object",
"required": ["x"],
"properties": {
"x": {
"not": {
"type": "null"
}
}
}
}'
)
);
}
Expand Down Expand Up @@ -69,6 +83,19 @@ public function getValidTests()
}
}
}'
),
array( // check that a missing, non-required property isn't validated
'{"y": "foo"}',
'{
"type": "object",
"properties": {
"x": {
"not": {
"type": "null"
}
}
}
}'
)
);
}
Expand Down

0 comments on commit 44c6787

Please sign in to comment.