/** * @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; }