|
9 | 9 |
|
10 | 10 | namespace JsonSchema\Tests\Constraints;
|
11 | 11 |
|
| 12 | +use JsonSchema\Constraints\UndefinedConstraint; |
| 13 | + |
12 | 14 | class RequiredPropertyTest extends BaseTestCase
|
13 | 15 | {
|
| 16 | + |
| 17 | + public function testErrorPropertyIsPopulatedForRequiredIfMissingInInput() |
| 18 | + { |
| 19 | + $validator = new UndefinedConstraint(); |
| 20 | + $document = json_decode( |
| 21 | + '{ |
| 22 | + "bar": 42 |
| 23 | + }' |
| 24 | + ); |
| 25 | + $schema = json_decode( |
| 26 | + '{ |
| 27 | + "type": "object", |
| 28 | + "properties": { |
| 29 | + "foo": {"type": "number"}, |
| 30 | + "bar": {"type": "number"} |
| 31 | + }, |
| 32 | + "required": ["foo"] |
| 33 | + }' |
| 34 | + ); |
| 35 | + |
| 36 | + $validator->check($document, $schema); |
| 37 | + $error = $validator->getErrors(); |
| 38 | + $this->assertErrorHasExpectedPropertyValue($error, "foo"); |
| 39 | + } |
| 40 | + |
| 41 | + public function testErrorPropertyIsPopulatedForRequiredIfEmptyValueInInput() |
| 42 | + { |
| 43 | + $validator = new UndefinedConstraint(); |
| 44 | + $document = json_decode( |
| 45 | + '{ |
| 46 | + "bar": 42, |
| 47 | + "foo": null |
| 48 | + }' |
| 49 | + ); |
| 50 | + $schema = json_decode( |
| 51 | + '{ |
| 52 | + "type": "object", |
| 53 | + "properties": { |
| 54 | + "foo": {"type": "number"}, |
| 55 | + "bar": {"type": "number"} |
| 56 | + }, |
| 57 | + "required": ["foo"] |
| 58 | + }' |
| 59 | + ); |
| 60 | + |
| 61 | + $validator->check($document, $schema); |
| 62 | + $error = $validator->getErrors(); |
| 63 | + $this->assertErrorHasExpectedPropertyValue($error, "foo"); |
| 64 | + } |
| 65 | + |
| 66 | + protected function assertErrorHasExpectedPropertyValue($error, $propertyValue) |
| 67 | + { |
| 68 | + if (!(isset($error[0]) && is_array($error[0]) && isset($error[0]['property']))) { |
| 69 | + $this->fail( |
| 70 | + "Malformed error response. Expected to have subset in form: array(0 => array('property' => <value>)))" |
| 71 | + . " . Error response was: " . json_encode($error) |
| 72 | + ); |
| 73 | + } |
| 74 | + $this->assertEquals($propertyValue, $error[0]['property']); |
| 75 | + |
| 76 | + } |
| 77 | + |
14 | 78 | public function getInvalidTests()
|
15 | 79 | {
|
16 | 80 | return array(
|
|
0 commit comments