Skip to content

Commit

Permalink
feat(string): add case-sensitive-matching if exist (stringLikeIfExists)
Browse files Browse the repository at this point in the history
  • Loading branch information
roggervalf committed Jul 26, 2021
1 parent d5dfa3c commit 695961d
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 3 deletions.
22 changes: 22 additions & 0 deletions dist/main.es.js

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

2 changes: 1 addition & 1 deletion dist/main.es.js.map

Large diffs are not rendered by default.

22 changes: 22 additions & 0 deletions dist/main.js

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

2 changes: 1 addition & 1 deletion dist/main.js.map

Large diffs are not rendered by default.

53 changes: 52 additions & 1 deletion src/IdentityBasedPolicy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ describe('IdentityBasedPolicy Class', () => {
});
});

describe('when match based on conditions', () => {
describe('when match based on custom conditions', () => {
it('returns true or false', () => {
const conditionResolver = {
greaterThan: (data: number, expected: number): boolean => {
Expand Down Expand Up @@ -342,6 +342,57 @@ describe('IdentityBasedPolicy Class', () => {
});
});

describe('when match based on default conditions', () => {
it('returns true or false', () => {
const policy = new IdentityBasedPolicy({
statements: [
{
resource: 'secrets:*',
action: ['read', 'write']
},
{
resource: ['posts:*'],
action: ['write', 'read', 'update'],
condition: {
stringLikeIfExists: {
'user.id': '12'
}
}
}
]
});

expect(
policy.evaluate({
action: 'read',
resource: 'secrets:123:ultra',
context: { user: { } }
})
).toBe(true);
expect(
policy.evaluate({
action: 'write',
resource: 'posts:ultra',
context: { user: { id: '123', age: 17 } }
})
).toBe(false);
expect(
policy.evaluate({
action: 'read',
resource: 'posts:ultra',
context: { user: { age: 19 } }
})
).toBe(true);
expect(
policy.evaluate({
action: 'read',
resource: 'posts:ultra',
context: { user: { id: '12', age: 19 } }
})
).toBe(true);
});
});

describe('can and cannot', () => {
it('can should return false when not found and true for when matched with allow', () => {
const policy = new IdentityBasedPolicy({
Expand Down
2 changes: 2 additions & 0 deletions src/conditionOperators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { numericNotEquals } from './numeric/numericNotEquals';
import { stringEquals } from './string/stringEquals';
import { stringEqualsIgnoreCase } from './string/stringEqualsIgnoreCase';
import { stringLike } from './string/stringLike';
import { stringLikeIfExists } from './string/stringLikeIfExists';
import { stringNotEquals } from './string/stringNotEquals';
import { stringNotEqualsIgnoreCase } from './string/stringNotEqualsIgnoreCase';

Expand All @@ -34,6 +35,7 @@ export const operators: Record<string, unknown> = {
stringEquals,
stringEqualsIgnoreCase,
stringLike,
stringLikeIfExists,
stringNotEquals,
stringNotEqualsIgnoreCase
};
15 changes: 15 additions & 0 deletions src/conditionOperators/string/stringLikeIfExists.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { stringLikeIfExists } from './stringLikeIfExists';

describe('stringLikeIfExists', () => {
it('returns true', () => {
expect(stringLikeIfExists('newHouse', 'new*')).toBeTruthy;
expect(stringLikeIfExists('topSecret', '*Secret')).toBeTruthy;
expect(stringLikeIfExists(undefined, 'hi')).toBeTruthy;
});

it('returns false', () => {
expect(stringLikeIfExists('NewHouse', 'new*')).toBeFalsy;
expect(stringLikeIfExists('TopSecret', '*Secret')).toBeFalsy;
expect(stringLikeIfExists('hi', 'no')).toBeFalsy;
});
});
24 changes: 24 additions & 0 deletions src/conditionOperators/string/stringLikeIfExists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {stringLike} from './stringLike';

/**
* Case-sensitive matching if exist. The values can include a multi-character match wildcard (*) anywhere in the string.
*
* @since 4.12.0
* @category String
* @param {string} data The value to be compared.
* @param {string} expected The expected value.
* @returns {boolean} Returns `true` if `value` is equal like `expected value` or if it does not exist.
* @example
* ```javascript
* stringLikeIfExists(undefined, 'new*')
* // => true
*
* stringLikeIfExists('House', 'new*')
* // => false
* ```
*/
export function stringLikeIfExists(data: string | undefined, expected: string): boolean {
return data?(
stringLike(data,expected)
):true;
}

0 comments on commit 695961d

Please sign in to comment.