/** * {@inheritdoc} */ public function apply($instance, stdClass $schema, Context $context, Walker $walker) { $accumulatingContext = $context->duplicate(false); $hasMatch = false; $hasDoubleMatch = false; foreach ($schema->oneOf as $subSchema) { $subContext = $context->duplicate(false); $walker->applyConstraints($instance, $subSchema, $subContext); if ($subContext->countViolations() === 0) { if (!$hasMatch) { $hasMatch = true; } else { $hasDoubleMatch = true; break; } } else { $accumulatingContext->mergeViolations($subContext); } } if (!$hasMatch) { $context->mergeViolations($accumulatingContext); } if (!$hasMatch || $hasDoubleMatch) { $context->addViolation('instance must match exactly one of the schemas listed in oneOf'); } }
/** * {@inheritdoc} */ public function apply($instance, stdClass $schema, Context $context, Walker $walker) { $altContext = $context->duplicate(); $walker->applyConstraints($instance, $schema->not, $altContext); if ($altContext->countViolations() === $context->countViolations()) { $context->addViolation('should not match schema in "not"'); } }
/** * Validates an instance against a given schema and returns a list * of violations, if any. If the schema contains relative remote * references, its (absolute) URI must be passed as argument. * * @param mixed $instance * @param stdClass $schema * @param string $schemaUri * * @return array */ public function validate($instance, stdClass $schema, $schemaUri = '') { $parseContext = new Context(); $constraintContext = new Context(); // todo: keep ref of already resolved/parsed schemas $schema = $this->walker->resolveReferences($schema, new Uri($schemaUri)); $schema = $this->walker->parseSchema($schema, $parseContext); $this->walker->applyConstraints($instance, $schema, $constraintContext); return $constraintContext->getViolations(); }
/** * {@inheritdoc} */ public function apply($instance, stdClass $schema, Context $context, Walker $walker) { $originalCount = $context->countViolations(); foreach ($schema->allOf as $subSchema) { $walker->applyConstraints($instance, $subSchema, $context); } if ($context->countViolations() > $originalCount) { $context->addViolation('instance must match all the schemas listed in allOf'); } }
/** * {@inheritdoc} */ public function apply($instance, stdClass $schema, Context $context, Walker $walker) { $accumulatingContext = $context->duplicate(); $hasMatch = false; foreach ($schema->anyOf as $subSchema) { $originalCount = $accumulatingContext->countViolations(); $walker->applyConstraints($instance, $subSchema, $accumulatingContext); if ($accumulatingContext->countViolations() === $originalCount) { $hasMatch = true; break; } } if (!$hasMatch) { $context->mergeViolations($accumulatingContext); $context->addViolation('instance must match at least one of the schemas listed in anyOf'); } }
/** * {@inheritdoc} */ public function apply($instance, stdClass $schema, Context $context, Walker $walker) { foreach ($schema->dependencies as $property => $value) { if (property_exists($instance, $property)) { if (is_object($value)) { // Schema dependencies (see §5.4.5.2.1) $walker->applyConstraints($instance, $value, $context); } else { // Property dependencies (see §5.4.5.2.2) foreach ($value as $propertyDependency) { if (!property_exists($instance, $propertyDependency)) { $context->addViolation('dependency property "%s" is missing', [$propertyDependency]); } } } } } }
/** * {@inheritdoc} */ public function normalize(stdClass $schema, Context $context, Walker $walker) { $keyword = $this->keywords()[0]; $context->enterNode($keyword); if (!is_array($schema->{$keyword})) { throw new InvalidTypeException($context, Types::TYPE_ARRAY); } if (count($schema->{$keyword}) === 0) { throw new EmptyArrayException($context); } foreach ($schema->{$keyword} as $index => $subSchema) { $context->enterNode($index); if (!is_object($subSchema)) { throw new InvalidTypeException($context, Types::TYPE_OBJECT); } $walker->parseSchema($subSchema, $context); $context->leaveNode(); } $context->leaveNode(); }
private function parseAdditionalItemsProperty(stdClass $schema, Context $context, Walker $walker) { if (is_object($schema->additionalItems)) { $walker->parseSchema($schema->additionalItems, $context); } elseif (!is_bool($schema->additionalItems)) { throw new InvalidTypeException($context, [Types::TYPE_OBJECT, Types::TYPE_BOOLEAN]); } }
private function parsePatternPropertiesProperty(stdClass $schema, Context $context, Walker $walker) { if (!is_object($schema->patternProperties)) { throw new InvalidTypeException($context, Types::TYPE_OBJECT); } foreach ($schema->patternProperties as $regex => $value) { $context->enterNode($regex); if (!Utils::isValidRegex($regex)) { throw new InvalidRegexException($context); } if (!is_object($value)) { throw new InvalidTypeException($context, Types::TYPE_OBJECT); } $walker->parseSchema($value, $context); $context->leaveNode(); } }