|
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 | + "bar": 42 |
| 22 | + }'); |
| 23 | + $schema = json_decode('{ |
| 24 | + "type": "object", |
| 25 | + "properties": { |
| 26 | + "foo": {"type": "number"}, |
| 27 | + "bar": {"type": "number"} |
| 28 | + }, |
| 29 | + "required": ["foo"] |
| 30 | + }'); |
| 31 | + |
| 32 | + $validator->check($document, $schema); |
| 33 | + $error = $validator->getErrors(); |
| 34 | + if(!isset($error[0]['property'])) { |
| 35 | + $this->fail("Malformed error response. Expected \$error[0]['property'] to be set. Error response was " |
| 36 | + . json_encode($error)); |
| 37 | + } |
| 38 | + $this->assertEquals('foo', $error[0]['property']); |
| 39 | + } |
| 40 | + |
| 41 | + public function testErrorPropertyIsPopulatedForRequiredIfEmptyValueInInput() |
| 42 | + { |
| 43 | + $validator = new UndefinedConstraint(); |
| 44 | + $document = json_decode('{ |
| 45 | + "bar": 42, |
| 46 | + "foo": null |
| 47 | + }'); |
| 48 | + $schema = json_decode('{ |
| 49 | + "type": "object", |
| 50 | + "properties": { |
| 51 | + "foo": {"type": "number"}, |
| 52 | + "bar": {"type": "number"} |
| 53 | + }, |
| 54 | + "required": ["foo"] |
| 55 | + }'); |
| 56 | + |
| 57 | + $validator->check($document, $schema); |
| 58 | + $error = $validator->getErrors(); |
| 59 | + if(!isset($error[0]['property'])) { |
| 60 | + $this->fail("Malformed error response. Expected \$error[0]['property'] to be set. Error response was " |
| 61 | + . json_encode($error)); |
| 62 | + } |
| 63 | + $this->assertEquals('foo', $error[0]['property']); |
| 64 | + } |
| 65 | + |
14 | 66 | public function getInvalidTests()
|
15 | 67 | {
|
16 | 68 | return array(
|
|
0 commit comments