mirror of
https://github.com/justinrainbow/json-schema.git
synced 2025-05-02 04:18:09 +02:00
* Add URI translation for retrieval & add local copies of spec schema * Add use line for InvalidArgumentException & adjust scope (#372) Fixes issue #371 * add quiet option (#382) * add quiet option * use verbose instead of quiet * add quiet option * always output dump-schema * always output dump-schema-url * fix typo and ws * [BUGFIX] Add provided schema under a dummy / internal URI (fixes #376) (#378) * Add provided schema under a dummy / internal URI (fixes #376) In order to resolve internal $ref references within a user-provided schema, SchemaStorage needs to know about the schema. As user-supplied schemas do not have an associated URI, use a dummy / internal one instead. * Remove dangling use * Change URI to class constant on SchemaStorage * Add option to disable validation of "format" constraint (#383) * Add more unit tests (#366) * Add test coverage for coercion API * Complete test coverage for SchemaStorage * Add test coverage for ObjectIterator * Add exception test for JsonPointer * MabeEnum\Enum appears to use singletons - add testing const * Don't check this line for coverage mbstring is on all test platforms, so this line will never be reached. * Add test for TypeConstraint::validateTypeNameWording() * Add test for exception on TypeConstraint::validateType() * PHPunit doesn't like an explanation with its @codeCoverageIgnore... * Add various tests for UriRetriever * Add tests for FileGetContents * Add tests for JsonSchema\Uri\Retrievers\Curl * Add missing bad-syntax test file * Restrict ignore to the exception line only * Fix exception scope * Allow the schema to be an associative array (#389) * Allow the schema to be an associative array Implements #388. * Use json_decode(json_encode()) for array -> object cast * Skip exception check on PHP versions < 5.5.0 * Skip test on HHVM, as it's happy to encode resources * Enable FILTER_FLAG_EMAIL_UNICODE for email format if present (#398) * Don't throw exceptions until after checking anyOf / oneOf (#394) Fixes #393 * Fix infinite recursion on some schemas when setting defaults (#359) (#365) * Don't try to fetch files that don't exist Throws an exception when the ref can't be resolved to a useful file URI, rather than waiting for something further down the line to fail after the fact. * Refactor defaults code to use LooseTypeCheck where appropriate * Test for not treating non-containers like arrays * Update comments * Rename variable for clarity * Add CHECK_MODE_ONLY_REQUIRED_DEFAULTS If CHECK_MODE_ONLY_REQUIRED_DEFAULTS is set, then only apply defaults if they are marked as required. * Workaround for $this scope issue on PHP-5.3 * Fix infinite recursion via $ref when applying defaults * Add missing second test for array case * Add test for setting a default value for null * Also fix infinite recursion via $ref for array defaults * Move nested closure into separate method * $parentSchema will always be set when $name is, so don't check it * Handle nulls properly - fixes issue #377 * Add option to also validate the schema (#357) * Remove stale files from #357 (obviated by #362) (#400) * Stop #386 sneaking in alongside another PR backport
442 lines
12 KiB
PHP
442 lines
12 KiB
PHP
<?php
|
|
|
|
/*
|
|
* This file is part of the JsonSchema package.
|
|
*
|
|
* For the full copyright and license information, please view the LICENSE
|
|
* file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace JsonSchema\Tests\Constraints;
|
|
|
|
use JsonSchema\Constraints\Constraint;
|
|
use JsonSchema\Constraints\UndefinedConstraint;
|
|
|
|
class RequiredPropertyTest extends BaseTestCase
|
|
{
|
|
// Most tests are draft-03 compliant, but some tests are draft-04, or mix draft-03 and
|
|
// draft-04 syntax within the same schema. Unfortunately, draft-03 and draft-04 required
|
|
// definitions are incompatible, so disabling schema validation for these tests.
|
|
protected $schemaSpec = 'http://json-schema.org/draft-03/schema#';
|
|
protected $validateSchema = false;
|
|
|
|
public function testErrorPropertyIsPopulatedForRequiredIfMissingInInput()
|
|
{
|
|
$validator = new UndefinedConstraint();
|
|
$document = json_decode(
|
|
'{
|
|
"bar": 42
|
|
}'
|
|
);
|
|
$schema = json_decode(
|
|
'{
|
|
"type": "object",
|
|
"properties": {
|
|
"foo": {"type": "number"},
|
|
"bar": {"type": "number"}
|
|
},
|
|
"required": ["foo"]
|
|
}'
|
|
);
|
|
|
|
$validator->check($document, $schema);
|
|
$error = $validator->getErrors();
|
|
$this->assertErrorHasExpectedPropertyValue($error, 'foo');
|
|
}
|
|
|
|
public function testPathErrorPropertyIsPopulatedForRequiredIfMissingInInput()
|
|
{
|
|
$validator = new UndefinedConstraint();
|
|
$document = json_decode(
|
|
'{
|
|
"foo": [{"baz": 1.5}]
|
|
}'
|
|
);
|
|
$schema = json_decode(
|
|
'{
|
|
"type": "object",
|
|
"properties": {
|
|
"foo": {
|
|
"type": "array",
|
|
"items": {
|
|
"type": "object",
|
|
"properties": {
|
|
"bar": {"type": "number"},
|
|
"baz": {"type": "number"}
|
|
},
|
|
"required": ["bar"]
|
|
}
|
|
}
|
|
},
|
|
"required": ["foo"]
|
|
}'
|
|
);
|
|
|
|
$validator->check($document, $schema);
|
|
$error = $validator->getErrors();
|
|
$this->assertErrorHasExpectedPropertyValue($error, 'foo[0].bar');
|
|
}
|
|
|
|
public function testErrorPropertyIsPopulatedForRequiredIfEmptyValueInInput()
|
|
{
|
|
$validator = new UndefinedConstraint();
|
|
$document = json_decode(
|
|
'{
|
|
"bar": 42,
|
|
"foo": null
|
|
}'
|
|
);
|
|
$schema = json_decode(
|
|
'{
|
|
"type": "object",
|
|
"properties": {
|
|
"foo": {"type": "number"},
|
|
"bar": {"type": "number"}
|
|
},
|
|
"required": ["foo"]
|
|
}'
|
|
);
|
|
|
|
$validator->check($document, $schema);
|
|
$error = $validator->getErrors();
|
|
$this->assertErrorHasExpectedPropertyValue($error, 'foo');
|
|
}
|
|
|
|
protected function assertErrorHasExpectedPropertyValue($error, $propertyValue)
|
|
{
|
|
if (!(isset($error[0]) && is_array($error[0]) && isset($error[0]['property']))) {
|
|
$this->fail(
|
|
"Malformed error response. Expected to have subset in form: array(0 => array('property' => <value>)))"
|
|
. ' . Error response was: ' . json_encode($error)
|
|
);
|
|
}
|
|
$this->assertEquals($propertyValue, $error[0]['property']);
|
|
}
|
|
|
|
public function getInvalidTests()
|
|
{
|
|
return array(
|
|
array(
|
|
'{}',
|
|
'{
|
|
"type":"object",
|
|
"properties":{
|
|
"number":{"type":"number","required":true}
|
|
}
|
|
}'
|
|
),
|
|
array(
|
|
'{}',
|
|
'{
|
|
"type": "object",
|
|
"properties": {
|
|
"number": {"type": "number"}
|
|
},
|
|
"required": ["number"]
|
|
}'
|
|
),
|
|
array(
|
|
'{
|
|
"foo": {}
|
|
}',
|
|
'{
|
|
"type": "object",
|
|
"properties": {
|
|
"foo": {
|
|
"type": "object",
|
|
"properties": {
|
|
"bar": {"type": "number"}
|
|
},
|
|
"required": ["bar"]
|
|
}
|
|
}
|
|
}'
|
|
),
|
|
array(
|
|
'{
|
|
"bar": 1.4
|
|
}',
|
|
'{
|
|
"type": "object",
|
|
"properties": {
|
|
"foo": {"type": "string", "required": true},
|
|
"bar": {"type": "number"}
|
|
},
|
|
"required": ["bar"]
|
|
}'
|
|
),
|
|
array(
|
|
'{}',
|
|
'{
|
|
"required": ["foo"]
|
|
}'
|
|
),
|
|
array(
|
|
'{
|
|
}',
|
|
'{
|
|
"type": "object",
|
|
"properties": {
|
|
"foo": { "required": true }
|
|
}
|
|
}'
|
|
),
|
|
array(
|
|
'{
|
|
"string":{}
|
|
}',
|
|
'{
|
|
"type":"object",
|
|
"properties": {
|
|
"string":{"type":"string", "required": true}
|
|
}
|
|
}'
|
|
),
|
|
array(
|
|
'{
|
|
"number":{}
|
|
}',
|
|
'{
|
|
"type":"object",
|
|
"properties": {
|
|
"number":{"type":"number", "required": true}
|
|
}
|
|
}'
|
|
),
|
|
array(
|
|
'{
|
|
"integer":{}
|
|
}',
|
|
'{
|
|
"type":"object",
|
|
"properties": {
|
|
"integer":{"type":"integer", "required": true}
|
|
}
|
|
}'
|
|
),
|
|
array(
|
|
'{
|
|
"boolean":{}
|
|
}',
|
|
'{
|
|
"type":"object",
|
|
"properties": {
|
|
"boolean":{"type":"boolean", "required": true}
|
|
}
|
|
}'
|
|
),
|
|
array(
|
|
'{
|
|
"array":{}
|
|
}',
|
|
'{
|
|
"type":"object",
|
|
"properties": {
|
|
"array":{"type":"array", "required": true}
|
|
}
|
|
}',
|
|
Constraint::CHECK_MODE_NORMAL
|
|
),
|
|
array(
|
|
'{
|
|
"null":{}
|
|
}',
|
|
'{
|
|
"type":"object",
|
|
"properties": {
|
|
"null":{"type":"null", "required": true}
|
|
}
|
|
}'
|
|
),
|
|
array(
|
|
'{
|
|
"foo": {"baz": 1.5}
|
|
}',
|
|
'{
|
|
"type": "object",
|
|
"properties": {
|
|
"foo": {
|
|
"type": "object",
|
|
"properties": {
|
|
"bar": {"type": "number"}
|
|
},
|
|
"required": ["bar"]
|
|
}
|
|
}
|
|
}'
|
|
),
|
|
array(
|
|
'{
|
|
"foo": {"baz": 1.5}
|
|
}',
|
|
'{
|
|
"type": "object",
|
|
"properties": {
|
|
"foo": {
|
|
"type": "object",
|
|
"properties": {
|
|
"bar": {"type": "number", "required": true}
|
|
}
|
|
}
|
|
}
|
|
}'
|
|
),
|
|
);
|
|
}
|
|
|
|
public function getValidTests()
|
|
{
|
|
return array(
|
|
array(
|
|
'{
|
|
"number": 1.4
|
|
}',
|
|
'{
|
|
"type":"object",
|
|
"properties":{
|
|
"number":{"type":"number","required":true}
|
|
}
|
|
}'
|
|
),
|
|
array(
|
|
'{}',
|
|
'{
|
|
"type":"object",
|
|
"properties":{
|
|
"number":{"type":"number"}
|
|
}
|
|
}'
|
|
),
|
|
array(
|
|
'{}',
|
|
'{
|
|
"type":"object",
|
|
"properties":{
|
|
"number":{"type":"number","required":false}
|
|
}
|
|
}'
|
|
),
|
|
array(
|
|
'{
|
|
"number": 0
|
|
}',
|
|
'{
|
|
"type":"object",
|
|
"properties":{
|
|
"number":{"type":"integer","required":true}
|
|
}
|
|
}'
|
|
),
|
|
array(
|
|
'{
|
|
"is_active": false
|
|
}',
|
|
'{
|
|
"type":"object",
|
|
"properties":{
|
|
"is_active":{"type":"boolean","required":true}
|
|
}
|
|
}'
|
|
),
|
|
array(
|
|
'{
|
|
"status": null
|
|
}',
|
|
'{
|
|
"type":"object",
|
|
"properties":{
|
|
"status":{"type":"null","required":true}
|
|
}
|
|
}'
|
|
),
|
|
array(
|
|
'{
|
|
"users": []
|
|
}',
|
|
'{
|
|
"type":"object",
|
|
"properties":{
|
|
"users":{"type":"array","required":true}
|
|
}
|
|
}'
|
|
),
|
|
array(
|
|
'{
|
|
"foo": "foo",
|
|
"bar": 1.4
|
|
}',
|
|
'{
|
|
"type": "object",
|
|
"properties": {
|
|
"foo": {"type": "string", "required": true},
|
|
"bar": {"type": "number"}
|
|
},
|
|
"required": ["bar"]
|
|
}'
|
|
),
|
|
array(
|
|
'{
|
|
"foo": {"bar": 1.5}
|
|
}',
|
|
'{
|
|
"type": "object",
|
|
"properties": {
|
|
"foo": {
|
|
"type": "object",
|
|
"properties": {
|
|
"bar": {"type": "number"}
|
|
},
|
|
"required": ["bar"]
|
|
}
|
|
},
|
|
"required": ["foo"]
|
|
}'
|
|
),
|
|
array(
|
|
'{
|
|
"foo": {}
|
|
}',
|
|
'{
|
|
"type": "object",
|
|
"properties": {
|
|
"foo": { "required": true }
|
|
}
|
|
}'
|
|
),
|
|
array(
|
|
'{
|
|
"boo": {"bar": 1.5}
|
|
}',
|
|
'{
|
|
"type": "object",
|
|
"properties": {
|
|
"foo": {
|
|
"type": "object",
|
|
"properties": {
|
|
"bar": {"type": "number"}
|
|
},
|
|
"required": ["bar"]
|
|
}
|
|
}
|
|
}'
|
|
),
|
|
array(
|
|
'{
|
|
"boo": {"bar": 1.5}
|
|
}',
|
|
'{
|
|
"type": "object",
|
|
"properties": {
|
|
"foo": {
|
|
"type": "object",
|
|
"properties": {
|
|
"bar": {"type": "number", "required": true}
|
|
}
|
|
}
|
|
}
|
|
}'
|
|
),
|
|
);
|
|
}
|
|
}
|