當前位置: 首頁>>代碼示例>>PHP>>正文


PHP JsonSchema\Validator類代碼示例

本文整理匯總了PHP中JsonSchema\Validator的典型用法代碼示例。如果您正苦於以下問題:PHP Validator類的具體用法?PHP Validator怎麽用?PHP Validator使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Validator類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: getValidator

 /**
  * @param \stdClass $responseBody
  *
  * @return Validator
  */
 protected function getValidator($responseBody)
 {
     $responseSchema = $this->schemaManager->getResponseSchema($this->path, $this->httpMethod, $this->httpCode);
     $validator = new Validator();
     $validator->check($responseBody, $responseSchema);
     return $validator;
 }
開發者ID:Beanhunter,項目名稱:SwaggerAssertions,代碼行數:12,代碼來源:ResponseBodyConstraint.php

示例2: doValidation

 protected function doValidation(Response $response)
 {
     $data = json_decode($response->getBody());
     if ($data === null) {
         throw new ValidationFailedException("The given JSON data can not be validated (last error: '" . $this->json_errors[json_last_error()] . "').");
     } else {
         $error = false;
         $messageParts = array();
         foreach ($this->jsonSchemaFiles as $jsonSchemaFile) {
             $factory = new Factory(null, null, Constraint::CHECK_MODE_TYPE_CAST | Constraint::CHECK_MODE_COERCE);
             $validator = new Validator($factory);
             $jsonSchemaObject = (object) json_decode(file_get_contents($jsonSchemaFile['jsonschemafileurl']));
             $validator->check($data, $jsonSchemaObject);
             if (!$validator->isValid()) {
                 $error = true;
                 $errorMessage = '';
                 foreach ($validator->getErrors() as $error) {
                     $errorMessage = $errorMessage . sprintf("[%s] %s\n", $error['property'], $error['message']);
                 }
                 $messageParts[] = $jsonSchemaFile['jsonschemafilename'] . ' - ' . $jsonSchemaFile['jsonschemafileurl'] . '(last error: ' . $errorMessage . ').';
             }
         }
         if ($error == true) {
             $message = 'JSON file (' . (string) $response->getUri() . ')  does not validate against the following JSON Schema files: ' . implode(", ", $messageParts);
             throw new ValidationFailedException($message);
         }
     }
 }
開發者ID:phmlabs,項目名稱:smoke,代碼行數:28,代碼來源:JsonSchemaRule.php

示例3: checkEntityAgainstSchema

 /**
  * Check data against a given entity type from the schema
  *
  * @param array|object $data Data from the API. Associative arrays will be reparsed into objects
  * @param string $entity Entity type, from the schema
  */
 protected function checkEntityAgainstSchema($data, $entity)
 {
     $absolute_ref = self::SCHEMA_BASE . 'definitions/' . $entity;
     $schema = $this->retriever->retrieve($absolute_ref);
     if (is_array($data)) {
         // Data was decoded as an array instead of an object, reencode for
         // schema checking
         $data = json_decode(json_encode($data));
     }
     $validator = new Validator(Validator::CHECK_MODE_NORMAL, $this->retriever);
     $validator->check($data, $schema);
     if (!$validator->isValid()) {
         $message = "JSON does not validate against schema:\n";
         $i = 0;
         foreach ($validator->getErrors() as $error) {
             $i++;
             $message .= $i . ') ';
             if (!empty($error['property'])) {
                 $message .= sprintf("[%s] %s\n", $error['property'], $error['message']);
             } else {
                 $message .= $error['message'] . "\n";
             }
         }
         $this->fail($message);
     }
 }
開發者ID:NikDevPHP,項目名稱:client-php,代碼行數:32,代碼來源:Server.php

