forked from accordproject/concerto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(decoratormanager) add validate method (accordproject#726)
* feat(decoratormanager) add validate method Signed-off-by: Ertugrul Karademir <[email protected]>
- Loading branch information
1 parent
c637f19
commit 030f412
Showing
5 changed files
with
91 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,15 @@ const Serializer = require('./serializer'); | |
const Factory = require('./factory'); | ||
const ModelUtil = require('./modelutil'); | ||
|
||
// Types needed for TypeScript generation. | ||
/* eslint-disable no-unused-vars */ | ||
/* istanbul ignore next */ | ||
if (global === undefined) { | ||
const ModelFile = require('./introspect/modelfile'); | ||
} | ||
/* eslint-enable no-unused-vars */ | ||
|
||
|
||
const DCS_MODEL = `concerto version "^3.0.0" | ||
namespace [email protected] | ||
|
@@ -105,6 +114,39 @@ function isUnversionedNamespaceEqual(modelFile, unversionedNamespace) { | |
* @memberof module:concerto-core | ||
*/ | ||
class DecoratorManager { | ||
|
||
/** | ||
* Structural validation of the decoratorCommandSet against the | ||
* Decorator Command Set model. Note that this only checks the | ||
* structural integrity of the command set, it cannot check | ||
* whether the commands are valid with respect to a model manager. | ||
* Use the options.validateCommands option with decorateModels | ||
* method to perform semantic validation. | ||
* @param {*} decoratorCommandSet the DecoratorCommandSet object | ||
* @param {ModelFile[]} [modelFiles] an optional array of model | ||
* files that are added to the validation model manager returned | ||
* @returns {ModelManager} the model manager created for validation | ||
* @throws {Error} throws an error if the decoratorCommandSet is invalid | ||
*/ | ||
static validate(decoratorCommandSet, modelFiles) { | ||
const validationModelManager = new ModelManager({ | ||
strict: true, | ||
metamodelValidation: true, | ||
addMetamodel: true, | ||
}); | ||
if(modelFiles) { | ||
validationModelManager.addModelFiles(modelFiles); | ||
} | ||
validationModelManager.addCTOModel( | ||
DCS_MODEL, | ||
'[email protected]' | ||
); | ||
const factory = new Factory(validationModelManager); | ||
const serializer = new Serializer(factory, validationModelManager); | ||
serializer.fromJSON(decoratorCommandSet); | ||
return validationModelManager; | ||
} | ||
|
||
/** | ||
* Applies all the decorator commands from the DecoratorCommandSet | ||
* to the ModelManager. | ||
|
@@ -119,19 +161,7 @@ class DecoratorManager { | |
*/ | ||
static decorateModels(modelManager, decoratorCommandSet, options) { | ||
if (options?.validate) { | ||
const validationModelManager = new ModelManager({ | ||
strict: true, | ||
metamodelValidation: true, | ||
addMetamodel: true, | ||
}); | ||
validationModelManager.addModelFiles(modelManager.getModelFiles()); | ||
validationModelManager.addCTOModel( | ||
DCS_MODEL, | ||
'[email protected]' | ||
); | ||
const factory = new Factory(validationModelManager); | ||
const serializer = new Serializer(factory, validationModelManager); | ||
serializer.fromJSON(decoratorCommandSet); | ||
const validationModelManager = DecoratorManager.validate(decoratorCommandSet, modelManager.getModelFiles()); | ||
if (options?.validateCommands) { | ||
decoratorCommandSet.commands.forEach((command) => { | ||
DecoratorManager.validateCommand( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,36 @@ describe('DecoratorManager', () => { | |
}); | ||
}); | ||
|
||
describe('#validate', function() { | ||
it('should support syntax validation', async function() { | ||
const dcs = fs.readFileSync('./test/data/decoratorcommands/web.json', 'utf-8'); | ||
const validationModelManager = DecoratorManager.validate( JSON.parse(dcs)); | ||
validationModelManager.should.not.be.null; | ||
}); | ||
|
||
it('should support syntax validation with model files', async function() { | ||
const testModelManager = new ModelManager({strict:true}); | ||
const modelText = fs.readFileSync('./test/data/decoratorcommands/test.cto', 'utf-8'); | ||
testModelManager.addCTOModel(modelText, 'test.cto'); | ||
const dcs = fs.readFileSync('./test/data/decoratorcommands/web.json', 'utf-8'); | ||
const validationModelManager = DecoratorManager.validate(JSON.parse(dcs), testModelManager.getModelFiles()); | ||
validationModelManager.should.not.be.null; | ||
validationModelManager.getType('[email protected]').should.not.be.null; | ||
}); | ||
|
||
it('should fail syntax validation', async function() { | ||
(() => { | ||
DecoratorManager.validate( { $class: 'invalid' }); | ||
}).should.throw(/Namespace is not defined for type/); | ||
}); | ||
|
||
it('should fail syntax validation', async function() { | ||
(() => { | ||
DecoratorManager.validate( { invalid: true }); | ||
}).should.throw(/Invalid JSON data/); | ||
}); | ||
}); | ||
|
||
describe('#decorateModels', function() { | ||
it('should support no validation', async function() { | ||
const testModelManager = new ModelManager({strict:true}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters