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

Any way to get meta by SchemaPointer? #73

Closed
gigi opened this issue Apr 16, 2019 · 5 comments
Closed

Any way to get meta by SchemaPointer? #73

gigi opened this issue Apr 16, 2019 · 5 comments

Comments

@gigi
Copy link

gigi commented Apr 16, 2019

Hi!

Is it possible to get metadata of field by InvalidValue exception?
For example I want to know if failed field marked as required in the schema.

 try {
    $schema = Schema::import($schemaFile);         
    $schema->in((object)$data);
} catch (Exception $exception) {
    // something like this JsonPointer::get((object)$schema, $exception->getSchemaPointer())->required;        
    // or
    // $meta = $schema->getByPointer(Schema$exception->getSchemaPointer())->required;
}

Thanx!

vearutop added a commit that referenced this issue Apr 17, 2019
@vearutop
Copy link
Member

@gigi please see the attached commit for failed schema resolution example:

$failedSchemaData = JsonPointer::getByPointer($schemaData, $exception->getSchemaPointer());

Hope it is helpful, error inspection is also discussed in #55 and json-schema-org/json-schema-spec#643. With JSON Schema draft-08 out error inspection in this library will likely need some revamping.

@gigi
Copy link
Author

gigi commented Apr 17, 2019

Not working at all.

$file = __DIR__ . '/../../../vendor/development-contract/mylib/user-profile.json';

$str = '{"user":{"id":"111","is_dev":true}, ... other data}'; // my data

$schema = Schema::import($file);

try {
    $schema->in((object)json_decode($str));
} catch (Exception $e) {
    $data = json_decode(file_get_contents($file));
    $pointer = $e->getSchemaPointer(); // /srv/www/namespace/module/test/Unit/Validator/../../..//vendor/development-contract/mylib/user-server-event.json/allOf/1/properties/field_data/properties/email
    // Different errors depending on exception:
    // Swaggest\JsonDiff\Exception : Key not found: srv
    // Swaggest\JsonDiff\Exception : Path must start with "/": ./user-event.json if there is ref to another schema
    // etc
    $meta = JsonPointer::getByPointer($data, $pointer);
}

I have to make a lot of string manipulations in order to get cause field
smth like this

private function isRequired($schema, $schemaPointer, $dataPointer)
{
        $path = str_replace($schema->getFromRefs()[0], '', $schemaPointer);
        $pathItems = array_slice(JsonPointer::splitPath($path), 0);
        array_splice($pathItems, -2);
        $pathItems[] = 'required';
        // FIXME to much json operations
        $items = JsonPointer::get(json_decode(json_encode($schema), true), $pathItems);
        $lastSegment = substr($dataPointer, strrpos($dataPointer, '/') + 1);

        return in_array($lastSegment, $items, true);
 }

@vearutop
Copy link
Member

vearutop commented Apr 24, 2019

@gigi please check v0.12.9. There is now InvalidValue->getFailedSubSchema method to resolve exception into Schema instance.

https://github.com/swaggest/php-json-schema/blob/v0.12.9/tests/src/PHPUnit/Error/ErrorTest.php#L193

@jpwijbenga
Copy link

Comment about getFailedSubSchema

image

Line 89 can override the schemapointer based on ref resolution, but line 98 does not use that value, calls the getter again? Is that correct?

@jpwijbenga
Copy link

Reason I'm asking is I'm looking for a way of getting a Sub-Schema from only 2 parts: root Schema object and DocumentPath string, specifically one that works with https $ref resolution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants