Skip to content
Open
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
96 changes: 45 additions & 51 deletions packages/element-templates-json-schema-shared/test/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
const {
forEach,
set
} = require('min-dash');

const chai = require('chai');

const { default: Ajv } = require('ajv');
const AjvErrors = require('ajv-errors');

const { withErrorMessages, withDeprecationWarnings, getDeprecationWarnings } = require('./utils');

module.exports = {
createValidator,
withErrorMessages
createValidator
};

function createValidator(schema, errors) {
function createValidator(schema, errors, deprecations) {

let deprecationWarnings = [];
const ajv = new Ajv({
allErrors: true,
strict: false,
Expand All @@ -23,52 +18,51 @@ function createValidator(schema, errors) {

AjvErrors(ajv);

return ajv.compile(withErrorMessages(schema, errors));
}

function withErrorMessages(schema, errors) {

if (!errors || !errors.length) {
return schema;
}

// clone a new copy
let newSchema = JSON.parse(JSON.stringify(schema));

// set <errorMessage> keyword for given path
forEach(errors, function(error) {
newSchema = setErrorMessage(newSchema, error);
ajv.addKeyword({
keyword: 'isDeprecated',
errors: true,
compile(schema, parentSchema) {
return function(data, dataCtx) {
if (schema) {
deprecationWarnings = [];

// AJV doesn't support real warnings, so this adds a non-strict validation with keyword 'isDeprecated'
const deprecationWarning = {
keyword: 'isDeprecated',

// TODO: schemaPath
dataPath: dataCtx.dataPath,
message: parentSchema.deprecatedWarning || 'This property is deprecated',
};

deprecationWarnings.push({
id: dataCtx.rootData.id,
warningDescription: deprecationWarning,
});

// just return true to not fail validation
return true;
}
return true;
};
}
});

return newSchema;
}

function setErrorMessage(schema, error) {
const {
path,
errorMessage
} = error;

const errorMessagePath = [
...path,
'errorMessage'
];
const validator = ajv.compile(withErrorMessages(withDeprecationWarnings(schema, deprecations), errors));

return set(schema, errorMessagePath, errorMessage);
}
// wrapper function checks for warnings before each validation
const wrappedValidator = function(data, ...args) {

function eqlErrors(chai, utils) {
// Empty deprecation before each validation
deprecationWarnings = [];

const Assertion = chai.Assertion;
const result = validator.call(this, data, ...args);

Assertion.addMethod('eqlErrors', function(expectedErrors, filter) {
wrappedValidator.errors = validator.errors;
wrappedValidator.warnings = getDeprecationWarnings(deprecationWarnings, data);

const actualErrors = this._obj;

// formats the validation errors, so that they can be used directly in the fixture files.
this.eql(expectedErrors,
`Errors from validation do not match expected.\n\tValidation returned this error (you can use it in the fixture):\n\t${JSON.stringify(actualErrors, null, 2).replace(/"([^"]+)":/g, '$1:')}\n`);
});
}
return result;
};

chai.use(eqlErrors);
return wrappedValidator;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
const chai = require('chai');

const {
forEach,
set
} = require('min-dash');

function withErrorMessages(schema, errors) {

if (!errors || !errors.length) {
return schema;
}

// clone a new copy
let newSchema = JSON.parse(JSON.stringify(schema));

// set <errorMessage> keyword for given path
forEach(errors, function(error) {
newSchema = setErrorMessage(newSchema, error);
});

return newSchema;
}

function setErrorMessage(schema, error) {
const {
path,
errorMessage
} = error;

const errorMessagePath = [
...path,
'errorMessage'
];

return set(schema, errorMessagePath, errorMessage);
}


function withDeprecationWarnings(schema, deprecations) {
if (!deprecations || !deprecations.length) {
return schema;
}

// clone a new copy
let newSchema = JSON.parse(JSON.stringify(schema));

// set deprecation warnings for given paths
forEach(deprecations, function(deprecation) {
newSchema = setDeprecationWarning(newSchema, deprecation);
});

return newSchema;
}

function setDeprecationWarning(schema, deprecation) {
const {
path,
warningMessage
} = deprecation;

const deprecationPath = [
...path,
'deprecatedWarning'
];

return set(schema, deprecationPath, warningMessage);
}

function getDeprecationWarnings(deprecationWarnings, data) {
if (deprecationWarnings.length > 0 && data.id === deprecationWarnings[0].id) {
return deprecationWarnings.map(warning => warning.warningDescription);
}
};

function eqlErrors(chai, utils) {

const Assertion = chai.Assertion;

Assertion.addMethod('eqlErrors', function(expectedErrors, filter) {

const actualErrors = this._obj;

// formats the validation errors, so that they can be used directly in the fixture files.
this.eql(expectedErrors,
`Errors from validation do not match expected.\n\tValidation returned this error (you can use it in the fixture):\n\t${JSON.stringify(actualErrors, null, 2).replace(/"([^"]+)":/g, '$1:')}\n`);
});
}

chai.use(eqlErrors);

module.exports = {
withErrorMessages,
withDeprecationWarnings,
getDeprecationWarnings
};

3 changes: 2 additions & 1 deletion packages/zeebe-element-templates-json-schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
"test:integration": "mocha --reporter=spec --recursive test/integration",
"dev": "npm run test -- --watch",
"all": "run-s build test",
"build": "run-s build:error-messages build:schema",
"build": "run-s build:error-messages build:deprecated-warnings build:schema",
"build:error-messages": "node ../../tasks/generate-error-messages.js --input=./src/error-messages.json --output=./resources/error-messages.json",
"build:deprecated-warnings": "node ../../tasks/generate-deprecated-warnings.js --input=./src/deprecated-warnings.json --output=./resources/deprecated-warnings.json",
"build:schema": "node ../../tasks/generate-schema.js --input=./src/schema.json --output=./resources/schema.json",
"prepare": "run-s build"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,45 @@
},
{
"$ref": "./properties/taskSchedule.json"
},
{
"if": {
"properties": {
"type": {
"const": "Hidden"
},
"binding": {
"properties": {
"type": {
"not": {
"const": "zeebe:userTask"
}
}
},
"required": [
"type"
]
}
},
"required": [
"type"
]
},
"then": {
"anyOf": [
{ "required": [ "value" ] },
{ "required": [ "generatedValue" ] },
{
"not": {
"anyOf": [
{ "required": [ "value" ] },
{ "required": [ "generatedValue" ] }
]
},
"isDeprecated": true
}
]
}
}
],
"properties": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"path": [
"definitions",
"properties",
"allOf",
1,
"items",
"allOf",
25,
"then",
"anyOf",
2
],
"warningMessage": "Hidden property must specify either \"value\" or \"generatedValue\""
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export const template = {
'name': 'HiddenProperty',
'id': 'com.camunda.example.HiddenProperty',
'appliesTo': [
'bpmn:Task'
],
'elementType': {
'value': 'bpmn:BusinessRuleTask'
},
'properties': [
{
'type': 'Hidden',
'value': 'decision',
'binding': {
'type': 'zeebe:calledDecision',
'property': 'decisionId'
}
},
{
'type': 'Hidden',
'binding': {
'type': 'zeebe:calledDecision',
'property': 'resultVariable'
}
}
]
};

export const errors = null;

export const warnings = [
{
keyword: 'isDeprecated',
dataPath: '/properties/1',
message: 'Hidden property must specify either "value" or "generatedValue"'
}
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export const template = {
'name': 'Zeebe User Task Hidden Property',
'id': 'com.camunda.example.ZeebeUserTaskHiddenProperty',
'description': 'A template to define a value less hidden property for a Zeebe user task.',
'version': 1,
'appliesTo': [
'bpmn:Task'
],
'elementType': {
'value': 'bpmn:UserTask'
},
'properties': [
{
'type': 'Hidden',
'binding': {
'type': 'zeebe:userTask',
}
}
]
};

export const errors = null;
Loading