Skip to content
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
22 changes: 15 additions & 7 deletions package-lock.json

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

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "@serverlessworkflow/sdk",
"version": "1.0.3-alpha1",
"version": "1.0.3-alpha2",
"schemaVersion": "1.0.3",
"supportedDslVersions": ">=1.0.0 <=1.0.3",
"description": "Typescript SDK for Serverless Workflow Specification",
"type": "commonjs",
"main": "./index.cjs",
Expand Down Expand Up @@ -77,12 +78,14 @@
"dependencies": {
"ajv": "^8.20.0",
"ajv-formats": "^3.0.1",
"js-yaml": "^4.1.1"
"js-yaml": "^4.1.1",
"semver": "^7.8.5"
},
"devDependencies": {
"@apidevtools/json-schema-ref-parser": "^15.3.1",
"@arethetypeswrong/cli": "^0.18.2",
"@types/js-yaml": "^4.0.9",
"@types/semver": "^7.7.1",
"@types/yargs": "^17.0.32",
"http-server": "^14.1.1",
"husky": "^9.1.7",
Expand Down
1 change: 1 addition & 0 deletions scripts/prepare-dist-package.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const distPackageJson = {
name: packageJson.name,
version: packageJson.version,
schemaVersion: packageJson.schemaVersion,
supportedDslVersions: packageJson.supportedDslVersions,
description: packageJson.description,
type: packageJson.type,
main: packageJson.main,
Expand Down
10 changes: 7 additions & 3 deletions src/lib/hooks/workflow-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import satisfies from 'semver/functions/satisfies';

import { LifecycleHooks } from '../lifecycle-hooks';
import { Specification } from '../generated/definitions';

import { schemaVersion } from '../../../package.json';
import { supportedDslVersions } from '../../../package.json';

export const WorkflowHooks = {
preValidation(instance) {
if (instance?.document?.dsl !== schemaVersion) {
const dsl = instance?.document?.dsl;
const isSupportedDsl = typeof dsl === 'string' && satisfies(dsl, supportedDslVersions, { includePrerelease: true });

if (!isSupportedDsl) {
throw new Error(
`'Workflow' is invalid - The DSL version of the workflow '${instance?.document?.dsl}' doesn't match the supported version of the SDK '${schemaVersion}'.`,
`'Workflow' is invalid - The DSL version of the workflow '${dsl}' does not saisfy the DSL version range supported by this SDK '${supportedDslVersions}'.`,
);
}
return;
Expand Down
101 changes: 97 additions & 4 deletions tests/validation/workflow-validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import { Classes } from '../../src/lib/generated/classes';
import { validate } from '../../src/lib/validation';

import { schemaVersion } from '../../package.json';
import { schemaVersion, supportedDslVersions } from '../../package.json';

describe('Workflow validation', () => {
it('should be valid', () => {
Expand Down Expand Up @@ -57,8 +57,8 @@ describe('Workflow validation', () => {
expect(test).toThrow(/'Workflow' is invalid/);
});

it('should throw with incompatible DSL version', () => {
const oldVersion = '0.9';
it('should throw with a DSL version below the minimum supported version', () => {
const oldVersion = '0.9.0';
const workflow = new Classes.Workflow({
document: {
dsl: oldVersion,
Expand All @@ -68,7 +68,100 @@ describe('Workflow validation', () => {
},
});
expect(() => workflow.validate()).toThrow(
`The DSL version of the workflow '${oldVersion}' doesn't match the supported version of the SDK '${schemaVersion}'.`,
`The DSL version of the workflow '${oldVersion}' does not saisfy the DSL version range supported by this SDK '${supportedDslVersions}'.`,
);
});

it('should throw with a DSL version newer than the current schemaVersion', () => {
const newerVersion = '3.0.0';
const workflow = new Classes.Workflow({
document: {
dsl: newerVersion,
name: 'test',
version: '1.0.0',
namespace: 'default',
},
});
expect(() => workflow.validate()).toThrow(
`The DSL version of the workflow '${newerVersion}' does not saisfy the DSL version range supported by this SDK '${supportedDslVersions}'.`,
);
});

it('should be valid with an older DSL version than the current schema but still supported', () => {
const oldVersion = '1.0.0';
const workflow = new Classes.Workflow({
document: {
dsl: oldVersion,
name: 'test',
version: '1.0.0',
namespace: 'default',
},
do: [
{
step1: {
set: {
foo: 'bar',
},
},
},
],
});

const result = () => validate('Workflow', workflow);
expect(result).not.toThrow(Error);
});

it('should be valid with a pre-release version within the supported range', () => {
const preReleaseVersion = `${schemaVersion}-alpha`;
const workflow = new Classes.Workflow({
document: {
dsl: preReleaseVersion,
name: 'test',
version: '1.0.0',
namespace: 'default',
},
do: [
{
step1: {
set: {
foo: 'bar',
},
},
},
],
});

const result = () => validate('Workflow', workflow);
expect(result).not.toThrow(Error);
});

it('should throw with a pre-release version below the minimum supported', () => {
const preReleaseVersion = '1.0.0-alpha';
const workflow = new Classes.Workflow({
document: {
dsl: preReleaseVersion,
name: 'test',
version: '1.0.0',
namespace: 'default',
},
});
expect(() => workflow.validate()).toThrow(
`The DSL version of the workflow '${preReleaseVersion}' does not saisfy the DSL version range supported by this SDK '${supportedDslVersions}'.`,
);
});

it('should throw with a pre-release version above the supported range', () => {
const preReleaseVersion = '2.0.0-alpha';
const workflow = new Classes.Workflow({
document: {
dsl: preReleaseVersion,
name: 'test',
version: '1.0.0',
namespace: 'default',
},
});
expect(() => workflow.validate()).toThrow(
`The DSL version of the workflow '${preReleaseVersion}' does not saisfy the DSL version range supported by this SDK '${supportedDslVersions}'.`,
);
});
});
4 changes: 2 additions & 2 deletions tsdown.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default defineConfig([
globalName: 'serverWorkflowSdk',
dts: false,
deps: {
alwaysBundle: [/^ajv(?:\/.*)?$/, /^ajv-formats$/, /^js-yaml$/],
alwaysBundle: [/^ajv(?:\/.*)?$/, /^ajv-formats$/, /^js-yaml$/, /^semver(?:\/.*)?$/],
onlyBundle: false,
},
},
Expand All @@ -35,7 +35,7 @@ export default defineConfig([
dts: false,
minify: true,
deps: {
alwaysBundle: [/^ajv(?:\/.*)?$/, /^ajv-formats$/, /^js-yaml$/],
alwaysBundle: [/^ajv(?:\/.*)?$/, /^ajv-formats$/, /^js-yaml$/, /^semver(?:\/.*)?$/],
onlyBundle: false,
},
outExtensions: () => ({
Expand Down