Skip to content

anyOf Warning #441

Closed
Closed
@allentsai

Description

@allentsai

Hello, I would first like to thank you all for the hard work you've put into this project, and add the disclaimer that this could very well be user error, but my schema validates with other validators, and previous versions, but I get a warning with v5.2.1.

Stripped down to what is causing the error:
Base schema:

{
	"$schema": "http://json-schema.org/draft-04/schema#",
	"type": "object",
	"properties": {
		"location": {
			"anyOf": [
				{
					"$ref": "./postalList.json"
				},
				{
					"$ref": "./area.json"
				}
			]
		}
	},
	"additionalProperties": false,
	"required": [
		"location"
	]
}

postalList.json:

{
	"$schema": "http://json-schema.org/draft-04/schema#",
	"type": "object",
	"properties": {
		"countryLimit": {
			"type": "string"
		},
		"type": {
			"enum": ["postalList"]
		},
		"data": {
			"type": "array",
			"items": {
				"type": "string",
				"minLength": 2
			},
			"minItems": 1,
			"uniqueItems": true
		}
	},
	"additionalItems": false
}

area.json:

{
	"$schema": "http://json-schema.org/draft-04/schema#",
	"type": "object",
	"properties": {
		"type": {
			"enum": ["area"]
		},
		"data": {
			"type": "array",
			"items": {
				"$ref": "./areas.json"
			},
			"minItems":1,
			"uniqueItems": true
		}
	},
	"additionalProperties": false
}

areas.json:

{
	"$schema": "http://json-schema.org/draft-04/schema#",
	"type": "object",
	"properties": {
		"id": {
			"type": "number",
			"minimum": 0
		},
		"name": {
			"type": "string",
			"minLength": 1
		},
		"abbr": {
			"type": "string",
			"pattern": "[A-Z]+",
			"minLength": 1
		}
	},
	"required": ["id", "name", "abbr"]
}

Test Code:

$validator = new \JsonSchema\Validator();
$value = json_encode(
    ["location" => ["type" => "area", "data" => [["id" => 1, "name" => "area name", "abbr" => "AB"]]]]
);

$result = $validator->validate(json_decode($value), (object)['$ref' => 'file://' . realpath('./test.json')]);

This gives me the follow error repeated twice:

PHP Warning:  mb_detect_encoding() expects parameter 1 to be string, object given in /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/StringConstraint.php on line 54$
PHP Stack trace:$
PHP   1. {main}() /usr/local/src/placelocal/public_html/test.php:0$
PHP   2. JsonSchema\\Validator->validate() /usr/local/src/placelocal/public_html/test.php:105$
PHP   3. JsonSchema\\Constraints\\SchemaConstraint->check() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Validator.php:63$
PHP   4. JsonSchema\\Constraints\\Constraint->checkUndefined() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/SchemaConstraint.php:93$
PHP   5. JsonSchema\\Constraints\\UndefinedConstraint->check() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php:121$
PHP   6. JsonSchema\\Constraints\\UndefinedConstraint->validateTypes() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php:51$
PHP   7. JsonSchema\\Constraints\\Constraint->checkObject() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php:81$
PHP   8. JsonSchema\\Constraints\\ObjectConstraint->check() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php:88$
PHP   9. JsonSchema\\Constraints\\ObjectConstraint->validateProperties() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/ObjectConstraint.php:47$
PHP  10. JsonSchema\\Constraints\\Constraint->checkUndefined() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/ObjectConstraint.php:145$
PHP  11. JsonSchema\\Constraints\\UndefinedConstraint->check() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php:121$
PHP  12. JsonSchema\\Constraints\\UndefinedConstraint->validateOfProperties() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php:48$
PHP  13. JsonSchema\\Constraints\\Constraint->checkUndefined() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php:319$
PHP  14. JsonSchema\\Constraints\\UndefinedConstraint->check() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php:121$
PHP  15. JsonSchema\\Constraints\\UndefinedConstraint->validateTypes() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php:51$
PHP  16. JsonSchema\\Constraints\\Constraint->checkObject() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php:81$
PHP  17. JsonSchema\\Constraints\\ObjectConstraint->check() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php:88$
PHP  18. JsonSchema\\Constraints\\ObjectConstraint->validateProperties() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/ObjectConstraint.php:47$
PHP  19. JsonSchema\\Constraints\\Constraint->checkUndefined() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/ObjectConstraint.php:145$
PHP  20. JsonSchema\\Constraints\\UndefinedConstraint->check() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php:121$
PHP  21. JsonSchema\\Constraints\\UndefinedConstraint->validateTypes() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php:51$
PHP  22. JsonSchema\\Constraints\\Constraint->checkArray() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php:66$
PHP  23. JsonSchema\\Constraints\\CollectionConstraint->check() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php:69$
PHP  24. JsonSchema\\Constraints\\CollectionConstraint->validateItems() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/CollectionConstraint.php:52$
PHP  25. JsonSchema\\Constraints\\StringConstraint->check() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/CollectionConstraint.php:86$
PHP  26. JsonSchema\\Constraints\\StringConstraint->strlen() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/StringConstraint.php:35$
PHP  27. mb_detect_encoding() /usr/local/src/placelocal/vendor/justinrainbow/json-schema/src/JsonSchema/Constraints/StringConstraint.php:54$

Seems like anyOf behavior is being too greedy when evaluating the included schemas. Note that if you swap the order of postalList.json and area.json, the warning no longer appears.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions