Skip to content

Backports for 5.2.1 #413

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 16, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"json-schema/JSON-Schema-Test-Suite": "1.2.0",
"phpunit/phpunit": "^4.8.22",
"friendsofphp/php-cs-fixer": "^2.1",
"phpdocumentor/phpdocumentor": "~2"
"phpdocumentor/phpdocumentor": "^2.7"
},
"autoload": {
"psr-4": { "JsonSchema\\": "src/JsonSchema/" }
Expand Down
8 changes: 5 additions & 3 deletions src/JsonSchema/Constraints/Constraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,15 @@ protected function checkArray(&$value, $schema = null, JsonPointer $path = null,
* @param mixed $value
* @param mixed $schema
* @param JsonPointer|null $path
* @param mixed $i
* @param mixed $properties
* @param mixed $additionalProperties
* @param mixed $patternProperties
*/
protected function checkObject(&$value, $schema = null, JsonPointer $path = null, $i = null, $patternProperties = null, $appliedDefaults = array())
protected function checkObject(&$value, $schema = null, JsonPointer $path = null, $properties = null,
$additionalProperties = null, $patternProperties = null, $appliedDefaults = array())
{
$validator = $this->factory->createInstanceFor('object');
$validator->check($value, $schema, $path, $i, $patternProperties, $appliedDefaults);
$validator->check($value, $schema, $path, $properties, $additionalProperties, $patternProperties, $appliedDefaults);

$this->addErrors($validator->getErrors());
}
Expand Down
44 changes: 24 additions & 20 deletions src/JsonSchema/Constraints/ObjectConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class ObjectConstraint extends Constraint
/**
* {@inheritdoc}
*/
public function check(&$element, $definition = null, JsonPointer $path = null, $additionalProp = null, $patternProperties = null, $appliedDefaults = array())
public function check(&$element, $schema = null, JsonPointer $path = null, $properties = null,
$additionalProp = null, $patternProperties = null, $appliedDefaults = array())
{
if ($element instanceof UndefinedConstraint) {
return;
Expand All @@ -37,16 +38,17 @@ public function check(&$element, $definition = null, JsonPointer $path = null, $

$matches = array();
if ($patternProperties) {
// validate the element pattern properties
$matches = $this->validatePatternProperties($element, $path, $patternProperties);
}

if ($definition) {
// validate the definition properties
$this->validateDefinition($element, $definition, $path);
if ($properties) {
// validate the element properties
$this->validateProperties($element, $properties, $path);
}

// additional the element properties
$this->validateElement($element, $matches, $definition, $path, $additionalProp);
// validate additional element properties & constraints
$this->validateElement($element, $matches, $schema, $path, $properties, $additionalProp);
}

public function validatePatternProperties($element, JsonPointer $path = null, $patternProperties)
Expand Down Expand Up @@ -81,18 +83,20 @@ public function validatePatternProperties($element, JsonPointer $path = null, $p
/**
* Validates the element properties
*
* @param \stdClass $element Element to validate
* @param array $matches Matches from patternProperties (if any)
* @param \stdClass $objectDefinition ObjectConstraint definition
* @param JsonPointer|null $path Path to test?
* @param mixed $additionalProp Additional properties
* @param \StdClass $element Element to validate
* @param array $matches Matches from patternProperties (if any)
* @param \StdClass $schema ObjectConstraint definition
* @param JsonPointer|null $path Current test path
* @param \StdClass $properties Properties
* @param mixed $additionalProp Additional properties
*/
public function validateElement($element, $matches, $objectDefinition = null, JsonPointer $path = null, $additionalProp = null)
public function validateElement($element, $matches, $schema = null, JsonPointer $path = null,
$properties = null, $additionalProp = null)
{
$this->validateMinMaxConstraint($element, $objectDefinition, $path);
$this->validateMinMaxConstraint($element, $schema, $path);

foreach ($element as $i => $value) {
$definition = $this->getProperty($objectDefinition, $i);
$definition = $this->getProperty($properties, $i);

// no additional properties allowed
if (!in_array($i, $matches) && $additionalProp === false && $this->inlineSchemaProperty !== $i && !$definition) {
Expand Down Expand Up @@ -124,17 +128,17 @@ public function validateElement($element, $matches, $objectDefinition = null, Js
/**
* Validates the definition properties
*
* @param \stdClass $element Element to validate
* @param \stdClass $objectDefinition ObjectConstraint definition
* @param JsonPointer|null $path Path?
* @param \stdClass $element Element to validate
* @param \stdClass $properties Property definitions
* @param JsonPointer|null $path Path?
*/
public function validateDefinition(&$element, $objectDefinition = null, JsonPointer $path = null)
public function validateProperties(&$element, $properties = null, JsonPointer $path = null)
{
$undefinedConstraint = $this->factory->createInstanceFor('undefined');

foreach ($objectDefinition as $i => $value) {
foreach ($properties as $i => $value) {
$property = &$this->getProperty($element, $i, $undefinedConstraint);
$definition = $this->getProperty($objectDefinition, $i);
$definition = $this->getProperty($properties, $i);

if (is_object($definition)) {
// Undefined constraint will check for is_object() and quit if is not - so why pass it?
Expand Down
3 changes: 2 additions & 1 deletion src/JsonSchema/Constraints/UndefinedConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ public function validateTypes(&$value, $schema = null, JsonPointer $path, $i = n
// is not set (i.e. don't use $this->getTypeCheck() here).
$this->checkObject(
$value,
isset($schema->properties) ? $this->factory->getSchemaStorage()->resolveRefSchema($schema->properties) : $schema,
$schema,
$path,
isset($schema->properties) ? $schema->properties : null,
isset($schema->additionalProperties) ? $schema->additionalProperties : null,
isset($schema->patternProperties) ? $schema->patternProperties : null,
$this->appliedDefaults
Expand Down
6 changes: 3 additions & 3 deletions src/JsonSchema/Rfc3339.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class Rfc3339
{
const REGEX = '/^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})(\.\d+)?(Z|([+-]\d{2}):?(\d{2}))$/';
const REGEX = '/^(\d{4}-\d{2}-\d{2}[T ]{1}\d{2}:\d{2}:\d{2})(\.\d+)?(Z|([+-]\d{2}):?(\d{2}))$/';

/**
* Try creating a DateTime instance
Expand All @@ -22,8 +22,8 @@ public static function createFromString($string)
$dateAndTime = $matches[1];
$microseconds = $matches[2] ?: '.000000';
$timeZone = 'Z' !== $matches[3] ? $matches[4] . ':' . $matches[5] : '+00:00';

$dateTime = \DateTime::createFromFormat('Y-m-d\TH:i:s.uP', $dateAndTime . $microseconds . $timeZone, new \DateTimeZone('UTC'));
$dateFormat = strpos($dateAndTime, 'T') === false ? 'Y-m-d H:i:s.uP' : 'Y-m-d\TH:i:s.uP';
$dateTime = \DateTime::createFromFormat($dateFormat, $dateAndTime . $microseconds . $timeZone, new \DateTimeZone('UTC'));

return $dateTime ?: null;
}
Expand Down
22 changes: 20 additions & 2 deletions tests/Constraints/MinMaxPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function getInvalidTests()
return array(
array(
'{
"value": 1
"value": {}
}',
'{
"type": "object",
Expand All @@ -83,9 +83,27 @@ public function getInvalidTests()
}
}'
),
array(
'{}',
'{
"type": "object",
"properties": {
"propertyOne": {
"type": "string"
},
"propertyTwo": {
"type": "string"
}
},
"minProperties": 1
}'
),
array(
'{
"value": 1
"value": {
"propertyOne": "valueOne",
"propertyTwo": "valueTwo"
}
}',
'{
"type": "object",
Expand Down
20 changes: 18 additions & 2 deletions tests/Rfc3339Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ public function provideValidFormats()
'2000-05-01T12:12:12Z',
\DateTime::createFromFormat('Y-m-d\TH:i:s', '2000-05-01T12:12:12', new \DateTimeZone('UTC'))
),
array('2000-05-01T12:12:12+0100', \DateTime::createFromFormat('Y-m-d\TH:i:sP', '2000-05-01T12:12:12+01:00')),
array('2000-05-01T12:12:12+01:00', \DateTime::createFromFormat('Y-m-d\TH:i:sP', '2000-05-01T12:12:12+01:00')),
array(
'2000-05-01T12:12:12+0100',
\DateTime::createFromFormat('Y-m-d\TH:i:sP', '2000-05-01T12:12:12+01:00')
),
array(
'2000-05-01T12:12:12+01:00',
\DateTime::createFromFormat('Y-m-d\TH:i:sP', '2000-05-01T12:12:12+01:00')
),
array(
'2000-05-01T12:12:12.123456Z',
\DateTime::createFromFormat('Y-m-d\TH:i:s.u', '2000-05-01T12:12:12.123456', new \DateTimeZone('UTC'))
Expand All @@ -45,6 +51,14 @@ public function provideValidFormats()
'2000-05-01T12:12:12.123Z',
\DateTime::createFromFormat('Y-m-d\TH:i:s.u', '2000-05-01T12:12:12.123000', new \DateTimeZone('UTC'))
),
array(
'2000-05-01 12:12:12.123Z',
\DateTime::createFromFormat('Y-m-d H:i:s.u', '2000-05-01 12:12:12.123000', new \DateTimeZone('UTC'))
),
array(
'2000-05-01 12:12:12.123456Z',
\DateTime::createFromFormat('Y-m-d H:i:s.u', '2000-05-01 12:12:12.123456', new \DateTimeZone('UTC'))
)
);
}

Expand All @@ -54,6 +68,8 @@ public function provideInvalidFormats()
array('1999-1-11T00:00:00Z'),
array('1999-01-11T00:00:00+100'),
array('1999-01-11T00:00:00+1:00'),
array('1999-01-01 00:00:00Z'),
array('1999-1-11 00:00:00Z')
);
}
}