aws / aws/aws-cdk

assertions: coarseStringLike

Open
#29,593 3 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/assertions effort/medium feature-request p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the feature

Checking if some string is in specific property JSON string
For example,
```
BucketPolicy01:
Type: AWS::S3::BucketPolicy
Properties:
Bucket:
Ref: Bucket01
PolicyDocument:
Statement:
- Action:
- s3:GetObject
Effect: Allow
Principal:
AWS:
Fn::Join:
- ""
- - "arn:"
- Ref: AWS::Partition
- :iam::${account-id}:user/some-user
...
```
To check this template, we can check as below.
```
template.hasResourceProperties("AWS::S3::BucketPolicy", {
PolicyDocument: {
Statement: [
{
Action: ["s3:GetObject"],
Effect: "Allow",
Principal: Match.coarseStringLike("some-user") // new method
},
],
},
});
```

### Use Case

```
const principal = new Capture();
template.hasResourceProperties("AWS::S3::BucketPolicy", {
PolicyDocument: {
Statement: [
{
Action: ["s3:GetObject"],
Effect: "Allow",
Principal: principal,
},
],
},
});
expect(JSON.stringify(principal.asObject())).toMatch(
"some-user",
);
```

For many cases, it may be useful to check only partial string in JSON string.

### Proposed Solution

I don't dived into assertions module yet, so I don't know this solution is feasible.
However, json utility tools( such as, JSON.stringify, json.dumps, json.Marshal ... ) would be helpful, I thought.

### Other Information

_No response_

### Acknowledgements

- [X] I may be able to implement this feature request
- [ ] This feature might incur a breaking change

### CDK version used

2.124.0

### Environment details (OS name and version, etc.)

Windows WSL2

Contributor guide

Open the contributing guide

Research direction

Start with the assertions module and inspect the existing Match and Capture entry points used with hasResourceProperties. Define how coarseStringLike should match a partial value within a JSON string, then verify the behavior against the AWS::S3::BucketPolicy example and the Capture use case.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
testing
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.