Skip to content

Commit

Permalink
add some example type to docs
Browse files Browse the repository at this point in the history
  • Loading branch information
cmawhorter authored Jan 13, 2021
1 parent 834636a commit d3066e6
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@ const parsed = myUrn.build({
});
```

You can also use build to change protocols:

```js
const arn = URN.create(...);
const parsedArn = arn.parse('arn:aws:...');
console.log(arn.build(Object.assign(parsedArn, {
protocol: 'somethingelse'
))); // somethingelse:aws:...
```
### Type: `ParsedUrn = { [key: string]: null | string }`
The key will be the component name and the corresponding urn value.
Expand All @@ -212,6 +222,41 @@ const parsed: ParsedUrn = {
};
```
## Types
Example of types that I use:
```ts
// You'd update this to match your component parts
export interface IParsedUrn {
protocol: null | string;
partition: null | string;
service: null | string;
region: null | string;
account: null | string;
resource: null | string;
}

export interface IPopulatedParsedUrn extends IParsedUrn {
protocol: string;
partition: string;
service: string;
region: string;
account: string;
resource: string;
}

export interface UrnFunctions {
parse(value: unknown): null | IParsedSun;
format(parsed: IParsedSun): null | IParsedSun;
/**
* Requires populated urn or else validation will fail for null values
* @param parsed
*/
validate(parsed: IPopulatedParsedSun): null | string[];
build(parsed: Partial<IParsedSun>): IParsedSun;
}
```
## Custom validation rules example
Using an AWS arn as an example, you would probably want to add some custom validation to ensure `partition`, `region`, `service` are all valid.
Expand Down

0 comments on commit d3066e6

Please sign in to comment.