Optional example


  private static function validateReleaseData(array $data): void {
    $not_blank_constraints = [
      new Type('string'),
      new NotBlank(),
    ];
    $collection_constraint = new Collection([
      'fields' => [
        'version' => $not_blank_constraints,
        'date' => new Optional([new Type('numeric')]),
        'core_compatible' => new Optional([new Type('boolean')]),
        'core_compatibility_message' => new Optional($not_blank_constraints),
        'status' => new Choice(['published', 'unpublished']),
        'download_link' => new Optional($not_blank_constraints),
        'release_link' => $not_blank_constraints,
        'terms' => new Optional([
          new Type('array'),
          new Collection([
            'Release type' => new Optional([
              new Type('array'),
            ]),
          ]),


    public function testRequiredConstraintIsIgnored()
    {
        $violations = $this->validator->validate([]new Required());

        $this->assertCount(0, $violations);
    }

    public function testOptionalConstraintIsIgnored()
    {
        $violations = $this->validator->validate([]new Optional());

        $this->assertCount(0, $violations);
    }

    public function testValidateDoNotCascadeNestedObjectsAndArraysByDefault()
    {
        $this->metadataFactory->addMetadata(new ClassMetadata(CascadingEntity::class));
        $this->metadataFactory->addMetadata((new ClassMetadata(CascadedChild::class))
            ->addPropertyConstraint('name', new NotNull())
        );

        

        $this->expectException(ConstraintDefinitionException::class);
        new Collection([
            'foo' => new Valid(),
        ]);
    }

    public function testRejectValidConstraintWithinOptional()
    {
        $this->expectException(ConstraintDefinitionException::class);
        new Collection([
            'foo' => new Optional(new Valid()),
        ]);
    }

    public function testRejectValidConstraintWithinRequired()
    {
        $this->expectException(ConstraintDefinitionException::class);
        new Collection([
            'foo' => new Required(new Valid()),
        ]);
    }

    
$this->buildViolation('myMessage')
            ->setParameter('{{ field }}', '"baz"')
            ->atPath('property.path[baz]')
            ->setInvalidValue(6)
            ->setCode(Collection::NO_SUCH_FIELD_ERROR)
            ->assertRaised();
    }

    public function testExtraFieldsDisallowedWithOptionalValues()
    {
        $constraint = new Optional();

        $data = $this->prepareTestData([
            'baz' => 6,
        ]);

        $this->validator->validate($datanew Collection([
            'fields' => [
                'foo' => $constraint,
            ],
            'extraFieldsMessage' => 'myMessage',
        ]));

        
new Assert\Range(min: 3),
            ],
        ),
        Assert\Collection(
            fields: [
                'foo' => [
                    new Assert\NotNull(),
                    new Assert\Range(min: 3),
                ],
                'bar' => new Assert\Range(min: 5),
                'baz' => new Assert\Required([new Assert\Email()]),
                'qux' => new Assert\Optional([new Assert\NotBlank()]),
            ],
            allowExtraFields: true
        ),
        Assert\Choice(choices: ['A', 'B'], message: 'Must be one of %choices%'),
        Assert\AtLeastOneOf(
            constraints: [
                new Assert\NotNull(),
                new Assert\Range(min: 3),
            ],
            message: 'foo',
            includeInternalMessages: false,
        ),

        $expected->addConstraint(new Callback(['callback' => 'validateMe', 'payload' => 'foo']));
        $expected->addConstraint(new Callback('validateMeStatic'));
        $expected->addPropertyConstraint('firstName', new NotNull());
        $expected->addPropertyConstraint('firstName', new Range(['min' => 3]));
        $expected->addPropertyConstraint('firstName', new All([new NotNull()new Range(['min' => 3])]));
        $expected->addPropertyConstraint('firstName', new All(['constraints' => [new NotNull()new Range(['min' => 3])]]));
        $expected->addPropertyConstraint('firstName', new Collection([
            'foo' => [new NotNull()new Range(['min' => 3])],
            'bar' => new Range(['min' => 5]),
            'baz' => new Required([new Email()]),
            'qux' => new Optional([new NotBlank()]),
        ], null, null, true));
        $expected->addPropertyConstraint('firstName', new Choice([
            'message' => 'Must be one of %choices%',
            'choices' => ['A', 'B'],
        ]));
        $expected->addPropertyConstraint('firstName', new AtLeastOneOf([
            new NotNull(),
            new Range(['min' => 3]),
        ], null, null, 'foo', null, false));
        $expected->addPropertyConstraint('firstName', new Sequentially([
            new NotBlank(),
            
Home | Imprint | This part of the site doesn't use cookies.