Skip to content

Commit b01f98d

Browse files
Copilothuangyiirene
andcommitted
Changes before error encountered
Co-authored-by: huangyiirene <7665279+huangyiirene@users.noreply.github.com>
1 parent c4d9d73 commit b01f98d

1 file changed

Lines changed: 133 additions & 0 deletions

File tree

packages/spec/src/data/filter.test.ts

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
RangeOperatorSchema,
1010
StringOperatorSchema,
1111
SpecialOperatorSchema,
12+
NormalizedFilterSchema,
1213
FILTER_OPERATORS,
1314
LOGICAL_OPERATORS,
1415
ALL_OPERATORS,
@@ -620,3 +621,135 @@ describe('Real-World Use Cases', () => {
620621
expect(() => QueryFilterSchema.parse(filter)).not.toThrow();
621622
});
622623
});
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

Comments
 (0)