Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function create(Constraint $constraint, PropertyMetadata $propertyMetadat

foreach ($constraint->fields as $field => $baseConstraint) {
/** @var Required|Optional $baseConstraint */
if ($baseConstraint instanceof Required) {
if ($baseConstraint instanceof Required && !$constraint->allowMissingFields) {
$required[] = $field;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,44 +77,88 @@ public function createProvider(): \Generator
{
yield 'empty' => [new Collection(['fields' => []]), new PropertyMetadata(), ['type' => 'object', 'properties' => [], 'additionalProperties' => false]];

$fields = [
'name' => new Required([
new NotBlank(),
]),
'email' => [
new NotNull(),
new Length(['min' => 2, 'max' => 255]),
new Email(['mode' => Email::VALIDATION_MODE_LOOSE]),
],
'phone' => new Optional([
new \Symfony\Component\Validator\Constraints\Type(['type' => 'string']),
new Regex(['pattern' => '/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\.\/0-9]*$/']),
]),
'age' => new Optional([
new \Symfony\Component\Validator\Constraints\Type(['type' => 'int']),
]),
'social' => new Collection([
'fields' => [
'githubUsername' => new NotNull(),
],
]),
];
$properties = [
'name' => [],
'email' => ['format' => 'email'],
'phone' => ['pattern' => '^([+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*)$'],
'age' => [],
'social' => ['type' => 'object', 'properties' => ['githubUsername' => []], 'additionalProperties' => false, 'required' => ['githubUsername']],
];
$required = ['name', 'email', 'social'];

yield 'with fields' => [
new Collection([
'fields' => $fields,
]),
new PropertyMetadata(),
[
'type' => 'object',
'properties' => $properties,
'additionalProperties' => false,
'required' => $required,
],
];

yield 'with fields + allowExtraFields' => [
new Collection([
'fields' => $fields,
'allowExtraFields' => true,
'fields' => [
'name' => new Required([
new NotBlank(),
]),
'email' => [
new NotNull(),
new Length(['min' => 2, 'max' => 255]),
new Email(['mode' => Email::VALIDATION_MODE_LOOSE]),
],
'phone' => new Optional([
new \Symfony\Component\Validator\Constraints\Type(['type' => 'string']),
new Regex(['pattern' => '/^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\.\/0-9]*$/']),
]),
'age' => new Optional([
new \Symfony\Component\Validator\Constraints\Type(['type' => 'int']),
]),
'social' => new Collection([
'fields' => [
'githubUsername' => new NotNull(),
],
]),
],
]),
new PropertyMetadata(),
[
'type' => 'object',
'properties' => [
'name' => [],
'email' => ['format' => 'email'],
'phone' => ['pattern' => '^([+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*)$'],
'age' => [],
'social' => ['type' => 'object', 'properties' => ['githubUsername' => []], 'additionalProperties' => false, 'required' => ['githubUsername']],
],
'properties' => $properties,
'additionalProperties' => true,
'required' => $required,
],
];

yield 'with fields + allowMissingFields' => [
new Collection([
'fields' => $fields,
'allowMissingFields' => true,
]),
new PropertyMetadata(),
[
'type' => 'object',
'properties' => $properties,
'additionalProperties' => false,
],
];

yield 'with fields + allowExtraFields + allowMissingFields' => [
new Collection([
'fields' => $fields,
'allowExtraFields' => true,
'allowMissingFields' => true,
]),
new PropertyMetadata(),
[
'type' => 'object',
'properties' => $properties,
'additionalProperties' => true,
'required' => ['name', 'email', 'social'],
],
];
}
Expand Down