|
9 | 9 | RangeOperatorSchema, |
10 | 10 | StringOperatorSchema, |
11 | 11 | SpecialOperatorSchema, |
| 12 | + NormalizedFilterSchema, |
12 | 13 | FILTER_OPERATORS, |
13 | 14 | LOGICAL_OPERATORS, |
14 | 15 | ALL_OPERATORS, |
@@ -620,3 +621,135 @@ describe('Real-World Use Cases', () => { |
620 | 621 | expect(() => QueryFilterSchema.parse(filter)).not.toThrow(); |
621 | 622 | }); |
622 | 623 | }); |
| 624 | + |
| 625 | +describe('NormalizedFilterSchema', () => { |
| 626 | + it('should accept normalized $and condition', () => { |
| 627 | + const filter = { |
| 628 | + $and: [ |
| 629 | + { age: { $eq: 18 } }, |
| 630 | + { role: { $eq: 'admin' } } |
| 631 | + ] |
| 632 | + }; |
| 633 | + |
| 634 | + expect(() => NormalizedFilterSchema.parse(filter)).not.toThrow(); |
| 635 | + }); |
| 636 | + |
| 637 | + it('should accept normalized $or condition', () => { |
| 638 | + const filter = { |
| 639 | + $or: [ |
| 640 | + { status: { $eq: 'active' } }, |
| 641 | + { status: { $eq: 'pending' } } |
| 642 | + ] |
| 643 | + }; |
| 644 | + |
| 645 | + expect(() => NormalizedFilterSchema.parse(filter)).not.toThrow(); |
| 646 | + }); |
| 647 | + |
| 648 | + it('should accept normalized $not condition', () => { |
| 649 | + const filter = { |
| 650 | + $not: { deleted: { $eq: true } } |
| 651 | + }; |
| 652 | + |
| 653 | + expect(() => NormalizedFilterSchema.parse(filter)).not.toThrow(); |
| 654 | + }); |
| 655 | + |
| 656 | + it('should accept nested normalized filters in $and', () => { |
| 657 | + const filter = { |
| 658 | + $and: [ |
| 659 | + { age: { $gte: 18 } }, |
| 660 | + { |
| 661 | + $or: [ |
| 662 | + { role: { $eq: 'admin' } }, |
| 663 | + { role: { $eq: 'moderator' } } |
| 664 | + ] |
| 665 | + } |
| 666 | + ] |
| 667 | + }; |
| 668 | + |
| 669 | + expect(() => NormalizedFilterSchema.parse(filter)).not.toThrow(); |
| 670 | + }); |
| 671 | + |
| 672 | + it('should accept nested normalized filters in $or', () => { |
| 673 | + const filter = { |
| 674 | + $or: [ |
| 675 | + { status: { $eq: 'active' } }, |
| 676 | + { |
| 677 | + $and: [ |
| 678 | + { status: { $eq: 'pending' } }, |
| 679 | + { verified: { $eq: true } } |
| 680 | + ] |
| 681 | + } |
| 682 | + ] |
| 683 | + }; |
| 684 | + |
| 685 | + expect(() => NormalizedFilterSchema.parse(filter)).not.toThrow(); |
| 686 | + }); |
| 687 | + |
| 688 | + it('should accept nested normalized filter in $not', () => { |
| 689 | + const filter = { |
| 690 | + $not: { |
| 691 | + $and: [ |
| 692 | + { deleted: { $eq: true } }, |
| 693 | + { archived: { $eq: true } } |
| 694 | + ] |
| 695 | + } |
| 696 | + }; |
| 697 | + |
| 698 | + expect(() => NormalizedFilterSchema.parse(filter)).not.toThrow(); |
| 699 | + }); |
| 700 | + |
| 701 | + it('should accept complex deeply nested normalized filters', () => { |
| 702 | + const filter = { |
| 703 | + $and: [ |
| 704 | + { active: { $eq: true } }, |
| 705 | + { |
| 706 | + $or: [ |
| 707 | + { type: { $eq: 'premium' } }, |
| 708 | + { |
| 709 | + $and: [ |
| 710 | + { type: { $eq: 'basic' } }, |
| 711 | + { credits: { $gte: 100 } } |
| 712 | + ] |
| 713 | + } |
| 714 | + ] |
| 715 | + } |
| 716 | + ] |
| 717 | + }; |
| 718 | + |
| 719 | + expect(() => NormalizedFilterSchema.parse(filter)).not.toThrow(); |
| 720 | + }); |
| 721 | + |
| 722 | + it('should accept multiple operators in $and', () => { |
| 723 | + const filter = { |
| 724 | + $and: [ |
| 725 | + { age: { $gte: 18 } }, |
| 726 | + { age: { $lte: 65 } }, |
| 727 | + { role: { $in: ['user', 'admin'] } } |
| 728 | + ] |
| 729 | + }; |
| 730 | + |
| 731 | + expect(() => NormalizedFilterSchema.parse(filter)).not.toThrow(); |
| 732 | + }); |
| 733 | + |
| 734 | + it('should accept empty optional operators', () => { |
| 735 | + const filter = {}; |
| 736 | + expect(() => NormalizedFilterSchema.parse(filter)).not.toThrow(); |
| 737 | + }); |
| 738 | + |
| 739 | + it('should accept combination of all logical operators', () => { |
| 740 | + const filter = { |
| 741 | + $and: [ |
| 742 | + { active: { $eq: true } } |
| 743 | + ], |
| 744 | + $or: [ |
| 745 | + { role: { $eq: 'admin' } }, |
| 746 | + { role: { $eq: 'moderator' } } |
| 747 | + ], |
| 748 | + $not: { |
| 749 | + deleted: { $eq: true } |
| 750 | + } |
| 751 | + }; |
| 752 | + |
| 753 | + expect(() => NormalizedFilterSchema.parse(filter)).not.toThrow(); |
| 754 | + }); |
| 755 | +}); |
0 commit comments