Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C8 templates validation #586

Merged
merged 3 commits into from
Feb 24, 2022
Merged
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
20 changes: 13 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
],
"license": "MIT",
"dependencies": {
"@bpmn-io/element-templates-validator": "^0.4.0",
"@bpmn-io/element-templates-validator": "^0.5.0",
"@bpmn-io/extract-process-variables": "^0.4.4",
"array-move": "^3.0.1",
"classnames": "^2.3.1",
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ export { DescriptionProvider as ZeebeDescriptionProvider } from './contextProvid

// hooks
export { useService } from './hooks';

// utils
export { Validator as CloudElementTemplatesValidator } from './provider/cloud-element-templates/Validator';
69 changes: 63 additions & 6 deletions src/provider/cloud-element-templates/Validator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { Validator as BaseValidator } from '../element-templates/Validator';
import {
Validator as BaseValidator,
filteredSchemaErrors,
getSchemaVersion
} from '../element-templates/Validator';

import semver from 'semver';

import {
validateZeebe as validateAgainstSchema,
getZeebeSchemaPackage as getTemplateSchemaPackage,
getZeebeSchemaVersion as getTemplateSchemaVersion
} from '@bpmn-io/element-templates-validator';

const SUPPORTED_SCHEMA_VERSION = getTemplateSchemaVersion();
const SUPPORTED_SCHEMA_PACKAGE = getTemplateSchemaPackage();

/**
* A Camunda Cloud element template validator.
Expand All @@ -10,8 +24,6 @@ export class Validator extends BaseValidator {
}

/**
* TODO(pinussilvestrus): we disable JSON schema validation for now.
*
* Validate given template and return error (if any).
*
* @param {TemplateDescriptor} template
Expand All @@ -22,9 +34,34 @@ export class Validator extends BaseValidator {
let err;

const id = template.id,
version = template.version || '_';
version = template.version || '_',
schema = template.$schema,
schemaVersion = schema && getSchemaVersion(schema);

// (1) $schema attribute defined
if (!schema) {
return this._logError(
'missing $schema attribute.',
template
);
}

if (!this.isSchemaValid(schema)) {
return this._logError(
`unsupported $schema attribute <${ schema }>.`,
template
);
}

// (2) compatibility
if (schemaVersion && (semver.compare(SUPPORTED_SCHEMA_VERSION, schemaVersion) < 0)) {
return this._logError(
`unsupported element template schema version <${ schemaVersion }>. Your installation only supports up to version <${ SUPPORTED_SCHEMA_VERSION }>. Please update your installation`,
template
);
}

// (1) versioning
// (3) versioning
if (this._templatesById[ id ] && this._templatesById[ id ][ version ]) {
if (version === '_') {
return this._logError(`template id <${ id }> already used`, template);
Expand All @@ -33,6 +70,26 @@ export class Validator extends BaseValidator {
}
}

// (4) JSON schema compliance
const validationResult = validateAgainstSchema(template);

const {
errors,
valid
} = validationResult;

if (!valid) {
err = new Error('invalid template');

filteredSchemaErrors(errors).forEach((error) => {
this._logError(error.message, template);
});
}

return err;
}
}

isSchemaValid(schema) {
return schema && schema.includes(SUPPORTED_SCHEMA_PACKAGE);
}
}
4 changes: 2 additions & 2 deletions src/provider/element-templates/Validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class Validator {
*
* @return {String} for example '99.99.99'
*/
function getSchemaVersion(schemaUri) {
export function getSchemaVersion(schemaUri) {
const re = /\d+\.\d+\.\d+/g;

const match = schemaUri.match(re);
Expand All @@ -189,7 +189,7 @@ function getSchemaVersion(schemaUri) {
*
* @return {Array}
*/
function filteredSchemaErrors(schemaErrors) {
export function filteredSchemaErrors(schemaErrors) {
return filter(schemaErrors, (err) => {
const {
dataPath,
Expand Down
Loading