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

Moving constraints min- and maxProperties to ObjectConstraint #232

Merged
merged 1 commit into from
Feb 17, 2016
Merged
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
25 changes: 25 additions & 0 deletions src/JsonSchema/Constraints/ObjectConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,14 @@ public function validatePatternProperties($element, $path, $patternProperties)
*/
public function validateElement($element, $matches, $objectDefinition = null, $path = null, $additionalProp = null)
{
$this->validateMinMaxConstraint($element, $objectDefinition, $path);
foreach ($element as $i => $value) {

$property = $this->getProperty($element, $i, new UndefinedConstraint());
$definition = $this->getProperty($objectDefinition, $i);

$this->validateMinMaxConstraint(!($property instanceof UndefinedConstraint) ? $property : $element, $definition, $path);

// no additional properties allowed
if (!in_array($i, $matches) && $additionalProp === false && $this->inlineSchemaProperty !== $i && !$definition) {
$this->addError($path, "The property " . $i . " is not defined and the definition does not allow additional properties", 'additionalProp');
Expand Down Expand Up @@ -146,4 +149,26 @@ protected function getProperty($element, $property, $fallback = null)

return $fallback;
}

/**
* validating minimum and maximum property constraints (if present) against an element
*
* @param \stdClass $element Element to validate
* @param \stdClass $objectDefinition ObjectConstraint definition
* @param string $path Path to test?
*/
protected function validateMinMaxConstraint($element, $objectDefinition, $path) {
// Verify minimum number of properties
if (isset($objectDefinition->minProperties)) {
if (count(get_object_vars($element)) < $objectDefinition->minProperties) {
$this->addError($path, "Must contain a minimum of " . $objectDefinition->minProperties . " properties", 'minProperties', array('minProperties' => $objectDefinition->minProperties,));
}
}
// Verify maximum number of properties
if (isset($objectDefinition->maxProperties)) {
if (count(get_object_vars($element)) > $objectDefinition->maxProperties) {
$this->addError($path, "Must contain no more than " . $objectDefinition->maxProperties . " properties", 'maxProperties', array('maxProperties' => $objectDefinition->maxProperties,));
}
}
}
}
27 changes: 3 additions & 24 deletions src/JsonSchema/Constraints/UndefinedConstraint.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,10 @@ class UndefinedConstraint extends Constraint
*/
public function check($value, $schema = null, $path = null, $i = null)
{
if (is_null($schema)) {
if (is_null($schema) || !is_object($schema)) {
return;
}

if (!is_object($schema)) {
throw new InvalidArgumentException(
'Given schema must be an object in ' . $path
. ' but is a ' . gettype($schema)
);
}

$i = is_null($i) ? "" : $i;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can leave this in to maintain better BC

$path = $this->incrementPath($path, $i);

Expand Down Expand Up @@ -65,10 +58,10 @@ public function validateTypes($value, $schema = null, $path = null, $i = null)
}

// check object
if (is_object($value) && (isset($schema->properties) || isset($schema->patternProperties) || isset($schema->additionalProperties))) {
if (is_object($value)) {
$this->checkObject(
$value,
isset($schema->properties) ? $schema->properties : null,
isset($schema->properties) ? $schema->properties : $schema,
$path,
isset($schema->additionalProperties) ? $schema->additionalProperties : null,
isset($schema->patternProperties) ? $schema->patternProperties : null
Expand Down Expand Up @@ -165,20 +158,6 @@ protected function validateCommonProperties($value, $schema = null, $path = null
}
}

// Verify minimum and maximum number of properties
if (is_object($value)) {
if (isset($schema->minProperties)) {
if (count(get_object_vars($value)) < $schema->minProperties) {
$this->addError($path, "Must contain a minimum of " . $schema->minProperties . " properties", 'minProperties', array('minProperties' => $schema->minProperties,));
}
}
if (isset($schema->maxProperties)) {
if (count(get_object_vars($value)) > $schema->maxProperties) {
$this->addError($path, "Must contain no more than " . $schema->maxProperties . " properties", 'maxProperties', array('maxProperties' => $schema->maxProperties,));
}
}
}

// Verify that dependencies are met
if (is_object($value) && isset($schema->dependencies)) {
$this->validateDependencies($value, $schema->dependencies, $path);
Expand Down