|
7 | 7 | use namespace Facebook\TypeAssert; |
8 | 8 | use function Facebook\FBExpect\expect; |
9 | 9 |
|
| 10 | +use type Slack\Hack\JsonSchema\{FieldErrorCode, FieldErrorConstraint}; |
10 | 11 | use type Slack\Hack\JsonSchema\Tests\Generated\ObjectSchemaValidator; |
11 | 12 |
|
12 | 13 | final class ObjectSchemaValidatorTest extends BaseCodegenTestCase { |
@@ -530,4 +531,165 @@ public function testAdditionalProperitesRef(): void { |
530 | 531 | expect($validator->isValid())->toBeFalse(); |
531 | 532 | } |
532 | 533 |
|
| 534 | + public function testMinPropertiesWithValidLength(): void { |
| 535 | + $validator = new ObjectSchemaValidator(dict[ |
| 536 | + 'only_min_properties' => dict[ |
| 537 | + 'a' => 0, |
| 538 | + ], |
| 539 | + ]); |
| 540 | + |
| 541 | + $validator->validate(); |
| 542 | + expect($validator->isValid())->toBeTrue(); |
| 543 | + } |
| 544 | + |
| 545 | + public function testMinPropertiesWithInvalidLength(): void { |
| 546 | + $validator = new ObjectSchemaValidator(dict[ |
| 547 | + 'only_min_properties' => dict[], |
| 548 | + ]); |
| 549 | + |
| 550 | + $validator->validate(); |
| 551 | + expect($validator->isValid())->toBeFalse(); |
| 552 | + |
| 553 | + $errors = $validator->getErrors(); |
| 554 | + expect(C\count($errors))->toEqual(1); |
| 555 | + |
| 556 | + $error = C\firstx($errors); |
| 557 | + expect($error['code'])->toEqual(FieldErrorCode::FAILED_CONSTRAINT); |
| 558 | + expect($error['message'])->toEqual('must have minimum 1 properties'); |
| 559 | + |
| 560 | + $constraint = Shapes::at($error, 'constraint'); |
| 561 | + expect($constraint['type'])->toEqual(FieldErrorConstraint::MIN_PROPERTIES); |
| 562 | + expect($constraint['got'] ?? null)->toEqual(0); |
| 563 | + } |
| 564 | + |
| 565 | + public function testMaxPropertiesWithValidLength(): void { |
| 566 | + // maxProperties is set to 1, so having 1 value should be fine |
| 567 | + $validator = new ObjectSchemaValidator(dict[ |
| 568 | + 'only_max_properties' => dict[ |
| 569 | + 'a' => 0, |
| 570 | + ], |
| 571 | + ]); |
| 572 | + |
| 573 | + $validator->validate(); |
| 574 | + expect($validator->isValid())->toBeTrue(); |
| 575 | + |
| 576 | + // maxProperties is set to 1, so having no values should also be fine |
| 577 | + $validator = new ObjectSchemaValidator(dict[ |
| 578 | + 'only_max_properties' => dict[], |
| 579 | + ]); |
| 580 | + |
| 581 | + $validator->validate(); |
| 582 | + expect($validator->isValid())->toBeTrue(); |
| 583 | + } |
| 584 | + |
| 585 | + public function testMaxPropertiesWithInvalidLength(): void { |
| 586 | + $validator = new ObjectSchemaValidator(dict[ |
| 587 | + 'only_max_properties' => dict[ |
| 588 | + 'a' => 0, |
| 589 | + 'b' => 1, |
| 590 | + ], |
| 591 | + ]); |
| 592 | + |
| 593 | + $validator->validate(); |
| 594 | + expect($validator->isValid())->toBeFalse(); |
| 595 | + |
| 596 | + $errors = $validator->getErrors(); |
| 597 | + expect(C\count($errors))->toEqual(1); |
| 598 | + |
| 599 | + $error = C\firstx($errors); |
| 600 | + expect($error['code'])->toEqual(FieldErrorCode::FAILED_CONSTRAINT); |
| 601 | + expect($error['message'])->toEqual('no more than 1 properties allowed'); |
| 602 | + |
| 603 | + $constraint = Shapes::at($error, 'constraint'); |
| 604 | + expect($constraint['type'])->toEqual(FieldErrorConstraint::MAX_PROPERTIES); |
| 605 | + expect($constraint['got'] ?? null)->toEqual(2); |
| 606 | + } |
| 607 | + |
| 608 | + public function testMinAndMaxPropertiesWithValidLength(): void { |
| 609 | + $validator = new ObjectSchemaValidator(dict[ |
| 610 | + 'min_and_max_properties' => dict[ |
| 611 | + 'a' => 0, |
| 612 | + ], |
| 613 | + ]); |
| 614 | + |
| 615 | + $validator->validate(); |
| 616 | + expect($validator->isValid())->toBeTrue(); |
| 617 | + |
| 618 | + $validator = new ObjectSchemaValidator(dict[ |
| 619 | + 'min_and_max_properties' => dict[ |
| 620 | + 'a' => 0, |
| 621 | + 'b' => 1, |
| 622 | + ], |
| 623 | + ]); |
| 624 | + |
| 625 | + $validator->validate(); |
| 626 | + expect($validator->isValid())->toBeTrue(); |
| 627 | + } |
| 628 | + |
| 629 | + public function testMinAndMaxPropertiesWithInvalidLength(): void { |
| 630 | + // minProperties is set to 1, violate it |
| 631 | + $validator = new ObjectSchemaValidator(dict[ |
| 632 | + 'min_and_max_properties' => dict[], |
| 633 | + ]); |
| 634 | + |
| 635 | + $validator->validate(); |
| 636 | + expect($validator->isValid())->toBeFalse(); |
| 637 | + |
| 638 | + $errors = $validator->getErrors(); |
| 639 | + expect(C\count($errors))->toEqual(1); |
| 640 | + |
| 641 | + $error = C\firstx($errors); |
| 642 | + expect($error['code'])->toEqual(FieldErrorCode::FAILED_CONSTRAINT); |
| 643 | + expect($error['message'])->toEqual('must have minimum 1 properties'); |
| 644 | + |
| 645 | + $constraint = Shapes::at($error, 'constraint'); |
| 646 | + expect($constraint['type'])->toEqual(FieldErrorConstraint::MIN_PROPERTIES); |
| 647 | + expect($constraint['got'] ?? null)->toEqual(0); |
| 648 | + |
| 649 | + // maxProperties is set to 2, violate it |
| 650 | + $validator = new ObjectSchemaValidator(dict[ |
| 651 | + 'min_and_max_properties' => dict[ |
| 652 | + 'a' => 0, |
| 653 | + 'b' => 1, |
| 654 | + 'c' => 2, |
| 655 | + ], |
| 656 | + ]); |
| 657 | + |
| 658 | + $validator->validate(); |
| 659 | + expect($validator->isValid())->toBeFalse(); |
| 660 | + |
| 661 | + $errors = $validator->getErrors(); |
| 662 | + expect(C\count($errors))->toEqual(1); |
| 663 | + |
| 664 | + $error = C\firstx($errors); |
| 665 | + expect($error['code'])->toEqual(FieldErrorCode::FAILED_CONSTRAINT); |
| 666 | + expect($error['message'])->toEqual('no more than 2 properties allowed'); |
| 667 | + |
| 668 | + $constraint = Shapes::at($error, 'constraint'); |
| 669 | + expect($constraint['type'])->toEqual(FieldErrorConstraint::MAX_PROPERTIES); |
| 670 | + expect($constraint['got'] ?? null)->toEqual(3); |
| 671 | + } |
| 672 | + |
| 673 | + public function testInvalidMinPropertiesWithNoAdditionalProperties(): void { |
| 674 | + $validator = new ObjectSchemaValidator(dict[ |
| 675 | + 'invalid_min_properties_with_no_additional_properties' => dict[ |
| 676 | + 'a' => 0, |
| 677 | + ], |
| 678 | + ]); |
| 679 | + |
| 680 | + $validator->validate(); |
| 681 | + expect($validator->isValid())->toBeFalse(); |
| 682 | + |
| 683 | + $errors = $validator->getErrors(); |
| 684 | + expect(C\count($errors))->toEqual(1); |
| 685 | + |
| 686 | + $error = C\firstx($errors); |
| 687 | + expect($error['code'])->toEqual(FieldErrorCode::FAILED_CONSTRAINT); |
| 688 | + expect($error['message'])->toEqual('invalid additional property: a'); |
| 689 | + |
| 690 | + $constraint = Shapes::at($error, 'constraint'); |
| 691 | + expect($constraint['type'])->toEqual(FieldErrorConstraint::ADDITIONAL_PROPERTIES); |
| 692 | + expect($constraint['got'] ?? null)->toEqual('a'); |
| 693 | + } |
| 694 | + |
533 | 695 | } |
0 commit comments