Skip to content

Commit

Permalink
feat(cloud-element-templates): validate against JSON Schema
Browse files Browse the repository at this point in the history
Closes #561
  • Loading branch information
Niklas Kiefer committed Feb 18, 2022
1 parent f1e8d9e commit 72f486b
Show file tree
Hide file tree
Showing 22 changed files with 1,160 additions and 8 deletions.
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) 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
);
}

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

if (!this.isSchemaValid(schema)) {
return this._logError(
`unsupported $schema attribute <${ schema }>.`,
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

0 comments on commit 72f486b

Please sign in to comment.