Closed dzhiryakov closed 6 years ago
Good catch. One solution to this is to define a default value in the default
property. That way the instance of the DTO will always have the properties it needs (even if they are empty).
e.g.
class BlogPostDto extends \Dto\Dto
{
protected $schema = [
'type' => 'object',
'properties' => [
'title' => ['type' => 'string'],
'content' => ['type' => 'string'],
'custom_fields' => [
'type' => 'object',
'additionalProperties' => ['type' => 'string']
]
],
'default' => [
'custom_fields' => []
]
];
}
I have updated the example and added an explanation.
Thanks! Now is work fine.
On this page https://github.com/fireproofsocks/dto/wiki/Ambiguous this sample don't work:
When i use this example it throw exception
Dto\Exceptions\InvalidKeyException: The key "custom_fields" does not exist in this DTO
.PHP 7.1.13