示例4: execute

 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $path = $input->getArgument('path');
     if (!$path) {
         $path = getcwd() . DIRECTORY_SEPARATOR . 'resolver.json';
     }
     if (!is_file($path)) {
         $output->writeln(sprintf('<error>The file "%s" does not exists.</error>', $path));
         return 1;
     }
     // Get the schema and data as objects:
     $schema = json_decode(file_get_contents(__DIR__ . '/../../resources/schema.json'));
     $data = json_decode(file_get_contents($path));
     // Validate:
     $validator = new Validator();
     $validator->check($data, $schema);
     if ($validator->isValid()) {
         $output->writeln($path . ' is valid');
         return 0;
     }
     $output->writeln(sprintf('<error>The file "%s" contains errors.</error>', $path));
     foreach ($validator->getErrors() as $error) {
         if ($error['property']) {
             $output->writeln(sprintf('- %s: %s', $error['property'], $error['message']));
         } else {
             $output->writeln(sprintf('- %s', $error['message']));
         }
     }
     return 1;
 }
開發者ID:pixelpolishers,項目名稱:resolver,代碼行數:30,代碼來源:Validate.php

示例5: assertJsonResponse

 /**
  * @param string $schemaName
  */
 protected function assertJsonResponse($response, $statusCode = Response::HTTP_OK, $schemaName = null)
 {
     // Assert HTTP response status code
     $this->assertEquals($statusCode, $response->getStatusCode(), $response->getContent());
     $content = $response->getContent();
     $data = null;
     if ($content) {
         // Assert response is JSON content-type (unless response content is empty)
         $this->assertTrue($response->headers->contains('Content-Type', 'application/json'), $response->headers);
         // Parse the response body
         $data = json_decode($response->getContent());
     }
     // Validate JSON data with given schema
     if (null !== $schemaName) {
         $schemaUri = 'file://' . realpath(__DIR__ . '/../Resources/json_schemas/' . $schemaName . '.json');
         $retriever = new JsonSchema\Uri\UriRetriever();
         $schema = $retriever->retrieve($schemaUri);
         $validator = new JsonSchema\Validator();
         $validator->check($data, $schema);
         if (!$validator->isValid()) {
             $errorMessage = 'JSON response does not validate the schema';
             foreach ($validator->getErrors() as $error) {
                 $errorMessage .= sprintf("\n[%s] %s", $error['property'], $error['message']);
             }
             $this->fail($errorMessage);
         }
     }
     return $data;
 }
開發者ID:andreaswarnaar,項目名稱:openl10n,代碼行數:32,代碼來源:WebTestCase.php

示例6: parseSingleSchema

 public function parseSingleSchema($schemeSpec)
 {
     if ($schemeSpec === null) {
         throw new ParseException('scheme must not be empty');
     }
     if (!is_string($schemeSpec)) {
         throw new ParseException('scheme must be a string');
     }
     $schemaJson = json_decode($schemeSpec);
     if (json_last_error() == JSON_ERROR_NONE) {
         $schemeUri = $schemaJson->{'$schema'};
         $retriever = new \JsonSchema\Uri\UriRetriever();
         $scheme = $retriever->retrieve($schemeUri);
         $validator = new Validator();
         $validator->check($schemaJson, $scheme);
         if ($validator->isValid()) {
             $arrayScheme = json_decode($schemeSpec, true);
             unset($arrayScheme['$schema']);
             return $arrayScheme;
         } else {
             foreach ($validator->getErrors() as $error) {
                 echo sprintf("[%s] %s\n", $error['property'], $error['message']);
             }
         }
     }
 }
開發者ID:limenius,項目名稱:arambla,代碼行數:26,代碼來源:SchemasParser.php

示例7: additionalFailureDescription

 /**
  * @inheritdoc
  */
 protected function additionalFailureDescription($other)
 {
     $other = $this->forceToObject($other);
     $validator = new Validator();
     $validator->check($other, $this->schema);
     return implode("\n", array_map(function ($error) {
         return $error['message'];
     }, $validator->getErrors()));
 }
開發者ID:martin-helmich,項目名稱:phpunit-json-assert,代碼行數:12,代碼來源:JsonValueMatchesSchema.php

示例8: initValidator

 /**
  * @param $jsonStr
  * @return Validator
  */
 private static function initValidator($jsonStr)
 {
     /** @var $this JsonValidatableTrait */
     $validator = new Validator();
     $data = json_decode($jsonStr);
     $schema = static::jsonViceVersa(static::getJsonSchema());
     $validator->check($data, $schema);
     return $validator;
 }
開發者ID:akentner,項目名稱:incoming-ftp,代碼行數:13,代碼來源:JsonValidatableTrait.php

