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

JsonSchema\Exception\ResourceNotFoundException #674

Closed
msarocha opened this issue Sep 29, 2021 · 1 comment
Closed

JsonSchema\Exception\ResourceNotFoundException #674

msarocha opened this issue Sep 29, 2021 · 1 comment

Comments

@msarocha
Copy link

Hi!
I use this code (inline scheme)...
`$jsonSchema = <<<'JSON'
{
"type": "object",
"properties": {
"CodigoEmpresa": { "type": "string"},
"Localidade": {"type": "string"}
}
},
"required": ["CodigoEmpresa","Localidade"],
}
JSON;

    $jsonSchemaObject = json_decode($jsonSchema);
    $schemaStorage = new SchemaStorage();
    $schemaStorage->addSchema('file://mySchema', $jsonSchemaObject);
    $jsonValidator = new Validator(new Factory($schemaStorage));

    // JSON must be decoded before it can be validated
    $jsonToValidateObject = json_decode('{"data":123}');

    // Do validation (use isValid() and getErrors() to check the result)
    $jsonValidator->validate($jsonToValidateObject, $jsonSchemaObject);
    if ($jsonValidator->isValid()) {
        echo "The supplied JSON validates against the schema.\n";
    } else {
        echo "JSON does not validate. Violations:\n";
        foreach ($jsonValidator->getErrors() as $error) {
            printf("[%s] %s\n", $error['property'], $error['message']);
        }
    }
    die();`

But i have this error...
JsonSchema\Exception\ResourceNotFoundException
file_get_contents(file://mySchema): failed to open stream: no suitable wrapper could be found

What is wrong?
thanks!

@DannyvdSluijs
Copy link
Collaborator

@msarocha you're lading the schema with a file:// prefix but are providing an inline schema. This is why your code is throwing an exception while trying to call file_get_contents().

The fixed code (including some boiler plate to run it from the library root and a fix of the schema to be valid JSON) is shown below. In an attempt to cleanup this repo we are trying to filter the issues and see which ones might be closed. Is it safe to assume with this answer it can be closed? Feel free to close it yourself with some comments if helpful.

Fixed code

<?php

use JsonSchema\Constraints\Factory;
use JsonSchema\SchemaStorage;
use JsonSchema\Validator;

require_once './vendor/autoload.php';

$jsonSchema = <<<'JSON'
{
  "type": "object",
  "properties": {
    "CodigoEmpresa": { "type": "string"},
    "Localidade": {"type": "string"}
  },
  "required": ["CodigoEmpresa","Localidade"]
}
JSON;

$jsonSchemaObject = json_decode($jsonSchema);
$schemaStorage = new SchemaStorage();
$schemaStorage->addSchema('internal://mySchema', $jsonSchemaObject);
$jsonValidator = new Validator(new Factory($schemaStorage));

$jsonToValidateObject = json_decode('{"data":123}');
$jsonValidator->validate($jsonToValidateObject, $jsonSchemaObject);
if ($jsonValidator->isValid()) {
    echo "The supplied JSON validates against the schema.\n";
} else {
    echo "JSON does not validate. Violations:\n";
    foreach ($jsonValidator->getErrors() as $error) {
        printf("[%s] %s\n", $error['property'], $error['message']);
    }
}

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