Skip to content

Commit c8ce243

Browse files
committed
test: added a new test setup to verify for warnings
- less strict warnings
1 parent d147ffb commit c8ce243

File tree

4 files changed

+118
-9
lines changed

4 files changed

+118
-9
lines changed

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

Lines changed: 91 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,62 @@ 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+
// Empty deprecation before each validation
73+
deprecationWarnings = [];
74+
75+
const result = validator.call(this, data, ...args);
76+
77+
wrappedValidator.errors = validator.errors;
78+
wrappedValidator.warnings = getDeprecationWarnings(data);
79+
80+
return result;
81+
};
82+
83+
return wrappedValidator;
2784
}
2885

2986
function withErrorMessages(schema, errors) {
@@ -71,4 +128,34 @@ function eqlErrors(chai, utils) {
71128
});
72129
}
73130

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@
654654
{ "required": [ "generatedValue" ] }
655655
]
656656
},
657-
"deprecated": true
657+
"isDeprecated": true
658658
}
659659
]
660660
}

packages/zeebe-element-templates-json-schema/test/fixtures/hidden-property.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,11 @@ export const template = {
2727
};
2828

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

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

Lines changed: 18 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,27 @@ 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+
59+
// less strict check for warnings
60+
if (expectedWarnings) {
61+
expect(warnings).to.eql(expectedWarnings);
62+
} else if (warnings && warnings.length > 0) {
63+
64+
// log warnings without failing the test
65+
warnings.forEach(x => console.warn('Deprecation warning:', x.message));
66+
}
5367
});
5468
}
5569

0 commit comments

Comments
 (0)