|
547 | 547 | ->and($objectWithoutRequired->hasRequiredProperties())->toBeFalse() |
548 | 548 | ->and($emptyObject->hasRequiredProperties())->toBeFalse(); |
549 | 549 | }); |
| 550 | + |
| 551 | +it('can mark all properties as required with requireAll method', function (): void { |
| 552 | + $objectSchema = Schema::object('user') |
| 553 | + ->properties( |
| 554 | + Schema::string('name'), |
| 555 | + Schema::integer('age'), |
| 556 | + Schema::string('email'), |
| 557 | + ) |
| 558 | + ->requireAll(); |
| 559 | + |
| 560 | + $schemaArray = $objectSchema->toArray(); |
| 561 | + |
| 562 | + expect($schemaArray)->toHaveKey('required', ['name', 'age', 'email']) |
| 563 | + ->and($objectSchema->getRequiredProperties())->toHaveCount(3) |
| 564 | + ->and($objectSchema->getRequiredProperties())->toContain('name') |
| 565 | + ->and($objectSchema->getRequiredProperties())->toContain('age') |
| 566 | + ->and($objectSchema->getRequiredProperties())->toContain('email'); |
| 567 | + |
| 568 | + // Validation test - all properties are now required |
| 569 | + expect(fn() => $objectSchema->validate([ |
| 570 | + 'name' => 'John Doe', |
| 571 | + 'age' => 30, |
| 572 | + ]))->toThrow( |
| 573 | + SchemaException::class, |
| 574 | + 'The required properties (email) are missing', |
| 575 | + ); |
| 576 | + |
| 577 | + expect(fn() => $objectSchema->validate([ |
| 578 | + 'name' => 'John Doe', |
| 579 | + 'email' => 'john@example.com', |
| 580 | + ]))->toThrow( |
| 581 | + SchemaException::class, |
| 582 | + 'The required properties (age) are missing', |
| 583 | + ); |
| 584 | + |
| 585 | + expect(fn() => $objectSchema->validate([ |
| 586 | + 'name' => 'John Doe', |
| 587 | + 'age' => 30, |
| 588 | + 'email' => 'john@example.com', |
| 589 | + ]))->not->toThrow(SchemaException::class); |
| 590 | +}); |
0 commit comments