public function __invoke($schema, OutputInterface $output) { $schema = Util::loadJson($schema); $metaSchema = (new Dereferencer())->dereference('file://' . Util::schemaPath('draft4.json')); $validator = new Validator($schema, $metaSchema); if ($validator->passes()) { $output->writeln('<info>✓ Valid draft-04 JSON Schema</info>'); } else { $output->writeln('<error>✗ Invalid draft-04 JSON Schema</error>'); Util::renderErrorTable($output, $validator->errors()); } }
public function __invoke($data, $schema, OutputInterface $output) { $schema = Util::loadJson($schema); $data = Util::loadJson($data); $schema = (new Dereferencer())->dereference($schema); $validator = new Validator($data, $schema); if ($validator->passes()) { $output->writeln('<info>✓ Validation passed</info>'); } else { $output->writeln('<error>✗ Validation failed</error>'); Util::renderErrorTable($output, $validator->errors()); } }
/** * Validation sugar to use on a flatMap * Validate against schemas of the constructors * @return \Closure */ public function validate() { return function ($payload) { // validate foreach ($this->schemas as $schema) { $validator = new Validator($payload, $schema); foreach ($this->formatExtensions as $format => $class) { $validator->registerFormatExtension($format, new $class()); } if ($validator->fails()) { foreach ($validator->errors() as $error) { /* @var ValidationError $error */ print_r($error->toArray()); } } } }; }
/** * @param string $value * @param string $schema * * @throws \RREST\Exception\InvalidJSONException * @throws \RREST\Exception\InvalidResponsePayloadBodyException */ public function assertResponseJSON($value, $schema) { $assertInvalidJSONException = function () { if (json_last_error() !== JSON_ERROR_NONE) { throw new InvalidJSONException([new Error(ucfirst(json_last_error_msg()), 'invalid-response-payloadbody-json')]); } }; //validate JSON format $schemaJSON = json_decode($schema); $assertInvalidJSONException(); //validate JsonSchema $deref = new JsonGuard\Dereferencer(); $schema = $deref->dereference($schemaJSON); $validator = new JsonGuard\Validator($value, $schema); if ($validator->fails()) { $errors = $validator->errors(); $jsonPointer = new JsonGuard\Pointer($value); $invalidBodyError = []; foreach ($validator->errors() as $jsonError) { $error = $jsonError->toArray(); $propertyValue = null; try { $propertyValue = $jsonPointer->get($error['pointer']); } catch (NonexistentValueReferencedException $e) { //don't care if we can't have the value here, it's just //for the context } $context = new \stdClass(); $context->jsonPointer = $error['pointer']; $context->value = $propertyValue; $context->constraints = $error['constraints']; $invalidBodyError[] = new Error(strtolower($error['pointer'] . ': ' . $error['message']), strtolower($error['code']), $context); } if (empty($invalidBodyError) == false) { throw new InvalidResponsePayloadBodyException($invalidBodyError); } } }
/** * @param string $value * @param string $schema * * @throws \RREST\Exception\InvalidJSONException * @throws \RREST\Exception\InvalidRequestPayloadBodyException */ protected function assertHTTPPayloadBodyJSON($value, $schema) { $assertInvalidJSONException = function () { if (json_last_error() !== JSON_ERROR_NONE) { throw new InvalidJSONException([new Error(ucfirst(json_last_error_msg()), 'invalid-request-payloadbody-json')]); } }; //validate JSON format $valueJSON = json_decode($value); $assertInvalidJSONException(); $schemaJSON = json_decode($schema); $assertInvalidJSONException(); //validate JsonSchema $deref = new JsonGuard\Dereferencer(); $schema = $deref->dereference($schemaJSON); $validator = new JsonGuard\Validator($valueJSON, $schema); if ($validator->fails()) { $errors = $validator->errors(); $jsonPointer = new JsonGuard\Pointer($value); $invalidBodyError = []; foreach ($validator->errors() as $jsonError) { $error = $jsonError->toArray(); $propertyValue = null; $pointer = empty($error['pointer']) ? null : $error['pointer']; if (empty($pointer) === false) { try { $propertyValue = $jsonPointer->get($pointer); } catch (NonexistentValueReferencedException $e) { //don't care if we can't have the value here, it's just //for the context } } $context = new \stdClass(); $context->jsonPointer = $pointer; $context->value = $propertyValue; $context->constraints = $error['constraints']; //$context->jsonSource = $valueJSON; $message = strtolower($error['message']); if (empty($pointer) === false) { $message = strtolower($pointer . ': ' . $error['message']); } //TODO: having RREST message error //special case for required to have a better message if ($error['code'] === JsonGuard\ErrorCode::MISSING_REQUIRED) { $message = strtolower($error['message'] . ' (' . implode(', ', $context->constraints['required']) . ')'); } $invalidBodyError[] = new Error(strtolower($message), strtolower($error['code']), $context); } if (empty($invalidBodyError) == false) { throw new InvalidRequestPayloadBodyException($invalidBodyError); } } $this->hintedPayloadBody = $valueJSON; }