Skip to content

Commit

Permalink
Merge pull request #5 from ProcessMaker/1026-add-id-validation
Browse files Browse the repository at this point in the history
  • Loading branch information
chipit24 authored Apr 6, 2020
2 parents ef369a9 + e272bbf commit 8553a87
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 2 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = {
'processmaker/gateway-direction': 'error',
'processmaker/call-activity-child-process': 'error',
'processmaker/call-activity-sequence-flow': 'error',
'processmaker/id-required': 'error',
},
},
},
Expand Down
29 changes: 29 additions & 0 deletions rules/id-required.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const { is } = require('bpmnlint-utils');

/**
* A rule that checks the presence of a node ID.
*/
module.exports = function() {
function isNonBpmnType(node) {
return node.$type.startsWith('bpmndi') || node.$type.startsWith('dc');
}

function check(node, reporter) {
if (is(node, 'bpmn:Definitions') || isNonBpmnType(node)) {
return;
}

const nodeId = (node.id || '').trim();

if (nodeId.length === 0) {
reporter.report(null, 'Element is missing ID');
return;
}

if (!nodeId.match(/^[a-zA-Z][^\s][a-zA-Z0-9_-]+$/)) {
reporter.report(node.id, 'Element ID must be a valid QName')
}
}

return { check };
};
13 changes: 13 additions & 0 deletions test-diagrams/id-required.invalid.bpmn
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:pm="http://processmaker.com/BPMN/2.0/Schema.xsd" id="Definitions_03dabax" targetNamespace="http://bpmn.io/schema/bpmn" exporter="ProcessMaker Modeler" exporterVersion="1.0">
<bpmn:process id="Process_1" isExecutable="true">
<bpmn:task id="" name="Form Task" pm:assignment="requester" />
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1">
<bpmndi:BPMNShape id="node_1_di" bpmnElement="">
<dc:Bounds x="340" y="240" width="116" height="76" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>
13 changes: 13 additions & 0 deletions test-diagrams/id-required.invalid2.bpmn
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:pm="http://processmaker.com/BPMN/2.0/Schema.xsd" id="Definitions_03dabax" targetNamespace="http://bpmn.io/schema/bpmn" exporter="ProcessMaker Modeler" exporterVersion="1.0">
<bpmn:process id="Process_1" isExecutable="true">
<bpmn:task id="1" name="Form Task" pm:assignment="requester" />
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1">
<bpmndi:BPMNShape id="node_1_di" bpmnElement="">
<dc:Bounds x="340" y="240" width="116" height="76" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>
13 changes: 13 additions & 0 deletions test-diagrams/id-required.valid.bpmn
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:pm="http://processmaker.com/BPMN/2.0/Schema.xsd" id="Definitions_03dabax" targetNamespace="http://bpmn.io/schema/bpmn" exporter="ProcessMaker Modeler" exporterVersion="1.0">
<bpmn:process id="Process_1" isExecutable="true">
<bpmn:task id="node_1" name="Form Task" pm:assignment="requester" />
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1">
<bpmndi:BPMNShape id="node_1_di" bpmnElement="">
<dc:Bounds x="340" y="240" width="116" height="76" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>
29 changes: 27 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ const RuleTester = require('bpmnlint/lib/testers/rule-tester');

const gatewayDirectionRule = require('./rules/gateway-direction');
const callActivityChildProcessRule = require('./rules/call-activity-child-process');
const callActivitySequenceFlowRules = require('./rules/call-activity-sequence-flow');
const callActivitySequenceFlowRule = require('./rules/call-activity-sequence-flow');
const idRequiredRule = require('./rules/id-required');

RuleTester.verify('gateway-direction', gatewayDirectionRule, {
valid: [
Expand Down Expand Up @@ -63,7 +64,7 @@ RuleTester.verify('call-activity-child-process', callActivityChildProcessRule, {
]
});

RuleTester.verify('call-activity-sequence-flow', callActivitySequenceFlowRules, {
RuleTester.verify('call-activity-sequence-flow', callActivitySequenceFlowRule, {
valid: [
{
moddleElement: readModdle('./test-diagrams/call-activity-sequence-flow.valid.bpmn')
Expand All @@ -79,3 +80,27 @@ RuleTester.verify('call-activity-sequence-flow', callActivitySequenceFlowRules,
}
]
});

RuleTester.verify('id-required', idRequiredRule, {
valid: [
{
moddleElement: readModdle('./test-diagrams/id-required.valid.bpmn')
}
],
invalid: [
{
moddleElement: readModdle('./test-diagrams/id-required.invalid.bpmn'),
report: {
id: null,
message: 'Element is missing ID'
}
},
{
moddleElement: readModdle('./test-diagrams/id-required.invalid2.bpmn'),
report: {
id: '1',
message: 'Element ID must be a valid QName'
}
}
]
});

0 comments on commit 8553a87

Please sign in to comment.