示例9: testValidCases

 /**
  * @dataProvider getValidTests
  */
 public function testValidCases($input, $schema, $checkMode = Validator::CHECK_MODE_NORMAL)
 {
     $schema = json_decode($schema);
     $refResolver = new RefResolver(new UriRetriever());
     $refResolver->resolve($schema);
     $validator = new Validator($checkMode);
     $validator->check(json_decode($input), $schema);
     $this->assertTrue($validator->isValid(), print_r($validator->getErrors(), true));
 }
開發者ID:Flesh192,項目名稱:magento,代碼行數:12,代碼來源:BaseTestCase.php

示例10: GetTestFiles

 public static function GetTestFiles()
 {
     $dir = dirname(dirname(__DIR__));
     $retriever = new UriRetriever();
     $schema = $retriever->retrieve('file://' . realpath($dir . '/schema/tests.json'));
     return new CallbackFilterIterator(new DirectoryIterator($dir . '/tests'), function (SplFileInfo $file) use($schema) {
         $validator = new Validator();
         return $file->isFile() && $file->isReadable() && preg_match('/\\.json$/', $file->getBasename()) && $validator->check($schema, json_decode(file_get_contents($file->getRealPath()))) == null && $validator->isValid();
     });
 }
開發者ID:SignpostMarv,項目名稱:Verbal-Expressions-Tests,代碼行數:10,代碼來源:DynamicTestGenerator.php

示例11: validateJsonObject

 /**
  * Validates a json object
  *
  * @param string $json
  *
  * @throws \Exception
  *
  * @return boolean
  */
 public function validateJsonObject($json)
 {
     $validator = new Validator();
     $jsonSchema = $this->json;
     $validator->check($json, $jsonSchema);
     if (!$validator->isValid()) {
         throw new InvalidSchemaException($validator->getErrors());
     }
     return true;
 }
開發者ID:TheKnarf,項目名稱:php-raml-parser,代碼行數:19,代碼來源:JsonSchemaDefinition.php

示例12: check

 private function check($json)
 {
     $schema = json_decode(file_get_contents(__DIR__ . '/../../../../res/composer-schema.json'));
     $validator = new Validator();
     $validator->check(json_decode($json), $schema);
     if (!$validator->isValid()) {
         return $validator->getErrors();
     }
     return true;
 }
開發者ID:Flesh192,項目名稱:magento,代碼行數:10,代碼來源:ComposerSchemaTest.php

示例13: validateSchema

 public function validateSchema($json, $schema)
 {
     $validator = new Validator();
     $validator->check(json_decode($json), json_decode($schema));
     if ($validator->isValid()) {
         return true;
     } else {
         return $validator->getErrors();
     }
 }
開發者ID:takeit,項目名稱:updater,代碼行數:10,代碼來源:JsonManager.php

示例14: check

 /**
  * Zkontroluje data proti zadanému schématu
  *
  * @param array|\StdClass
  * @param string
  * @return bool
  */
 public function check($data, $schemaFile)
 {
     $validator = new Validator();
     $schema = $this->getSchema($schemaFile);
     $validator->check($data, Json::decode($schema));
     if ($validator->isValid()) {
         return TRUE;
     }
     $this->errors = $validator->getErrors();
     return FALSE;
 }
開發者ID:gorocacher,項目名稱:RestApiBase,代碼行數:18,代碼來源:JsonSchemaValidator.php

示例15: getValidator

 /**
  * @param \stdClass $headers
  *
  * @return Validator
  */
 protected function getValidator($headers)
 {
     $schema = new \stdClass();
     $headers = (object) array_change_key_case((array) $headers, CASE_LOWER);
     $properties = $this->schemaManager->getResponseHeaders($this->path, $this->httpMethod, $this->httpCode);
     $schema->properties = (object) array_change_key_case((array) $properties, CASE_LOWER);
     $schema->required = array_keys((array) $schema->properties);
     $validator = new Validator();
     $validator->check($headers, $schema);
     return $validator;
 }
開發者ID:Beanhunter,項目名稱:SwaggerAssertions,代碼行數:16,代碼來源:ResponseHeadersConstraint.php


注:本文中的JsonSchema\Validator類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。