Skip to content
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

Fix php warning, docblocks, and peephole optimizations #659

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
38 changes: 17 additions & 21 deletions src/JsonSchema/Constraints/TypeConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ public function check(&$value = null, $schema = null, JsonPointer $path = null,
* of $isValid to true, if at least one $type mateches the type of $value or the value
* passed as $isValid is already true.
*
* @param mixed $value Value to validate
* @param array $type TypeConstraints to check against
* @param array $validTypesWording An array of wordings of the valid types of the array $type
* @param bool $isValid The current validation value
* @param $path
* @param mixed $value Value to validate
* @param array $type TypeConstraints to check against
* @param array $validTypesWording An array of wordings of the valid types of the array $type
* @param bool $isValid The current validation value
* @param JsonPointer|null $path
*/
protected function validateTypesArray(&$value, array $type, &$validTypesWording, &$isValid, $path, $coerce = false)
{
Expand All @@ -98,21 +98,17 @@ protected function validateTypesArray(&$value, array $type, &$validTypesWording,
// $tp can be an object, if it's a schema instead of a simple type, validate it
// with a new type constraint
if (is_object($tp)) {
if (!$isValid) {
$validator = $this->factory->createInstanceFor('type');
$subSchema = new \stdClass();
$subSchema->type = $tp;
$validator->check($value, $subSchema, $path, null);
$error = $validator->getErrors();
$isValid = !(bool) $error;
$validTypesWording[] = self::$wording['object'];
}
$validator = $this->factory->createInstanceFor('type');
$subSchema = new \stdClass();
$subSchema->type = $tp;
$validator->check($value, $subSchema, $path, null);
$error = $validator->getErrors();
$isValid = !(bool) $error;
$validTypesWording[] = self::$wording['object'];
} else {
$this->validateTypeNameWording($tp);
$validTypesWording[] = self::$wording[$tp];
if (!$isValid) {
$isValid = $this->validateType($value, $tp, $coerce);
}
$isValid = $this->validateType($value, $tp, $coerce);
}
}
}
Expand All @@ -122,9 +118,9 @@ protected function validateTypesArray(&$value, array $type, &$validTypesWording,
* difference, that, if $listEnd isn't false, the last element delimiter is $listEnd instead of
* $delimiter.
*
* @param array $elements The elements to implode
* @param string $delimiter The delimiter to use
* @param bool $listEnd The last delimiter to use (defaults to $delimiter)
* @param array $elements The elements to implode
* @param string $delimiter The delimiter to use
* @param bool|string $listEnd The last delimiter to use (defaults to $delimiter)
*
* @return string
*/
Expand Down Expand Up @@ -239,7 +235,7 @@ protected function validateType(&$value, $type, $coerce = false)
/**
* Converts a value to boolean. For example, "true" becomes true.
*
* @param $value The value to convert to boolean
* @param mixed $value The value to convert to boolean
*
* @return bool|mixed
*/
Expand Down
2 changes: 1 addition & 1 deletion src/JsonSchema/Constraints/UndefinedConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ protected function validateCommonProperties(&$value, $schema, JsonPointer $path,
if (!$this->getTypeCheck()->propertyExists($value, $required)) {
$this->addError(
ConstraintError::REQUIRED(),
$this->incrementPath($path ?: new JsonPointer(''), $required), array(
$this->incrementPath($path, $required), array(
'property' => $required
)
);
Expand Down
24 changes: 19 additions & 5 deletions src/JsonSchema/SchemaStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,25 @@ public function addSchema($id, $schema = null)
// workaround for bug in draft-03 & draft-04 meta-schemas (id & $ref defined with incorrect format)
// see https://github.com/json-schema-org/JSON-Schema-Test-Suite/issues/177#issuecomment-293051367
if (is_object($schema) && property_exists($schema, 'id')) {
if ($schema->id == 'http://json-schema.org/draft-04/schema#') {
$schema->properties->id->format = 'uri-reference';
} elseif ($schema->id == 'http://json-schema.org/draft-03/schema#') {
$schema->properties->id->format = 'uri-reference';
$schema->properties->{'$ref'}->format = 'uri-reference';
$draft03 = false;
if (
$schema->id === 'http://json-schema.org/draft-04/schema#' ||
($schema->id === 'http://json-schema.org/draft-03/schema#' && $draft03 = true)
) {
if (!property_exists($schema, 'properties')) {
$schema->properties = new \stdClass;
}
if (!property_exists($schema->properties, 'id')) {
$schema->properties->id = new \stdClass;
}
$schema->properties->id->format = 'uri-reference';

if ($draft03) {
if (!property_exists($schema->properties, '$ref')) {
$schema->properties->{'$ref'} = new \stdClass;
}
$schema->properties->{'$ref'}->format = 'uri-reference';
}
}
}

Expand Down
10 changes: 10 additions & 0 deletions tests/SchemaStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,14 @@ public function testNoDoubleResolve()
$schemas['test/schema']->{'$ref'}
);
}

public function testAddSchemaMissingProperties()
{
$schema_03 = array('id' => 'http://json-schema.org/draft-03/schema#');
$schema_04 = array('id' => 'http://json-schema.org/draft-04/schema#');

$s = new SchemaStorage();
$s->addSchema($schema_03['id'], $schema_03);
$s->addSchema($schema_04['id'], $schema_04);
}
}