mirror of
https://github.com/justinrainbow/json-schema.git
synced 2025-05-31 19:09:12 +02:00
Correctly set the schema ID when passing it as assoc array (#794)
Replaces #769 due to lack of permissions to update PR Closes https://github.com/jsonrainbow/json-schema/issues/767. --------- Co-authored-by: Dalibor Karlović <dalibor.karlovic@sigwin.hr>
This commit is contained in:
parent
44066980b7
commit
649793d2b9
@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
- Upgrade php cs fixer to latest ([#783](https://github.com/jsonrainbow/json-schema/pull/783))
|
||||
- Create deep copy before checking each sub schema in oneOf ([#791](https://github.com/jsonrainbow/json-schema/pull/791))
|
||||
- Create deep copy before checking each sub schema in anyOf ([#792](https://github.com/jsonrainbow/json-schema/pull/792))
|
||||
- Correctly set the schema ID when passing it as assoc array ([#794](https://github.com/jsonrainbow/json-schema/pull/794))
|
||||
|
||||
### Changed
|
||||
- Used PHPStan's int-mask-of<T> type where applicable ([#779](https://github.com/jsonrainbow/json-schema/pull/779))
|
||||
|
@ -44,7 +44,7 @@ class LooseTypeCheck implements TypeCheckInterface
|
||||
return property_exists($value, $property);
|
||||
}
|
||||
|
||||
return array_key_exists($property, $value);
|
||||
return is_array($value) && array_key_exists($property, $value);
|
||||
}
|
||||
|
||||
public static function propertyCount($value)
|
||||
|
@ -13,6 +13,7 @@ namespace JsonSchema;
|
||||
|
||||
use JsonSchema\Constraints\BaseConstraint;
|
||||
use JsonSchema\Constraints\Constraint;
|
||||
use JsonSchema\Constraints\TypeCheck\LooseTypeCheck;
|
||||
|
||||
/**
|
||||
* A JsonSchema Constraint
|
||||
@ -59,10 +60,9 @@ class Validator extends BaseConstraint
|
||||
}
|
||||
|
||||
// add provided schema to SchemaStorage with internal URI to allow internal $ref resolution
|
||||
if (is_object($schema) && property_exists($schema, 'id')) {
|
||||
$schemaURI = $schema->id;
|
||||
} else {
|
||||
$schemaURI = SchemaStorage::INTERNAL_PROVIDED_SCHEMA_URI;
|
||||
$schemaURI = SchemaStorage::INTERNAL_PROVIDED_SCHEMA_URI;
|
||||
if (LooseTypeCheck::propertyExists($schema, 'id')) {
|
||||
$schemaURI = LooseTypeCheck::propertyGet($schema, 'id');
|
||||
}
|
||||
$this->factory->getSchemaStorage()->addSchema($schemaURI, $schema);
|
||||
|
||||
|
@ -18,6 +18,17 @@ class ValidatorTest extends TestCase
|
||||
$this->assertFalse($validator->isValid(), 'Validation succeeded, but should have failed.');
|
||||
}
|
||||
|
||||
public function testValidateWithAssocSchemaWithRelativeRefs(): void
|
||||
{
|
||||
$schema = json_decode(file_get_contents(__DIR__ . '/fixtures/relative.json'), true);
|
||||
$data = json_decode('{"foo":{"foo": "bar"}}', false);
|
||||
|
||||
$validator = new Validator();
|
||||
$validator->validate($data, $schema);
|
||||
|
||||
$this->assertTrue($validator->isValid(), 'Validation failed, but should have succeeded.');
|
||||
}
|
||||
|
||||
public function testBadAssocSchemaInput(): void
|
||||
{
|
||||
if (version_compare(phpversion(), '5.5.0', '<')) {
|
||||
|
11
tests/fixtures/relative.json
vendored
Normal file
11
tests/fixtures/relative.json
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
{
|
||||
"id": "tests/fixtures/relative.json",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"foo": {
|
||||
"$ref": "foobar.json"
|
||||
}
|
||||
},
|
||||
"required": ["foo"],
|
||||
"additionalProperties": false
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user