diff --git a/client/package.json b/client/package.json index 2e99c9ec..091cdaab 100644 --- a/client/package.json +++ b/client/package.json @@ -1,6 +1,6 @@ { "name": "gridify-client", - "version": "2.0.1", + "version": "2.0.2", "description": "Client JS library for the dotnet Gridify project", "main": "dist/index.cjs.js", "module": "dist/index.esm.js", diff --git a/client/src/GridifyQueryBuilder.ts b/client/src/GridifyQueryBuilder.ts index fc8b32de..77aa0968 100644 --- a/client/src/GridifyQueryBuilder.ts +++ b/client/src/GridifyQueryBuilder.ts @@ -31,7 +31,7 @@ export class GridifyQueryBuilder { addCondition( field: string, - operator: ConditionalOperator, + operator: ConditionalOperator | string, value: string | number | boolean, caseSensitive: boolean = true, escapeValue: boolean = true @@ -45,6 +45,12 @@ export class GridifyQueryBuilder { filterValue = `${filterValue.toString()}/i`; } + if (typeof operator === "string" && !Object.values(ConditionalOperator).includes(operator as ConditionalOperator)) { + if (!operator.startsWith('#')) { + throw new Error(`Custom operators must start with the '#' character. Received: ${operator}`); + } + } + var filterExpression = `${field.trim()}${operator}${filterValue}`; this.filteringExpressions.push({ value: filterExpression, diff --git a/client/test/GridifyQueryBuilder.test.ts b/client/test/GridifyQueryBuilder.test.ts index eb6525ac..4bea348d 100644 --- a/client/test/GridifyQueryBuilder.test.ts +++ b/client/test/GridifyQueryBuilder.test.ts @@ -53,6 +53,24 @@ describe("GridifyQueryBuilder", () => { }); }); +describe("GridifyQueryBuilder Custom Operator", () => { + it("should allow custom operator that starts with #", () => { + const query = new GridifyQueryBuilder() + .addCondition("age", "#customOp", 30) + .build(); + + expect(query.filter).toEqual("age#customOp30"); + }); + + it("should throw an error for custom operator without # prefix", () => { + expect(() => { + new GridifyQueryBuilder() + .addCondition("age", "customOp", 30) + .build(); + }).toThrow("Custom operators must start with the '#' character. Received: customOp"); + }); +}); + describe("GridifyQueryBuilder Validation", () => { it("should allow balanced parentheses", () => { const query = new GridifyQueryBuilder() @@ -161,3 +179,4 @@ describe("GridifyQueryBuilder Validation", () => { }); }); +