Skip to content

TEST IF IMPROVEMENT #13

Description

@github-actions

$ct = $request->getHeaderLine('Content-Type');

if (!preg_match('#^application/json\b#i', $ct)) {

throw new \Exception('Content-Type must be application/json.', 400);

}

// TODO: TEST IF IMPROVEMENT

    public function getValidatedRequestBody(Request $request, Response $response, $schema = false): object|array
    {
        if (($request->getHeader('Content-Type')[0] ?? '') != 'application/json') { throw new \Exception('Content-Type header missing or not set to `application/json`.', 400); };

        /*
        // TODO: TEST IF IMPROVEMENT
        $ct = $request->getHeaderLine('Content-Type');
        if (!preg_match('#^application/json\b#i', $ct)) {
            throw new \Exception('Content-Type must be application/json.', 400);
        }
        */

        try {
            $raw = (string) $request->getBody();
            $doc = json_decode($raw === '' ? 'null' : $raw, /* assoc */ false, 512, JSON_THROW_ON_ERROR);
        } catch (\JsonException $e) {
            throw new \Exception('Invalid JSON: '.$e->getMessage(), 400);
        }

        // Only arrays or objects are accepted as top-level JSON
        if (!is_array($doc) && !is_object($doc)) {
            throw new \Exception('JSON root must be an object or array.', 400);
        }
        //$doc = json_decode(json_encode($request->getParsedBody()));
        if ($schema !== false) {
            $validation = $this->validator->validate((object) $doc, $schema);
            if ($validation->isValid()) { $res = $doc; }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions