Skip to content
Closed
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
39 changes: 35 additions & 4 deletions src/Json/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function validate($entity, $entityName = null)
protected function validateProperties($entity, $schema, $entityName)
{
$properties = get_object_vars($entity);
$checkedProperties = array();

if (!isset($schema->properties)) {
throw new SchemaException(sprintf('No properties defined for [%s]', $entityName));
Expand All @@ -83,6 +84,8 @@ protected function validateProperties($entity, $schema, $entityName)
// Check type
$path = $entityName . '.' . $propertyName;
$this->validateType($entity->{$propertyName}, $property, $path);
// add this property to the list of checked properties
array_push($checkedProperties, $propertyName);
} else {
// Check required
if (isset($property->required) && $property->required) {
Expand All @@ -91,11 +94,39 @@ protected function validateProperties($entity, $schema, $entityName)
}
}

// check properties whose keys match pattern
foreach($schema->patternProperties as $pattern => $property) {
// walk through each of the properties to see if it matches this pattern
foreach ($properties as $propertyName) {
if (preg_match($pattern, $propertyName)) {
// Check type
$path = $entityName . '.' . $propertyName;
$this->validateType($entity->{$propertyName}, $property, $path);
// add this property to the list of checked properties
array_push($checkedProperties, $propertyName);
} else {
// Check required
if (isset($property->required) && $property->required) {
throw new ValidationException(sprintf('Missing required patternProperty [%s] for [%s]', $propertyName, $entityName));
}
}
}
}

// Check additional properties
if (isset($schema->additionalProperties) && !$schema->additionalProperties) {
$extra = array_diff(array_keys((array)$entity), array_keys((array)$schema->properties));
if (count($extra)) {
throw new ValidationException(sprintf('Additional properties [%s] not allowed for property [%s]', implode(',', $extra), $entityName));
if (isset($schema->additionalProperties)) {
// are there any extra that haven't been checked?
$extra = array_diff($properties, $checkedProperties);
if (!$schema->additionalProperties) {
if (count($extra)) {
throw new ValidationException(sprintf('Additional properties [%s] not allowed for property [%s]', implode(',', $extra), $entityName));
}
} else if (is_object($schema->additionalProperties)) {
// validate each extra property against the additionalProperties schema
foreach ($extra as $propertyName) {
$path = $entityName . '.' . $propertyName;
$this->validateType($entity->{$propertyName}, $property, $path);
}
}
}

Expand Down