Skip to content

fix: regex method fixed for validation #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .talismanrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fileignoreconfig:
- filename: package-lock.json
checksum: d388091773e9515cd3c0a5b881644775aa7b8233a405642e49133f296a4ceeeb
checksum: c27c6a4629a6b1cec5e01b5db15d0e12646a1250e0cf1292ac58c561e9bc4993
- filename: test/unit/image-transform.spec.ts
checksum: 7beabdd07bd35d620668fcd97e1a303b9cbc40170bf3008a376d75ce0895de2a
- filename: test/utils/mocks.ts
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### Version: 4.4.3
#### Date: November-30-2024
Fix: regex method fixed for validation

### Version: 4.4.2
#### Date: November-16-2024
Fix: Variants reset issue fix on query call
Expand Down
13 changes: 6 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@contentstack/delivery-sdk",
"version": "4.4.2",
"version": "4.4.3",
"type": "module",
"license": "MIT",
"main": "./dist/legacy/index.cjs",
Expand Down Expand Up @@ -36,7 +36,7 @@
"@contentstack/core": "^1.1.3",
"@contentstack/utils": "^1.3.14",
"@types/humps": "^2.0.6",
"axios": "^1.7.7",
"axios": "^1.7.8",
"dotenv": "^16.4.5",
"humps": "^2.0.1",
"path-browserify": "^1.0.1"
Expand Down
12 changes: 5 additions & 7 deletions src/lib/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import { BaseQuery } from './base-query';
import { BaseQueryParameters, QueryOperation, QueryOperator, TaxonomyQueryOperation } from './types';
import { params, queryParams } from './internal-types';

const safePatterns: RegExp[] = [
/^[a-zA-Z0-9_.-]+$/, // Alphanumeric with underscores, periods, and dashes
];

export class Query extends BaseQuery {
private _contentTypeUid?: string;

Expand Down Expand Up @@ -34,10 +30,12 @@ export class Query extends BaseQuery {

// Validate if input matches any of the safe, pre-approved patterns
private isValidRegexPattern(input: string): boolean {
if (!this.isValidAlphanumeric(input)) {
return false;
try {
new RegExp(input); // Try to create a new RegExp object
return true; // No error means it's a valid regex
} catch (e) {
return false; // Error means it's not a valid regex
}
return safePatterns.some(pattern => pattern.test(input));
}

private isValidValue(value: any[]): boolean {
Expand Down
76 changes: 76 additions & 0 deletions test/unit/query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,82 @@ describe('Query class', () => {
expect(mainQuery2._parameters).toHaveProperty('$and', [subQuery1._parameters, subQuery2._parameters]);
});

it('should result in error when regex method is called with invalid regex', async () => {
const regexQuery = getQueryObject(client, 'your-referenced-content-type-uid');
expect(() => regexQuery.regex("fieldUid", "[a-z")).toThrow("Invalid regexPattern: Must be a valid regular expression");
});

it('should add a regex parameter to _parameters when regex method is called with valid regex', () => {
query.regex('fieldUid', '^ABCXYZ123');
expect(query._parameters['fieldUid']).toEqual({ $regex: '^ABCXYZ123' });
});

it('should add a containedIn parameter to _parameters', () => {
query.containedIn('fieldUid', ['value1', 'value2']);
expect(query._parameters['fieldUid']).toEqual({ '$in': ['value1', 'value2'] });
});

it('should add a notContainedIn parameter to _parameters', () => {
query.notContainedIn('fieldUid', ['value1', 'value2']);
expect(query._parameters['fieldUid']).toEqual({ '$nin': ['value1', 'value2'] });
});

it('should add an exists parameter to _parameters', () => {
query.exists('fieldUid');
expect(query._parameters['fieldUid']).toEqual({ '$exists': true });
});

it('should add a notExists parameter to _parameters', () => {
query.notExists('fieldUid');
expect(query._parameters['fieldUid']).toEqual({ '$exists': false });
});

it('should add an equalTo parameter to _parameters', () => {
query.equalTo('fieldUid', 'value');
expect(query._parameters['fieldUid']).toEqual('value');
});

it('should add a notEqualTo parameter to _parameters', () => {
query.notEqualTo('fieldUid', 'value');
expect(query._parameters['fieldUid']).toEqual({ '$ne': 'value' });
});

it('should add a lessThan parameter to _parameters', () => {
query.lessThan('fieldUid', 10);
expect(query._parameters['fieldUid']).toEqual({ '$lt': 10 });
});

it('should add a lessThanOrEqualTo parameter to _parameters', () => {
query.lessThanOrEqualTo('fieldUid', 10);
expect(query._parameters['fieldUid']).toEqual({ '$lte': 10 });
});

it('should add a greaterThan parameter to _parameters', () => {
query.greaterThan('fieldUid', 10);
expect(query._parameters['fieldUid']).toEqual({ '$gt': 10 });
});

it('should add a greaterThanOrEqualTo parameter to _parameters', () => {
query.greaterThanOrEqualTo('fieldUid', 10);
expect(query._parameters['fieldUid']).toEqual({ '$gte': 10 });
});

it('should add a tags parameter to _parameters', () => {
query.tags(['tag1', 'tag2']);
expect(query._parameters['tags']).toEqual(['tag1', 'tag2']);
});

it('should add a search parameter to _queryParams', () => {
query.search('searchKey');
expect(query._queryParams['typeahead']).toEqual('searchKey');
});

it('should provide proper response when find method is called', async () => {
mockClient.onGet(`/content_types/contentTypeUid/entries`).reply(200, entryFindMock);
const returnedValue = await query.find();
expect(returnedValue).toEqual(entryFindMock);
});

it('should provide proper response when find method is called', async () => {
mockClient.onGet(`/content_types/contentTypeUid/entries`).reply(200, entryFindMock);
const returnedValue = await query.find();
Expand Down