Skip to content

Commit 5937e64

Browse files
authored
feat(QueryBuilder): Make regex validation filter optional (#227)
This pull request updates the `QueryBuilder` module to allow users to optionally skip regex validation when building queries. The most significant change is the introduction of a `validateRegex` flag in the filter type, which, if set to `false`, bypasses regex validation and prevents errors for invalid regex strings. **Feature: Optional regex validation in query builder**
1 parent 4ff2483 commit 5937e64

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

src/modules/QueryBuilder.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,20 @@ describe('QueryBuilder', () => {
831831
})
832832
).toThrow(QueryBuilderErrors.INVALID_REGEX)
833833
})
834+
it('should not throw an error if regex string is invalid if validateRegex is false', () => {
835+
expect(
836+
builder.build({
837+
operator: EvaluationQueryOperatorEnum.REGEX,
838+
value: '[',
839+
field: foobar,
840+
validateRegex: false,
841+
})
842+
).toEqual({
843+
foobar: {
844+
[EvaluationQueryOperatorEnum.REGEX]: '[',
845+
},
846+
})
847+
})
834848
it('simple test', () => {
835849
expect(
836850
builder.build({

src/modules/QueryBuilder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ export class QueryBuilder {
116116
if (typeof filter.value !== 'string')
117117
throw new Error(QueryBuilderErrors.NOT_A_STRING)
118118
// throw error if regex is invalid
119-
if (!isValidRegex(filter.value))
119+
if (filter.validateRegex !== false && !isValidRegex(filter.value))
120120
throw new Error(QueryBuilderErrors.INVALID_REGEX)
121121
return {
122122
[filter.field]: {

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -902,6 +902,7 @@ export type EvaluationFilter = {
902902
field: string
903903
operator: EvaluationQueryOperatorEnum.REGEX
904904
value: string
905+
validateRegex?: boolean
905906
}
906907

907908
export type QueryBuilderQuery =

0 commit comments

Comments
 (0)