Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need help: how to get detailed data about failed validation? #153

Closed
jokaorgua opened this issue Feb 9, 2022 · 11 comments
Closed

Need help: how to get detailed data about failed validation? #153

jokaorgua opened this issue Feb 9, 2022 · 11 comments
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@jokaorgua
Copy link

Hello. Could anyone recommend or explain how to extract detailed validation errors? Which field, error type, etc?

Thanks.

I'm using such a code in symfony event listener

$validator = (new \League\OpenAPIValidation\PSR7\ValidatorBuilder)->fromYamlFile($openapiFilePath)
                          ->getRequestValidator();
$validator->validate($psrRequest);

p.s. I've looked through the code and I do not see any public method to get such an info

@davidyell
Copy link

👍
This would sure be a handy thing to have

@scaytrase
Copy link
Member

Validation failure is expose through exception tree, it's documented in the readme. Basic exceptions are ValidationFailed for common PSR-7 validator process. You can inspect exception chain to get exact reason.
Some of the exceptions have programmatically accessible data, like breadcrumbs https://github.com/thephpleague/openapi-psr7-validator/blob/master/src/Schema/Exception/SchemaMismatch.php#L13

@scaytrase scaytrase added help wanted Extra attention is needed question Further information is requested labels Feb 10, 2022
@jokaorgua
Copy link
Author

@scaytrase thanks for these details. I've seen them. But as for this is very inconvenient to make a new method that will get the exact reason from the failed chain. Moreover, each exception has a different public interface for getting the fail details.

Don't you think that will be much better to have something like

interface FailExceptionInterface{
    public function getCode();
    public function getMessage();
    public function getSomethingElse();
}

after that, we could use it on any kind of failed exception to get some details like keyword, reason, field etc

@davidyell
Copy link

How does this \League\OpenAPIValidation\PSR7\Exception\Validation\InvalidBody tell me where to look in my json body?

@davidyell
Copy link

Apologies, but yes, dumping the whole exception it does indeed include way more helpful information! 🎉

@jokaorgua
Copy link
Author

jokaorgua commented Feb 10, 2022

@davidyell when you will understand how to get the field name from KeywordMismatch exception please let me know

@jokaorgua
Copy link
Author

@scaytrase I would be very appreciative if you could explain how to get protected compoundIndex from BreadCrumb object without using any kind of reflection
or any other way how to get field name which value does not correspond to openapi specification

@scaytrase
Copy link
Member

You have dedicated method buildChain which actually gives you an ordered array of breadcrumbs, pointing you to place in data where the fail is

@canvural
Copy link
Contributor

@jokaorgua I'm using this code. I don't know your use case, but probably you can adjust it to your needs

private function getValidationMessage(ValidationFailed $exception): string
    {
        $ex = $exception->getPrevious();

        if ($ex === null) {
            return $exception->getMessage();
        }

        if ($ex instanceof SchemaMismatch) {
            if ($ex->dataBreadCrumb() === null) {
                return $exception->getMessage();
            }

            $field = implode('.', $ex->dataBreadCrumb()->buildChain());

            return sprintf(
                'Validation against spec failed for "%s": "%s"',
                $field,
                str_replace(['Keyword validation failed: '], '', $ex->getMessage())
            );
        }

        return $exception->getMessage();
    }

@jokaorgua
Copy link
Author

@scaytrase thanks. But as for me, the interface should be created for getting errors normally without hacks

@canvural thanks!

@scaytrase
Copy link
Member

Interpreting validation errors is pure userland responsibility. Every project can have it's own implementation and it's own usecase (some use psr-7 validator, some validate raw data, etc). And there is nothing hacky in using fields exampled in the readme (yeah, maybe not that explicitly but anyway)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants