@accordproject/concerto-vocabulary 1.2.2-20211215122352

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,11 @@
1
+ Licensed under the Apache License, Version 2.0 (the "License");
2
+ you may not use this file except in compliance with the License.
3
+ You may obtain a copy of the License at
4
+
5
+ http://www.apache.org/licenses/LICENSE-2.0
6
+
7
+ Unless required by applicable law or agreed to in writing, software
8
+ distributed under the License is distributed on an "AS IS" BASIS,
9
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10
+ See the License for the specific language governing permissions and
11
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # Concerto Vocabulary
2
+
3
+ Library to manage vocabulary terms associated with Concerto models. Vocabulary terms are human-readable labels (for a specific locale) for model elements,
4
+
5
+ Terms may be loaded from YAML format files and then validated against a Concerto ModelManager to detect missing and additional terms.
6
+
7
+ # Installation
8
+
9
+ ```
10
+ npm install @accordproject/concerto-vocabulary --save
11
+ ```
12
+
13
+ ## License <a name="license"></a>
14
+ Accord Project source code files are made available under the Apache License, Version 2.0 (Apache-2.0), located in the LICENSE file. Accord Project documentation files are made available under the Creative Commons Attribution 4.0 International License (CC-BY-4.0), available at http://creativecommons.org/licenses/by/4.0/.
15
+
package/index.js ADDED
@@ -0,0 +1,24 @@
1
+ /*
2
+ * Licensed under the Apache License, Version 2.0 (the "License");
3
+ * you may not use this file except in compliance with the License.
4
+ * You may obtain a copy of the License at
5
+ *
6
+ * http://www.apache.org/licenses/LICENSE-2.0
7
+ *
8
+ * Unless required by applicable law or agreed to in writing, software
9
+ * distributed under the License is distributed on an "AS IS" BASIS,
10
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ * See the License for the specific language governing permissions and
12
+ * limitations under the License.
13
+ */
14
+
15
+ 'use strict';
16
+
17
+ /**
18
+ * Concerto Vocabulary module. Concerto is a framework for defining domain
19
+ * specific models.
20
+ *
21
+ * @module concerto-vocabulary
22
+ */
23
+
24
+ module.exports.VocabularyManager = require('./lib/vocabularymanager');
@@ -0,0 +1,120 @@
1
+ /*
2
+ * Licensed under the Apache License, Version 2.0 (the "License");
3
+ * you may not use this file except in compliance with the License.
4
+ * You may obtain a copy of the License at
5
+ *
6
+ * http://www.apache.org/licenses/LICENSE-2.0
7
+ *
8
+ * Unless required by applicable law or agreed to in writing, software
9
+ * distributed under the License is distributed on an "AS IS" BASIS,
10
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ * See the License for the specific language governing permissions and
12
+ * limitations under the License.
13
+ */
14
+
15
+ 'use strict';
16
+
17
+ /**
18
+ * A vocabulary for a concerto model
19
+ * @class
20
+ * @memberof module:concerto-vocabulary
21
+ */
22
+ class Vocabulary {
23
+ /**
24
+ * Create the Vocabulary
25
+ * @constructor
26
+ * @param {VocabularyManager} vocabularyManager - the manager for this vocabulary
27
+ * @param {object} voc - the JSON representation of the vocabulary
28
+ */
29
+ constructor(vocabularyManager, voc) {
30
+ if(!vocabularyManager) {
31
+ throw new Error('VocabularyManager must be specified');
32
+ }
33
+ if(!voc) {
34
+ throw new Error('Vocabulary object must be specified');
35
+ }
36
+
37
+ if(!voc.locale) {
38
+ throw new Error('A vocabulary must specify a locale');
39
+ }
40
+ if(!voc.namespace) {
41
+ throw new Error('A vocabulary must specify a namespace');
42
+ }
43
+
44
+ this.vocabularyManager = vocabularyManager;
45
+ this.content = voc;
46
+ }
47
+
48
+ /**
49
+ * Returns the namespace for the vocabulary
50
+ * @returns {string} the namespace for this vocabulary
51
+ */
52
+ getNamespace() {
53
+ return this.content.namespace;
54
+ }
55
+
56
+ /**
57
+ * Returns the locale for the vocabulary
58
+ * @returns {string} the locale for this vocabulary
59
+ */
60
+ getLocale() {
61
+ return this.content.locale;
62
+ }
63
+
64
+ /**
65
+ * Returns the identifier for the vocabulary, composed of the namespace plus the locale
66
+ * @returns {string} the identifier for this vocabulary
67
+ */
68
+ getIdentifier() {
69
+ return `${this.getNamespace()}/${new Intl.Locale(this.content.locale).toString()}`;
70
+ }
71
+
72
+ /**
73
+ * Gets the term for a concept, enum or property
74
+ * @param {string} declarationName the name of a concept or enum
75
+ * @param {string} [propertyName] the name of a property (optional)
76
+ * @returns {string} the term or null if it does not exist
77
+ */
78
+ getTerm(declarationName, propertyName) {
79
+ const decl = this.content.declarations.find( d => d[declarationName] !== null);
80
+ if(!decl) {
81
+ return null;
82
+ }
83
+ if(!propertyName) {
84
+ return decl[declarationName];
85
+ }
86
+ else {
87
+ const property = decl.properties.find( d => d[propertyName] !== null);
88
+ return property ? property[propertyName] : null;
89
+ }
90
+ }
91
+
92
+ /**
93
+ * Validates a vocabulary against a ModelFile, returning errors for
94
+ * missing and additional terms.
95
+ * @param {ModelFile} modelFile the model file for this vocabulary
96
+ * @returns {*} an object with missingTerms and additionalTerms properties
97
+ */
98
+ validate(modelFile) {
99
+ const result = {
100
+ missingTerms: modelFile.getAllDeclarations().flatMap( d => this.getTerm(d.getName())
101
+ ? d.getOwnProperties().flatMap( p => this.getTerm(d.getName(), p.getName()) ? null : `${d.getName()}.${p.getName()}`)
102
+ : d.getName() ).filter( i => i !== null),
103
+ additionalTerms: this.content.declarations.flatMap( k => modelFile.getLocalType(Object.keys(k)[0])
104
+ ? k.properties.flatMap( p => this.getTerm(Object.keys(k)[0], Object.keys(p)[0]) ? null : `${Object.keys(k)[0]}.${Object.keys(p)[0]}`)
105
+ : Object.keys(k)[0] ).filter( i => i !== null)
106
+ };
107
+
108
+ return result;
109
+ }
110
+
111
+ /**
112
+ * Converts the object to JSON
113
+ * @returns {*} the contens of this vocabulary
114
+ */
115
+ toJSON() {
116
+ return this.content;
117
+ }
118
+ }
119
+
120
+ module.exports = Vocabulary;
@@ -0,0 +1,126 @@
1
+ /*
2
+ * Licensed under the Apache License, Version 2.0 (the "License");
3
+ * you may not use this file except in compliance with the License.
4
+ * You may obtain a copy of the License at
5
+ *
6
+ * http://www.apache.org/licenses/LICENSE-2.0
7
+ *
8
+ * Unless required by applicable law or agreed to in writing, software
9
+ * distributed under the License is distributed on an "AS IS" BASIS,
10
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
+ * See the License for the specific language governing permissions and
12
+ * limitations under the License.
13
+ */
14
+
15
+ 'use strict';
16
+
17
+ const YAML = require('yaml');
18
+
19
+ const Vocabulary = require('./vocabulary');
20
+
21
+ /**
22
+ * A vocabulary manager for concerto models
23
+ * @class
24
+ * @memberof module:concerto-vocabulary
25
+ */
26
+ class VocabularyManager {
27
+ /**
28
+ * Create the VocabularyManager
29
+ * @constructor
30
+ */
31
+ constructor() {
32
+ this.vocabularies = {}; // key is locale, value is a Vocabulary object
33
+ }
34
+
35
+ /**
36
+ * Adds a vocabulary to the vocabulary manager
37
+ * @param {string} contents the YAML string for the vocabulary
38
+ */
39
+ addVocabulary(contents) {
40
+ if(!contents) {
41
+ throw new Error('Vocabulary contents must be specified');
42
+ }
43
+ const voc = new Vocabulary(this, YAML.parse(contents));
44
+ this.vocabularies[voc.getIdentifier()] = voc;
45
+ }
46
+
47
+ /**
48
+ * Gets a vocabulary for a given namespace plus locale
49
+ * @param {string} namespace the namespace for the vocabulary
50
+ * @param {string} locale the locale identifier, as defined via Intl.Locale
51
+ * @returns {Vocabulary} the vocabulary or null if no vocabulary exists for the locale
52
+ */
53
+ getVocabulary(namespace, locale) {
54
+ const loc = new Intl.Locale(locale).toString();
55
+ return Object.values(this.vocabularies).find( v => v.getIdentifier() === `${namespace}/${loc}`);
56
+ }
57
+
58
+ /**
59
+ * Gets all the vocabulary files for a given namespace
60
+ * @param {string} namespace the namespace
61
+ * @returns {Vocabulary[]} the array of vocabularies
62
+ */
63
+ getVocabulariesForNamespace(namespace) {
64
+ return Object.values(this.vocabularies).filter( v => v.getNamespace() === namespace);
65
+ }
66
+
67
+ /**
68
+ * Gets all the vocabulary files for a given locale
69
+ * @param {string} locale the locale
70
+ * @returns {Vocabulary[]} the array of vocabularies
71
+ */
72
+ getVocabulariesForLocale(locale) {
73
+ const loc = new Intl.Locale(locale).toString();
74
+ return Object.values(this.vocabularies).filter( v => v.getLocale() === loc);
75
+ }
76
+
77
+ /**
78
+ * Validates the terms in the vocabulary against the namespaces and declarations
79
+ * within a ModelManager
80
+ * @param {ModelManager} modelManager - the Model Manager
81
+ * @returns {*} the result of validation
82
+ */
83
+ validate(modelManager) {
84
+ // missing vocabularies
85
+ const missingVocabularies = modelManager.getModelFiles()
86
+ .map( m => this.getVocabulariesForNamespace(m.getNamespace()).length === 0 ? m.getNamespace() : null)
87
+ .filter( m => m !== null);
88
+
89
+ // additional vocabularies
90
+ const additionalVocabularies = Object.values(this.vocabularies)
91
+ .filter( v => !modelManager.getModelFile(v.getNamespace()));
92
+
93
+ const result = {
94
+ missingVocabularies,
95
+ additionalVocabularies,
96
+ vocabularies: {}
97
+ };
98
+
99
+ // validate the models against the vocs
100
+ Object.values(this.vocabularies)
101
+ .forEach(voc => {
102
+ const vocResult = {
103
+ locale: voc.getLocale(),
104
+ namespace: voc.getNamespace(),
105
+ missingTerms: [],
106
+ additionalTerms: []
107
+ };
108
+
109
+ const model = modelManager.getModelFile(voc.getNamespace());
110
+ if(model) {
111
+ const errors = voc.validate(model);
112
+ if(errors.missingTerms) {
113
+ vocResult.missingTerms = vocResult.missingTerms.concat(errors.missingTerms);
114
+ }
115
+ if(errors.additionalTerms) {
116
+ vocResult.additionalTerms = vocResult.additionalTerms.concat(errors.additionalTerms);
117
+ }
118
+
119
+ result.vocabularies[`${vocResult.namespace}/${vocResult.locale}`] = vocResult;
120
+ }
121
+ });
122
+ return result;
123
+ }
124
+ }
125
+
126
+ module.exports = VocabularyManager;
package/package.json ADDED
@@ -0,0 +1,114 @@
1
+ {
2
+ "name": "@accordproject/concerto-vocabulary",
3
+ "version": "1.2.2-20211215122352",
4
+ "description": "Associate human-readable text to model declarations",
5
+ "homepage": "https://github.com/accordproject/concerto",
6
+ "engines": {
7
+ "node": ">=12",
8
+ "npm": ">=6"
9
+ },
10
+ "main": "index.js",
11
+ "typings": "types/index.d.ts",
12
+ "scripts": {
13
+ "pretest": "npm run lint",
14
+ "lint": "eslint .",
15
+ "postlint": "npm run licchk",
16
+ "licchk": "license-check-and-add",
17
+ "postlicchk": "npm run doc",
18
+ "doc": "jsdoc --pedantic --recurse -c jsdoc.json",
19
+ "test": "nyc mocha --recursive -t 10000",
20
+ "test:watch": "nyc mocha --watch --recursive -t 10000",
21
+ "mocha": "mocha --recursive -t 10000",
22
+ "nyc": "nyc mocha --recursive -t 10000",
23
+ "build:types": "npx -p typescript tsc ./lib/*.js index.js --declaration --allowJs --emitDeclarationOnly --outDir types"
24
+ },
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "https://github.com/accordproject/concerto.git",
28
+ "directory": "packages/concerto-cto"
29
+ },
30
+ "keywords": [
31
+ "blockchain",
32
+ "hyperledger",
33
+ "solutions"
34
+ ],
35
+ "author": "accordproject.org",
36
+ "license": "Apache-2.0",
37
+ "devDependencies": {
38
+ "chai": "4.3.4",
39
+ "chai-as-promised": "7.1.1",
40
+ "chai-things": "0.2.0",
41
+ "eslint": "8.2.0",
42
+ "jsdoc": "^3.6.7",
43
+ "license-check-and-add": "2.3.6",
44
+ "mocha": "8.3.2",
45
+ "nyc": "15.1.0",
46
+ "sinon": "10.0.0",
47
+ "sinon-chai": "3.6.0"
48
+ },
49
+ "dependencies": {
50
+ "yaml": "2.0.0-9"
51
+ },
52
+ "browserslist": "> 0.25%, not dead",
53
+ "license-check-and-add-config": {
54
+ "folder": "./lib",
55
+ "license": "HEADER",
56
+ "exact_paths_method": "EXCLUDE",
57
+ "exact_paths": [
58
+ "api.txt",
59
+ "composer-logs",
60
+ "coverage",
61
+ "./parser.js",
62
+ "LICENSE",
63
+ "node_modules",
64
+ ".nyc-output",
65
+ "out",
66
+ ".tern-project"
67
+ ],
68
+ "file_type_method": "EXCLUDE",
69
+ "file_types": [
70
+ ".yml",
71
+ ".yaml",
72
+ ".zip",
73
+ ".tgz"
74
+ ],
75
+ "insert_license": false,
76
+ "license_formats": {
77
+ "js|njk|pegjs|cto|acl|qry": {
78
+ "prepend": "/*",
79
+ "append": " */",
80
+ "eachLine": {
81
+ "prepend": " * "
82
+ }
83
+ },
84
+ "npmrc|editorconfig|txt": {
85
+ "eachLine": {
86
+ "prepend": "# "
87
+ }
88
+ },
89
+ "md": {
90
+ "file": "HEADER.md"
91
+ }
92
+ }
93
+ },
94
+ "nyc": {
95
+ "produce-source-map": "true",
96
+ "sourceMap": "inline",
97
+ "reporter": [
98
+ "lcov",
99
+ "text-summary",
100
+ "html",
101
+ "json"
102
+ ],
103
+ "include": [
104
+ "lib/**/*.js"
105
+ ],
106
+ "exclude": [],
107
+ "all": true,
108
+ "check-coverage": true,
109
+ "statements": 54,
110
+ "branches": 41,
111
+ "functions": 70,
112
+ "lines": 55
113
+ }
114
+ }
@@ -0,0 +1 @@
1
+ export var VocabularyManager: typeof import("./lib/vocabularymanager");
@@ -0,0 +1,44 @@
1
+ export = Vocabulary;
2
+ /**
3
+ * A vocabulary for a concerto model
4
+ * @class
5
+ * @memberof module:concerto-vocabulary
6
+ */
7
+ declare class Vocabulary {
8
+ /**
9
+ * Create the Vocabulary
10
+ * @constructor
11
+ * @param {VocabularyManager} vocabularyManager - the manager for this vocabulary
12
+ * @param {object} voc - the JSON representation of the vocabulary
13
+ */
14
+ constructor(vocabularyManager: VocabularyManager, voc: object);
15
+ vocabularyManager: VocabularyManager;
16
+ content: any;
17
+ /**
18
+ * Returns the namespace for the vocabulary
19
+ * @returns {string} the namespace for this vocabulary
20
+ */
21
+ getNamespace(): string;
22
+ /**
23
+ * Returns the locale for the vocabulary
24
+ * @returns {string} the locale for this vocabulary
25
+ */
26
+ getLocale(): string;
27
+ /**
28
+ * Returns the identifier for the vocabulary, composed of the namespace plus the locale
29
+ * @returns {string} the identifier for this vocabulary
30
+ */
31
+ getIdentifier(): string;
32
+ /**
33
+ * Gets the term for a concept, enum or property
34
+ * @param {string} declarationName the name of a concept or enum
35
+ * @param {string} [propertyName] the name of a property (optional)
36
+ * @returns {string} the term or null if it does not exist
37
+ */
38
+ getTerm(declarationName: string, propertyName?: string): string;
39
+ /**
40
+ * Converts the object to JSON
41
+ * @returns {*} the contens of this vocabulary
42
+ */
43
+ toJSON(): any;
44
+ }
@@ -0,0 +1,41 @@
1
+ export = VocabularyManager;
2
+ /**
3
+ * A vocabulary manager for concerto models
4
+ * @class
5
+ * @memberof module:concerto-vocabulary
6
+ */
7
+ declare class VocabularyManager {
8
+ vocabularies: {};
9
+ /**
10
+ * Adds a vocabulary to the vocabulary manager
11
+ * @param {string} contents the YAML string for the vocabulary
12
+ */
13
+ addVocabulary(contents: string): void;
14
+ /**
15
+ * Gets a vocabulary for a given namespace plus locale
16
+ * @param {string} namespace the namespace for the vocabulary
17
+ * @param {string} locale the locale identifier, as defined via Intl.Locale
18
+ * @returns {Vocabulary} the vocabulary or null if no vocabulary exists for the locale
19
+ */
20
+ getVocabulary(namespace: string, locale: string): Vocabulary;
21
+ /**
22
+ * Gets all the vocabulary files for a given namespace
23
+ * @param {string} namespace the namespace
24
+ * @returns {Vocabulary[]} the array of vocabularies
25
+ */
26
+ getVocabulariesForNamespace(namespace: string): Vocabulary[];
27
+ /**
28
+ * Gets all the vocabulary files for a given locale
29
+ * @param {string} locale the locale
30
+ * @returns {Vocabulary[]} the array of vocabularies
31
+ */
32
+ getVocabulariesForLocale(locale: string): Vocabulary[];
33
+ /**
34
+ * Validates the terms in the vocabulary against the namespaces and declarations
35
+ * within a ModelManager
36
+ * @param {ModelManager} modelManager - the Model Manager
37
+ * @returns {*} the result of validation
38
+ */
39
+ validate(modelManager: ModelManager): any;
40
+ }
41
+ import Vocabulary = require("./vocabulary");