diff --git a/src/ValidationField.php b/src/ValidationField.php index 2a1a1a5..5609fab 100644 --- a/src/ValidationField.php +++ b/src/ValidationField.php @@ -191,7 +191,7 @@ public function val($key, $default = null) { * @return bool Returns **true** if the field has a key or **false** otherwise. */ public function hasVal($key) { - return array_key_exists($key, $this->field); + return isset($this->field[$key]) || (is_array($this->field) && array_key_exists($key, $this->field)); } /** diff --git a/tests/RealWorldTest.php b/tests/RealWorldTest.php index 697d7f0..c0e8e36 100644 --- a/tests/RealWorldTest.php +++ b/tests/RealWorldTest.php @@ -116,4 +116,19 @@ public function testNestedMergedFilteredAdd() { $validatedData = $filteredSchema->validate($data); $this->assertEquals($expectedData, $validatedData); } + + /** + * A nested schema object should have its default value respected. + */ + public function testNestedDefaultRequired() { + $schema = Schema::parse([ + 'letter' => Schema::parse([ + 'default' => 'a', + 'type' => 'string', + ]) + ]); + $data = []; + $data = $schema->validate($data); + $this->assertEquals(['letter' => 'a'], $data); + } }