Skip to content

Commit

Permalink
feat(element-templates/Validator): handle scopes as arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Niklas Kiefer authored and fake-join[bot] committed Mar 4, 2021
1 parent 1c2e104 commit 4d93706
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 85 deletions.
53 changes: 37 additions & 16 deletions lib/provider/camunda/element-templates/Validator.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
'use strict';

var isArray = require('lodash/isArray');
var isObject = require('lodash/isObject');
var isArray = require('min-dash').isArray,
isObject = require('min-dash').isObject,
keys = require('min-dash').keys;

var semver = require('semver');

var handleLegacyScopes = require('./util/handleLegacyScopes');

var DROPDOWN_TYPE = 'Dropdown';

var VALID_TYPES = [ 'String', 'Text', 'Boolean', 'Hidden', DROPDOWN_TYPE ];
Expand All @@ -16,7 +20,8 @@ var PROPERTY_TYPE = 'property',
CAMUNDA_OUT_TYPE = 'camunda:out',
CAMUNDA_IN_BUSINESS_KEY_TYPE = 'camunda:in:businessKey',
CAMUNDA_EXECUTION_LISTENER = 'camunda:executionListener',
CAMUNDA_FIELD = 'camunda:field';
CAMUNDA_FIELD = 'camunda:field',
CAMUNDA_CONNECTOR = 'camunda:Connector';

