diff --git a/src/models.ts b/src/models.ts index 4d9dba2..c5c4f3f 100644 --- a/src/models.ts +++ b/src/models.ts @@ -475,7 +475,7 @@ export interface IAdvancedFilterCondition { export interface IAdvancedFilter extends IFilter { logicalOperator: AdvancedFilterLogicalOperators; - conditions: IAdvancedFilterCondition[]; + conditions?: IAdvancedFilterCondition[]; } export enum FilterType { @@ -846,9 +846,6 @@ export class AdvancedFilter extends Filter { extractedConditions = (conditions as IAdvancedFilterCondition[]); } - if (extractedConditions.length === 0) { - throw new Error(`conditions must be a non-empty array. You passed: ${conditions}`); - } if (extractedConditions.length > 2) { throw new Error(`AdvancedFilters may not have more than two conditions. You passed: ${conditions.length}`); } diff --git a/src/validators/models/filtersValidator.ts b/src/validators/models/filtersValidator.ts index 7c9aa69..1be86ee 100644 --- a/src/validators/models/filtersValidator.ts +++ b/src/validators/models/filtersValidator.ts @@ -246,7 +246,7 @@ export class AdvancedFilterValidator extends FilterValidatorBase { }, { field: "conditions", - validators: [Validators.fieldRequiredValidator, Validators.filterConditionsValidator] + validators: [Validators.filterConditionsValidator] }, { field: "filterType", diff --git a/test/models.spec.ts b/test/models.spec.ts index 8823ef6..d4cc54c 100644 --- a/test/models.spec.ts +++ b/test/models.spec.ts @@ -3159,6 +3159,28 @@ describe("Unit | Filters", () => { expect(filter.toJSON()).toEqual(expectedFilter); }); + it("should output the correct json when toJSON is called for empty advanced filter", () => { + // Arrange + const expectedFilter: models.IAdvancedFilter = { + $schema: "http://powerbi.com/product/schema#advanced", + target: { + table: "a", + column: "b" + }, + conditions: [], + logicalOperator: "And", + filterType: models.FilterType.Advanced + }; + + // Act + const filter = new models.AdvancedFilter( + expectedFilter.target as models.IFilterTarget, + expectedFilter.logicalOperator); + + // Assert + expect(filter.toJSON()).toEqual(expectedFilter); + }); + it("can be constructed using either array form or individual arguments", () => { // Arrange const expectedFilter: models.IAdvancedFilter = {