Skip to content

Commit 8d14b72

Browse files
committed
Comments & README
- refactored WorkflowValidator.validationErrors as errors to keep consistancy with the underlying validator and other validators - added missing comments - fixed README `Add as dependency to your project`, used the scoped name - added imports in README examples - added 'validate parts of a workflow' example in README Signed-off-by: JBBianchi <[email protected]>
1 parent 5316360 commit 8d14b72

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+484
-31
lines changed

README.md

Lines changed: 69 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ npm install && npm run update-code-base && npm run test
2424

2525

2626
### Add as dependency to your project
27-
You can use [npm link](https://docs.npmjs.com/cli/v7/commands/npm-link) to add the `sdk-typescript`
27+
You can use [npm link](https://docs.npmjs.com/cli/v7/commands/npm-link) to add the `@severlessworkflow/sdk-typescript`
2828
as dependency in your project.
2929

3030
- Clone the `sdk-typescript` project and build it:
@@ -34,38 +34,40 @@ cd sdk-typescript
3434
npm install && npm run build
3535
```
3636

37-
- Make the package visible globally to npm. Inside the `sdk-typescript` project folder run:
37+
- Make the package visible globally to npm. Inside the `sdk-typescript\dist` project folder run:
3838
```sh
3939
npm link
4040
```
4141

4242
- Navigate to the folder/project in which you want to use the sdk, and run the following command:
4343
```sh
44-
npm link sdk-typescript
44+
npm link @severlessworkflow/sdk-typescript
4545
```
4646

47-
It will create a symbolic link from globally-installed `sdk-typescript` to `node_modules/` of the current folder.
47+
It will create a symbolic link from globally-installed `@severlessworkflow/sdk-typescript` to `node_modules/` of the current folder.
4848

4949

5050
### How to use
5151

5252
#### Create Workflow using builder API
5353

5454
```typescript
55+
import { workflowBuilder, injectstateBuilder, Specification } from '@severlessworkflow/sdk-typescript';
5556

5657
const workflow: Specification.Workflow = workflowBuilder()
5758
.id("helloworld")
5859
.version("1.0")
5960
.name("Hello World Workflow")
6061
.description("Inject Hello World")
6162
.start("Hello State")
62-
.states([injectstateBuilder()
63-
.name("Hello State")
64-
.data({
65-
"result": "Hello World!"
66-
})
67-
.end(true)
68-
.build()
63+
.states([
64+
injectstateBuilder()
65+
.name("Hello State")
66+
.data({
67+
"result": "Hello World!"
68+
})
69+
.end(true)
70+
.build()
6971
])
7072
.build();
7173
```
@@ -74,6 +76,8 @@ const workflow: Specification.Workflow = workflowBuilder()
7476
#### Create Workflow using object literals
7577

7678
```typescript
79+
import { Specification } from '@severlessworkflow/sdk-typescript';
80+
7781
const workflow: Specification.Workflow = {
7882
id: 'helloworld',
7983
version: '1.0',
@@ -97,7 +101,9 @@ const workflow: Specification.Workflow = {
97101
#### Load a file JSON/YAML to a Workflow instance
98102

99103
```typescript
100-
const workflow = WorkflowConverter.fromString(source)
104+
import { Specification, WorkflowConverter } from '@severlessworkflow/sdk-typescript';
105+
106+
const workflow: Specification.Workflow = WorkflowConverter.fromString(source);
101107
```
102108
Where `source` is a JSON or a YAML string.
103109

@@ -107,19 +113,22 @@ Where `source` is a JSON or a YAML string.
107113
Having the following workflow instance:
108114

109115
```typescript
110-
const workflow = workflowBuilder()
116+
import { workflowBuilder, injectstateBuilder, Specification } from '@severlessworkflow/sdk-typescript';
117+
118+
const workflow: Specification.Workflow = workflowBuilder()
111119
.id("helloworld")
112120
.version("1.0")
113121
.name("Hello World Workflow")
114122
.description("Inject Hello World")
115123
.start("Hello State")
116-
.states([injectstateBuilder()
117-
.name("Hello State")
118-
.data({
119-
"result": "Hello World!"
120-
})
121-
.end(true)
122-
.build()
124+
.states([
125+
injectstateBuilder()
126+
.name("Hello State")
127+
.data({
128+
"result": "Hello World!"
129+
})
130+
.end(true)
131+
.build()
123132
])
124133
.build();
125134
```
@@ -128,11 +137,15 @@ You can convert it to its string representation in JSON or YAML format
128137
by using the static methods `toJson` or `toYaml` respectively:
129138

130139
```typescript
131-
const workflowAsJson = WorkflowConverter.toJson(workflow);
140+
import { WorkflowConverter } from '@severlessworkflow/sdk-typescript';
141+
142+
const workflowAsJson: string = WorkflowConverter.toJson(workflow);
132143
```
133144

134145
```typescript
135-
const workflowAsYaml = WorkflowConverter.toYaml(workflow);
146+
import { WorkflowConverter } from '@severlessworkflow/sdk-typescript';
147+
148+
const workflowAsYaml: string = WorkflowConverter.toYaml(workflow);
136149
```
137150

138151

@@ -145,8 +158,40 @@ The sdk provides a way to validate if a workflow object is compliant with the se
145158
- `validate(): boolean`
146159

147160
```typescript
148-
const workflowValidator = new WorkflowValidator(workflow);
161+
import { WorkflowValidator, Specification } from '@severlessworkflow/sdk-typescript';
162+
163+
const workflow: Specification.Workflow = {
164+
id: 'helloworld',
165+
version: '1.0',
166+
name: 'Hello World Workflow',
167+
description: 'Inject Hello World',
168+
start: 'Hello State',
169+
states: [
170+
{
171+
type: 'inject',
172+
name: 'Hello State',
173+
end: true,
174+
data: {
175+
result: "Hello World!"
176+
}
177+
} as Specification.Injectstate
178+
]
179+
};
180+
const workflowValidator: WorkflowValidator = new WorkflowValidator(workflow);
149181
if (!workflowValidator.validate()) {
150-
workflowValidator.validationErrors.forEach(error => console.error(error.message));
182+
workflowValidator.errors.forEach(error => console.error(error.message));
183+
}
184+
```
185+
186+
You can also validate parts of a workflow using `validators`:
187+
188+
```typescript
189+
import { ValidateFunction } from 'ajv';
190+
import { validators, Specification } from '@severlessworkflow/sdk-typescript';
191+
192+
const injectionState: Specification.Injectstate = workflow.states[0];
193+
const injectionStateValidator: ValidateFunction<Specification.Injectstate> = validators.get('Injectstate');
194+
if (!injectionStateValidator(injectionState)) {
195+
injectionStateValidator.errors.forEach(error => console.error(error.message));
151196
}
152197
```

src/lib/builder.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
/**
2+
* Represents a builder proxy
3+
*/
14
export type Builder<T> = {
25
build: () => T
36
} & {
47
[k in keyof T]-?: (arg: T[k]) => Builder<T>
58
};
69

710
/**
8-
* A builder factory to proxy properties assignations and validate against schema on build
11+
* A factory for builders that proxy properties assignations and validate against schema on build
912
* @param {Function} validatorFn The function used to validated and produce the object on build()
1013
* @returns {Builder} A builder proxy
1114
*/

src/lib/builders/action-builder.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { Builder, builder } from '../builder';
33
import { Specification } from '../definitions';
44
import { validators } from '../validators';
55

6+
/**
7+
* The internal function used by the builder proxy to validate and return its underlying object
8+
* @param {Specification.Action} data The underlying object
9+
* @returns {Specification.Action} The validated underlying object
10+
*/
611
export function actionValidator(data: Specification.Action): (() => Specification.Action) {
712
return () => {
813
const validate = validators.get('Action');
@@ -17,6 +22,10 @@ export function actionValidator(data: Specification.Action): (() => Specificatio
1722
};
1823
}
1924

25+
/**
26+
* A factory to create a builder proxy for the type `Specification.Action`
27+
* @returns {Specification.Action} A builder for `Specification.Action`
28+
*/
2029
export function actionBuilder(): Builder<Specification.Action> {
2130
return builder<Specification.Action>(actionValidator);
2231
}

src/lib/builders/actiondatafilter-builder.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { Builder, builder } from '../builder';
33
import { Specification } from '../definitions';
44
import { validators } from '../validators';
55

6+
/**
7+
* The internal function used by the builder proxy to validate and return its underlying object
8+
* @param {Specification.Actiondatafilter} data The underlying object
9+
* @returns {Specification.Actiondatafilter} The validated underlying object
10+
*/
611
export function actiondatafilterValidator(data: Specification.Actiondatafilter): (() => Specification.Actiondatafilter) {
712
return () => {
813
const validate = validators.get('Actiondatafilter');
@@ -17,6 +22,10 @@ export function actiondatafilterValidator(data: Specification.Actiondatafilter):
1722
};
1823
}
1924

25+
/**
26+
* A factory to create a builder proxy for the type `Specification.Actiondatafilter`
27+
* @returns {Specification.Actiondatafilter} A builder for `Specification.Actiondatafilter`
28+
*/
2029
export function actiondatafilterBuilder(): Builder<Specification.Actiondatafilter> {
2130
return builder<Specification.Actiondatafilter>(actiondatafilterValidator);
2231
}

src/lib/builders/branch-builder.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { Builder, builder } from '../builder';
33
import { Specification } from '../definitions';
44
import { validators } from '../validators';
55

6+
/**
7+
* The internal function used by the builder proxy to validate and return its underlying object
8+
* @param {Specification.Branch} data The underlying object
9+
* @returns {Specification.Branch} The validated underlying object
10+
*/
611
export function branchValidator(data: Specification.Branch): (() => Specification.Branch) {
712
return () => {
813
const validate = validators.get('Branch');
@@ -17,6 +22,10 @@ export function branchValidator(data: Specification.Branch): (() => Specificatio
1722
};
1823
}
1924

25+
/**
26+
* A factory to create a builder proxy for the type `Specification.Branch`
27+
* @returns {Specification.Branch} A builder for `Specification.Branch`
28+
*/
2029
export function branchBuilder(): Builder<Specification.Branch> {
2130
return builder<Specification.Branch>(branchValidator);
2231
}

src/lib/builders/callbackstate-builder.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { Builder, builder } from '../builder';
33
import { Specification } from '../definitions';
44
import { validators } from '../validators';
55

6+
/**
7+
* The internal function used by the builder proxy to validate and return its underlying object
8+
* @param {Specification.Callbackstate} data The underlying object
9+
* @returns {Specification.Callbackstate} The validated underlying object
10+
*/
611
export function callbackstateValidator(data: Specification.Callbackstate): (() => Specification.Callbackstate) {
712
return () => {
813
data.type = 'callback';
@@ -18,6 +23,10 @@ export function callbackstateValidator(data: Specification.Callbackstate): (() =
1823
};
1924
}
2025

26+
/**
27+
* A factory to create a builder proxy for the type `Specification.Callbackstate`
28+
* @returns {Specification.Callbackstate} A builder for `Specification.Callbackstate`
29+
*/
2130
export function callbackstateBuilder(): Builder<Specification.Callbackstate> {
2231
return builder<Specification.Callbackstate>(callbackstateValidator);
2332
}

src/lib/builders/correlation-def-builder.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { Builder, builder } from '../builder';
33
import { Specification } from '../definitions';
44
import { validators } from '../validators';
55

6+
/**
7+
* The internal function used by the builder proxy to validate and return its underlying object
8+
* @param {Specification.CorrelationDef} data The underlying object
9+
* @returns {Specification.CorrelationDef} The validated underlying object
10+
*/
611
export function correlationDefValidator(data: Specification.CorrelationDef): (() => Specification.CorrelationDef) {
712
return () => {
813
const validate = validators.get('CorrelationDef');
@@ -17,6 +22,10 @@ export function correlationDefValidator(data: Specification.CorrelationDef): (()
1722
};
1823
}
1924

25+
/**
26+
* A factory to create a builder proxy for the type `Specification.CorrelationDef`
27+
* @returns {Specification.CorrelationDef} A builder for `Specification.CorrelationDef`
28+
*/
2029
export function correlationDefBuilder(): Builder<Specification.CorrelationDef> {
2130
return builder<Specification.CorrelationDef>(correlationDefValidator);
2231
}

src/lib/builders/crondef-builder.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { Builder, builder } from '../builder';
33
import { Specification } from '../definitions';
44
import { validators } from '../validators';
55

6+
/**
7+
* The internal function used by the builder proxy to validate and return its underlying object
8+
* @param {Specification.Crondef} data The underlying object
9+
* @returns {Specification.Crondef} The validated underlying object
10+
*/
611
export function crondefValidator(data: Specification.Crondef): (() => Specification.Crondef) {
712
return () => {
813
const validate = validators.get('Crondef');
@@ -17,6 +22,10 @@ export function crondefValidator(data: Specification.Crondef): (() => Specificat
1722
};
1823
}
1924

25+
/**
26+
* A factory to create a builder proxy for the type `Specification.Crondef`
27+
* @returns {Specification.Crondef} A builder for `Specification.Crondef`
28+
*/
2029
export function crondefBuilder(): Builder<Specification.Crondef> {
2130
return builder<Specification.Crondef>(crondefValidator);
2231
}

src/lib/builders/databasedswitch-builder.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { Builder, builder } from '../builder';
33
import { Specification } from '../definitions';
44
import { validators } from '../validators';
55

6+
/**
7+
* The internal function used by the builder proxy to validate and return its underlying object
8+
* @param {Specification.Databasedswitch} data The underlying object
9+
* @returns {Specification.Databasedswitch} The validated underlying object
10+
*/
611
export function databasedswitchValidator(data: Specification.Databasedswitch): (() => Specification.Databasedswitch) {
712
return () => {
813
data.type = 'switch';
@@ -18,6 +23,10 @@ export function databasedswitchValidator(data: Specification.Databasedswitch): (
1823
};
1924
}
2025

26+
/**
27+
* A factory to create a builder proxy for the type `Specification.Databasedswitch`
28+
* @returns {Specification.Databasedswitch} A builder for `Specification.Databasedswitch`
29+
*/
2130
export function databasedswitchBuilder(): Builder<Specification.Databasedswitch> {
2231
return builder<Specification.Databasedswitch>(databasedswitchValidator);
2332
}

src/lib/builders/datacondition-builder.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ import { Builder, builder } from '../builder';
33
import { Specification } from '../definitions';
44
import { validators } from '../validators';
55

6+
/**
7+
* The internal function used by the builder proxy to validate and return its underlying object
8+
* @param {Specification.Datacondition} data The underlying object
9+
* @returns {Specification.Datacondition} The validated underlying object
10+
*/
611
export function dataconditionValidator(data: Specification.Datacondition): (() => Specification.Datacondition) {
712
return () => {
813
const validate = validators.get('Datacondition');
@@ -17,6 +22,10 @@ export function dataconditionValidator(data: Specification.Datacondition): (() =
1722
};
1823
}
1924

25+
/**
26+
* A factory to create a builder proxy for the type `Specification.Datacondition`
27+
* @returns {Specification.Datacondition} A builder for `Specification.Datacondition`
28+
*/
2029
export function dataconditionBuilder(): Builder<Specification.Datacondition> {
2130
return builder<Specification.Datacondition>(dataconditionValidator);
2231
}

0 commit comments

Comments
 (0)