Skip to content

Error Schema must be an object or a boolean, NULL given in #25

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

Closed
PavelKoll opened this issue Mar 20, 2019 · 6 comments
Closed

Error Schema must be an object or a boolean, NULL given in #25

PavelKoll opened this issue Mar 20, 2019 · 6 comments

Comments

@PavelKoll
Copy link

Hi, I have this script for validation
index.php

<?php
use Opis\JsonSchema\{Validator, ValidationResult, ValidationError, Schema};
require 'vendor/autoload.php';
$loader = new \Opis\JsonSchema\Loaders\File("http://test/", [
    "",
]);
$data = json_decode('{
  "title": "TEST",
  "code": "TWOR"
}');
$validator = new Validator();
$validator->setLoader($loader);
/** @var ValidationResult $result */
$result = $validator->uriValidation($data, "http://test/cTest.json");
if ($result->isValid()) {
    echo '$data is valid', PHP_EOL;
} else {
    /** @var ValidationError $error */
    var_dump($result->getErrors());
    $error = $result->getFirstError();
    echo '$data is invalid', PHP_EOL;
    echo "Error: ", $error->keyword(), PHP_EOL; 
    echo json_encode($error->keywordArgs(), JSON_PRETTY_PRINT), PHP_EOL;
}

this JSON schema cTest.json

{
	"$schema": "http://json-schema.org/draft-07/schema#",
	"type": "object",
	"properties": { 
		"title": {
			"$ref": "http://test/title.json#"
		},
		"code": {
			"$ref": "http://test/title.json#"
		}
	},
	"required": ["title", "code"],
    "additionalProperties": false
} 

and this title.json

{
  	"$id": "title.json",
  	"type": "string",
	"minLength": 2,
	"pattern": "^(.*)$"
}

These 3 files are in the same folder.
Script shows this error:
Fatal error: Uncaught Opis\JsonSchema\Exception\InvalidSchemaException: Schema must be an object or a boolean, NULL given in /Data/Projects/test/vendor/opis/json-schema/src/Schema.php:63
Stack trace:
#0 /Data/Projects/test/vendor/opis/json-schema/src/Loaders/File.php(63): Opis\JsonSchema\Schema->__construct(NULL, 'http://test/titl...')
#1 /Data/Projects/test/vendor/opis/json-schema/src/Validator.php(419): Opis\JsonSchema\Loaders\File->loadSchema('http://test/titl...')
#2 /Data/Projects/test/vendor/opis/json-schema/src/Validator.php(322): Opis\JsonSchema\Validator->validateRef(Object(stdClass), 'TEST workcenter', Array, Array, Object(Opis\JsonSchema\Schema), NULL, Object(Opis\JsonSchema\ValidationResult))
#3 /Data/Projects/test/vendor/opis/json-schema/src/Validator.php(1908): Opis\JsonSchema\Validator->validateSchema(Object(stdClass), 'TEST workcenter', Array, Array, Object(Opis\JsonSchema\Schema), Object(stdClass), Object(Opis\JsonSchema\ValidationResult))
#4 /Data/Projects/test/vendor/opis/json-schema/src/Validator.php(946): Opi in /Data/Projects/test/vendor/opis/json-schema/src/Schema.php on line 63

What am I doing wrong?
Thank you.

@sorinsarca
Copy link
Member

Hello, try to modify the files like so:

cTest.json

  • $id must be present at the root and must be an URI (absolute)
  • $refs can use relative URIs (this is not mandatory, but will make the schema more readable)
{
  "$id": "http://test/cTest.json#",
  "$schema": "http://json-schema.org/draft-07/schema#",

  "type": "object",
  "properties": { 
    "title": {
      "$ref": "title.json"
    },
    "code": {
      "$ref": "title.json"
    }
  },
  "required": ["title", "code"],
  "additionalProperties": false
} 

title.json

  • $id must be present at the root and must be an URI (absolute)
{
  "$id": "http://test/title.json#",

  "type": "string",
  "minLength": 2,
  "pattern": "^(.*)$"
}

And this should be enough.

@sorinsarca
Copy link
Member

Well, I cannot reproduce the issue even with your original files. Are you sure you are using the latest version of opis/json-schema?

@PavelKoll
Copy link
Author

Yes, I am using the latest version. I installed it last week

@sorinsarca
Copy link
Member

@PavelKoll I cannot reproduce your issue.
Here are the steps I tried:

cd ~
mkdir issue-25
cd issue-25

composer require opis/json-schema

touch index.php
# put the index.php content with nano or similar
touch cTest.json
# put the cTest.json content with nano or similar
touch title.json
# put the title.json content with nano or similar

php index.php
# The output is: $data is valid

@PavelKoll
Copy link
Author

I'll try to install it again

@PavelKoll
Copy link
Author

I don't know where the error was, but I installed the opis/json-schema again and everything works

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

2 participants