Skip to content

Commit 8392211

Browse files
committed
test: added a new test setup to verify for warnings
1 parent 03f3e3b commit 8392211

File tree

5 files changed

+122
-15
lines changed

5 files changed

+122
-15
lines changed

packages/element-templates-json-schema-shared/test/helpers/index.js

Lines changed: 88 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ const AjvErrors = require('ajv-errors');
1010

1111
module.exports = {
1212
createValidator,
13-
withErrorMessages
13+
withErrorMessages,
14+
withDeprecationWarnings
1415
};
1516

16-
function createValidator(schema, errors) {
17+
function createValidator(schema, errors, deprecations) {
1718

19+
let deprecationWarnings = [];
1820
const ajv = new Ajv({
1921
allErrors: true,
2022
strict: false,
@@ -23,7 +25,59 @@ function createValidator(schema, errors) {
2325

2426
AjvErrors(ajv);
2527

26-
return ajv.compile(withErrorMessages(schema, errors));
28+
ajv.addKeyword({
29+
keyword: 'isDeprecated',
30+
errors: true,
31+
compile(schema, parentSchema) {
32+
return function(data, dataCtx) {
33+
if (schema) {
34+
deprecationWarnings = [];
35+
36+
// AJV doesn't support real warnings, so this adds a non-strict validation with keyword 'isDeprecated'
37+
const deprecationWarning = {
38+
keyword: 'isDeprecated',
39+
40+
// TODO: schemaPath
41+
dataPath: dataCtx.dataPath,
42+
message: parentSchema.deprecatedWarning || 'This property is deprecated',
43+
};
44+
45+
deprecationWarnings.push({
46+
id: dataCtx.rootData.id,
47+
warningDescription: deprecationWarning,
48+
});
49+
50+
// just return true to not fail validation
51+
// AJV will not throw an error, but will add the deprecation warning to the
52+
// validator.errors array
53+
return true;
54+
}
55+
return true;
56+
};
57+
}
58+
});
59+
60+
const validator = ajv.compile(withErrorMessages(withDeprecationWarnings(schema, deprecations), errors));
61+
62+
63+
const getDeprecationWarnings = function(data) {
64+
if (deprecationWarnings.length > 0 && data.id === deprecationWarnings[0].id) {
65+
return deprecationWarnings.map(warning => warning.warningDescription);
66+
}
67+
};
68+
69+
// wrapper function checks for warnings before each validation
70+
const wrappedValidator = function(data, ...args) {
71+
72+
const result = validator.call(this, data, ...args);
73+
74+
wrappedValidator.errors = validator.errors;
75+
wrappedValidator.warnings = getDeprecationWarnings(data);
76+
77+
return result;
78+
};
79+
80+
return wrappedValidator;
2781
}
2882

2983
function withErrorMessages(schema, errors) {
@@ -71,4 +125,34 @@ function eqlErrors(chai, utils) {
71125
});
72126
}
73127

74-
chai.use(eqlErrors);
128+
chai.use(eqlErrors);
129+
130+
function withDeprecationWarnings(schema, deprecations) {
131+
if (!deprecations || !deprecations.length) {
132+
return schema;
133+
}
134+
135+
// clone a new copy
136+
let newSchema = JSON.parse(JSON.stringify(schema));
137+
138+
// set deprecation warnings for given paths
139+
forEach(deprecations, function(deprecation) {
140+
newSchema = setDeprecationWarning(newSchema, deprecation);
141+
});
142+
143+
return newSchema;
144+
}
145+
146+
function setDeprecationWarning(schema, deprecation) {
147+
const {
148+
path,
149+
warningMessage
150+
} = deprecation;
151+
152+
const deprecationPath = [
153+
...path,
154+
'deprecatedWarning'
155+
];
156+
157+
return set(schema, deprecationPath, warningMessage);
158+
}

packages/zeebe-element-templates-json-schema/src/defs/properties.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@
646646
{ "required": [ "generatedValue" ] }
647647
]
648648
},
649-
"deprecated": true
649+
"isDeprecated": true
650650
}
651651
]
652652
}

packages/zeebe-element-templates-json-schema/test/fixtures/binding-type/additional-property.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,12 @@ export const template = {
1616
]
1717
};
1818

19-
export const errors = null;
19+
export const errors = null;
20+
21+
export const warnings = [
22+
{
23+
keyword: 'isDeprecated',
24+
dataPath: '/properties/0',
25+
message: 'Hidden property must specify either "value" or "generatedValue"'
26+
}
27+
];
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export const template = {
22
'name': 'HiddenProperty',
3+
'$schema': '../../resources/schema.json',
34
'id': 'com.camunda.example.HiddenProperty',
45
'appliesTo': [
56
'bpmn:Task'
@@ -10,20 +11,28 @@ export const template = {
1011
'properties': [
1112
{
1213
'type': 'Hidden',
13-
'value': 'decision',
1414
'binding': {
15-
'type': 'zeebe:calledDecision',
16-
'property': 'decisionId'
15+
'type': 'property',
16+
'name': 'resultVariable'
1717
}
1818
},
1919
{
2020
'type': 'Hidden',
21+
'value': 'hiddenValue',
2122
'binding': {
22-
'type': 'zeebe:calledDecision',
23-
'property': 'resultVariable'
23+
'type': 'property',
24+
'name': 'resultVariable'
2425
}
2526
}
2627
]
2728
};
2829

2930
export const errors = null;
31+
32+
export const warnings = [
33+
{
34+
keyword: 'isDeprecated',
35+
dataPath: '/properties/0',
36+
message: 'Hidden property must specify either "value" or "generatedValue"'
37+
}
38+
];

packages/zeebe-element-templates-json-schema/test/spec/validationSpec.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ const util = require('util');
55
const schema = require('../../resources/schema.json');
66

77
const errorMessages = require('../../resources/error-messages.json');
8+
const deprecatedWarnings = require('../../resources/deprecated-warnings.json');
89

910
const {
1011
createValidator
1112
} = require('../../../element-templates-json-schema-shared/test/helpers');
1213

13-
const validator = createValidator(schema, errorMessages);
14+
const validator = createValidator(schema, errorMessages, deprecatedWarnings);
1415

1516
// we save this for some other shinanigans
1617
const iit = it;
@@ -20,10 +21,12 @@ function validateTemplate(template) {
2021
const valid = validator(template);
2122

2223
const errors = validator.errors;
24+
const warnings = validator.warnings;
2325

2426
return {
2527
valid,
26-
errors
28+
errors,
29+
warnings
2730
};
2831
}
2932

@@ -40,16 +43,19 @@ function createTest(name, file, it) {
4043

4144
const {
4245
errors: expectedErrors,
43-
template
46+
template,
47+
warnings: expectedWarnings
4448
} = testDefinition;
4549

4650
// when
4751
const {
48-
errors
52+
errors,
53+
warnings
4954
} = validateTemplate(template);
5055

5156
// then
5257
expect(errors).to.eqlErrors(expectedErrors);
58+
expect(warnings).to.eql(expectedWarnings);
5359
});
5460
}
5561

0 commit comments

Comments
 (0)