示例#1
0
文件: Validator.php 项目: stefk/jval
 /**
  * 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();
 }
示例#2
0
 /**
  * {@inheritdoc}
  */
 public function normalize(stdClass $schema, Context $context, Walker $walker)
 {
     $context->enterNode('dependencies');
     if (!is_object($schema->dependencies)) {
         throw new InvalidTypeException($context, Types::TYPE_OBJECT);
     }
     foreach ($schema->dependencies as $property => $value) {
         $context->enterNode($property);
         if (is_object($value)) {
             $walker->parseSchema($value, $context);
         } elseif (is_array($value)) {
             if (0 === ($propertyCount = count($value))) {
                 throw new EmptyArrayException($context);
             }
             foreach ($value as $index => $subProperty) {
                 if (!is_string($subProperty)) {
                     $context->enterNode($index);
                     throw new InvalidTypeException($context, Types::TYPE_STRING);
                 }
             }
             if (count(array_unique($value)) !== $propertyCount) {
                 throw new NotUniqueException($context);
             }
         } else {
             throw new InvalidTypeException($context, [Types::TYPE_OBJECT, Types::TYPE_ARRAY]);
         }
         $context->leaveNode();
     }
     $context->leaveNode();
 }
示例#3
0
 /**
  * {@inheritdoc}
  */
 public function normalize(stdClass $schema, Context $context, Walker $walker)
 {
     $context->enterNode('not');
     if (!is_object($schema->not)) {
         throw new InvalidTypeException($context, Types::TYPE_OBJECT);
     }
     $walker->parseSchema($schema->not, $context);
     $context->leaveNode();
 }
示例#4
0
 /**
  * {@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();
 }
示例#5
0
 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]);
     }
 }
示例#6
0
 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();
     }
 }