var VALID_BINDING_TYPES = [
PROPERTY_TYPE,
Expand Down Expand Up @@ -155,37 +160,53 @@ function Validator() {
* Validate given scopes and return error (if any).
*
* @param {TemplateDescriptor} template
* @param {ScopesDescriptor} scopes
* @param {ScopesDescriptor|Array<TemplateDescriptor>} scopes
*
* @return {Error} validation error, if any
*/
this._validateScopes = function(template, scopes) {

var err,
scope,
scopeName;
scopeType;

var self = this;

// handle legacy scope descriptor
if (!isArray(scopes)) {

if (!isObject(scopes)) {
return this._logError('invalid scopes, should be scopes={} or scopes=[]', template);
}

if (!isObject(scopes) || isArray(scopes)) {
return this._logError('invalid scopes, should be scopes={}', template);
// only support <camunda:Connector> for legacy scopes
keys(scopes).forEach(function(scope) {
if (scope !== CAMUNDA_CONNECTOR) {
err = self._logError('invalid scope <' + scope + '>, object descriptor is only supported for <' + CAMUNDA_CONNECTOR + '>', template);
}
});
}

for (scopeName in scopes) {
scope = scopes[scopeName];
handleLegacyScopes(scopes).forEach(function(scope) {
scopeType = scope.type;

if (!isObject(scope) || isArray(scope)) {
err = this._logError('invalid scope, should be scope={}', template);
err = self._logError('invalid scope, should be scope={}', template);
}

if (!scopeType) {
err = self._logError('invalid scope, missing type', template);
}

if (!isArray(scope.properties)) {
err = this._logError(
'missing properties=[] in scope <' + scopeName + '>', template
err = self._logError(
'missing properties=[] in scope <' + scopeType + '>', template
);
} else {
if (!this._validateProperties(template, scope.properties)) {
err = new Error('invalid properties in scope <' + scopeName + '>');
if (!self._validateProperties(template, scope.properties)) {
err = new Error('invalid properties in scope <' + scopeType + '>');
}
}
}
});

return err;
};
Expand Down
145 changes: 84 additions & 61 deletions test/spec/provider/camunda/element-templates/ValidatorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,99 +462,122 @@ describe('element-templates - Validator', function() {
});


it('should reject invalid scopes type', function() {
describe('scopes', function() {

// given
var templates = new Validator();
it('should accept scopes as array', function() {

var templateDescriptors = require('./fixtures/error-scopes-invalid');
// given
var templates = new Validator();

// when
templates.addAll(templateDescriptors);
var templateDescriptors = require('./fixtures/scopes-array');

// then
expect(errors(templates)).to.contain('template(id: <foo>, name: <Invalid>): invalid scopes, should be scopes={}');
// when
templates.addAll(templateDescriptors);

expect(valid(templates)).to.be.empty;
});
// then
expect(errors(templates)).to.be.empty;

expect(valid(templates)).to.have.length(1);
});

it('should reject invalid scopes content', function() {

// given
var templates = new Validator();
it('should accept scopes as object descriptor (connectors)', function() {

var templateDescriptors = require('./fixtures/error-scopes-invalid-scope');
// given
var templates = new Validator();

// when
templates.addAll(templateDescriptors);
var templateDescriptors = require('./fixtures/scopes-single-connector');

// then
expect(errors(templates)).to.contain('template(id: <foo>, name: <Invalid>): invalid scope, should be scope={}');
// when
templates.addAll(templateDescriptors);

expect(valid(templates)).to.be.empty;
});
// then
expect(errors(templates)).to.be.empty;

expect(valid(templates)).to.have.length(1);
});

it('should reject missing scope properties', function() {

// given
var templates = new Validator();
it('should reject invalid scopes content', function() {

var templateDescriptors = require('./fixtures/error-scopes-properties-missing');
// given
var templates = new Validator();

// when
templates.addAll(templateDescriptors);
var templateDescriptors = require('./fixtures/error-scopes-invalid-scope');

// then
expect(errors(templates)).to.contain(
'template(id: <foo>, name: <Invalid>): missing properties=[] in scope <camunda:Connector>'
);
// when
templates.addAll(templateDescriptors);

expect(valid(templates)).to.be.empty;
});
// then
expect(errors(templates)).to.contain('template(id: <foo>, name: <Invalid>): invalid scope <properties>, object descriptor is only supported for <camunda:Connector>');

expect(valid(templates)).to.be.empty;
});

it('should reject scope with invalid property', function() {

// given
var templates = new Validator();
it('should reject missing scope properties', function() {

var templateDescriptors = require('./fixtures/error-scopes-property-invalid');
// given
var templates = new Validator();

// when
templates.addAll(templateDescriptors);
var templateDescriptors = require('./fixtures/error-scopes-properties-missing');

// then
expect(errors(templates)).to.eql([
'template(id: <foo>, name: <Invalid>): invalid property type <InvalidType>; must be any of { ' +
'String, Text, Boolean, Hidden, Dropdown ' +
'}',
'template(id: <foo>, name: <Invalid>): invalid property.binding type <alsoInvalid>; must be any of { ' +
'property, camunda:property, camunda:inputParameter, ' +
'camunda:outputParameter, camunda:in, camunda:out, ' +
'camunda:in:businessKey, camunda:executionListener, ' +
'camunda:field ' +
'}'
]);
expect(valid(templates)).to.be.empty;
});
// when
templates.addAll(templateDescriptors);

// then
expect(errors(templates)).to.contain(
'template(id: <foo>, name: <Invalid>): missing properties=[] in scope <camunda:Connector>'
);

it('should accept scopes example template', function() {
expect(valid(templates)).to.be.empty;
});

// given
var templates = new Validator();

var templateDescriptors = require('./fixtures/scopes');
it('should reject missing scope type', function() {

// when
templates.addAll(templateDescriptors);
// given
var templates = new Validator();

// then
expect(errors(templates)).to.be.empty;
var templateDescriptors = require('./fixtures/error-scopes-type-missing');

// when
templates.addAll(templateDescriptors);

// then
expect(errors(templates)).to.contain(
'template(id: <foo>, name: <Invalid>): invalid scope, missing type'
);

expect(valid(templates)).to.be.empty;
});


it('should reject scope with invalid property', function() {

// given
var templates = new Validator();

var templateDescriptors = require('./fixtures/error-scopes-property-invalid');

// when
templates.addAll(templateDescriptors);

// then
expect(errors(templates)).to.eql([
'template(id: <foo>, name: <Invalid>): invalid property type <InvalidType>; must be any of { ' +
'String, Text, Boolean, Hidden, Dropdown ' +
'}',
'template(id: <foo>, name: <Invalid>): invalid property.binding type <alsoInvalid>; must be any of { ' +
'property, camunda:property, camunda:inputParameter, ' +
'camunda:outputParameter, camunda:in, camunda:out, ' +
'camunda:in:businessKey, camunda:executionListener, ' +
'camunda:field ' +
'}'
]);
expect(valid(templates)).to.be.empty;
});

expect(valid(templates)).to.have.length(1);
});


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
"bpmn:UserTask"
],
"properties": [],
"scopes": {
"camunda:Connector": {
"scopes": [
{
"type": "camunda:Connector"
}
}
]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"bpmn:UserTask"
],
"properties": [],
"scopes": {
"camunda:Connector": {
"scopes": [
{
"type": "camunda:Connector",
"properties": [
{
"label": "Label",
Expand All @@ -20,6 +21,6 @@
}
]
}
}
]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
"bpmn:UserTask"
],
"properties": [],
"scopes": []
"scopes": [
{
"properties": []
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"name": "Connector - Array",
"id": "foo",
"appliesTo": [
"bpmn:UserTask"
],
"properties": [],
"scopes": [
{
"type": "camunda:Connector",
"properties": [
{
"label": "ConnectorId",
"type": "String",
"value": "My Connector HTTP - GET",
"binding": {
"type": "property",
"name": "connectorId"
}
}
]
}
]
}
]
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"name": "Invalid",
"name": "Connector",
"id": "foo",
"appliesTo": [
"bpmn:UserTask"
Expand Down

0 comments on commit 4d93706

Please sign in to comment.