@accordproject/concerto-vocabulary 5.0.0 → 5.2.0

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.
Files changed (45) hide show
  1. package/dist/esm/chunk-BJEPALVN.mjs +143 -0
  2. package/dist/esm/chunk-BJEPALVN.mjs.map +7 -0
  3. package/dist/esm/chunk-M2X75AXU.mjs +13 -0
  4. package/dist/esm/chunk-M2X75AXU.mjs.map +7 -0
  5. package/dist/esm/{chunk-3VH3UOGB.mjs → chunk-X3NNESEP.mjs} +19 -6
  6. package/dist/esm/chunk-X3NNESEP.mjs.map +7 -0
  7. package/dist/esm/{chunk-7RW3VXN4.mjs → chunk-ZZFBMCTJ.mjs} +5 -3
  8. package/dist/esm/chunk-ZZFBMCTJ.mjs.map +7 -0
  9. package/dist/esm/index.mjs +4 -2
  10. package/dist/esm/vocabulary.mjs +2 -1
  11. package/dist/esm/vocabularymanager.mjs +4 -2
  12. package/dist/esm/vocabularyvalidator.mjs +10 -0
  13. package/dist/esm/vocabularyvalidator.mjs.map +7 -0
  14. package/dist/esm/yamlparser.mjs +1 -0
  15. package/dist/esm-browser/{chunk-DSN2E4MW.mjs → chunk-CSZFRMP7.mjs} +2 -36
  16. package/dist/esm-browser/chunk-CSZFRMP7.mjs.map +7 -0
  17. package/dist/esm-browser/{chunk-CKXQ5OWJ.mjs → chunk-LCSHHQ7B.mjs} +5 -3
  18. package/dist/esm-browser/chunk-LCSHHQ7B.mjs.map +7 -0
  19. package/dist/esm-browser/{chunk-2FE6COGH.mjs → chunk-LF5LTG3B.mjs} +23 -8
  20. package/dist/esm-browser/{chunk-2FE6COGH.mjs.map → chunk-LF5LTG3B.mjs.map} +2 -2
  21. package/dist/esm-browser/chunk-NPUAPXXA.mjs +42 -0
  22. package/dist/esm-browser/chunk-NPUAPXXA.mjs.map +7 -0
  23. package/dist/esm-browser/chunk-PFDH5LIN.mjs +229 -0
  24. package/dist/esm-browser/chunk-PFDH5LIN.mjs.map +7 -0
  25. package/dist/esm-browser/index.mjs +5 -3
  26. package/dist/esm-browser/index.mjs.map +1 -1
  27. package/dist/esm-browser/vocabulary.mjs +1 -1
  28. package/dist/esm-browser/vocabularymanager.mjs +5 -3
  29. package/dist/esm-browser/vocabularyvalidator.mjs +9 -0
  30. package/dist/esm-browser/vocabularyvalidator.mjs.map +7 -0
  31. package/dist/esm-browser/yamlparser.mjs +2 -1
  32. package/dist/vocabulary.d.ts +3 -1
  33. package/dist/vocabulary.js +4 -2
  34. package/dist/vocabulary.js.map +1 -1
  35. package/dist/vocabularymanager.d.ts +4 -2
  36. package/dist/vocabularymanager.js +19 -4
  37. package/dist/vocabularymanager.js.map +1 -1
  38. package/dist/vocabularyvalidator.d.ts +12 -0
  39. package/dist/vocabularyvalidator.js +177 -0
  40. package/dist/vocabularyvalidator.js.map +1 -0
  41. package/package.json +3 -2
  42. package/dist/esm/chunk-3VH3UOGB.mjs.map +0 -7
  43. package/dist/esm/chunk-7RW3VXN4.mjs.map +0 -7
  44. package/dist/esm-browser/chunk-CKXQ5OWJ.mjs.map +0 -7
  45. package/dist/esm-browser/chunk-DSN2E4MW.mjs.map +0 -7
@@ -11,7 +11,7 @@ var Vocabulary = class _Vocabulary {
11
11
  * @param {VocabularyManager} vocabularyManager - the manager for this vocabulary
12
12
  * @param {object} voc - the JSON representation of the vocabulary
13
13
  */
14
- constructor(vocabularyManager, voc) {
14
+ constructor(vocabularyManager, voc, options) {
15
15
  if (!vocabularyManager) {
16
16
  throw new Error("VocabularyManager must be specified");
17
17
  }
@@ -27,7 +27,9 @@ var Vocabulary = class _Vocabulary {
27
27
  if (!voc.locale) {
28
28
  throw new Error("A vocabulary must specify a locale");
29
29
  }
30
- _Vocabulary.validateLocale(voc.locale);
30
+ if (!options?.skipLocaleValidation) {
31
+ _Vocabulary.validateLocale(voc.locale);
32
+ }
31
33
  this.vocabularyManager = vocabularyManager;
32
34
  this.content = voc;
33
35
  }
@@ -184,4 +186,4 @@ export {
184
186
  Vocabulary,
185
187
  vocabulary_default
186
188
  };
187
- //# sourceMappingURL=chunk-CKXQ5OWJ.mjs.map
189
+ //# sourceMappingURL=chunk-LCSHHQ7B.mjs.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/vocabulary.ts"],
4
+ "sourcesContent": ["/*\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { ModelFile, ClassDeclaration, Declaration, Property } from '@accordproject/concerto-core';\nimport type VocabularyManager from './vocabularymanager';\n\n/**\n* A vocabulary for a concerto model\n* @class\n* @memberof module:concerto-vocabulary\n*/\nclass Vocabulary {\n public vocabularyManager: VocabularyManager;\n public content: any;\n\n /**\n * Create the Vocabulary\n * @constructor\n * @param {VocabularyManager} vocabularyManager - the manager for this vocabulary\n * @param {object} voc - the JSON representation of the vocabulary\n */\n constructor(vocabularyManager: VocabularyManager, voc: any, options?: { skipLocaleValidation?: boolean }) {\n if(!vocabularyManager) {\n throw new Error('VocabularyManager must be specified');\n }\n if(!voc) {\n throw new Error('Vocabulary object must be specified');\n }\n\n if(!voc.declarations) {\n throw new Error('Vocabulary object must have declarations');\n }\n\n if(!voc.namespace) {\n throw new Error('A vocabulary must specify a namespace');\n }\n\n if(!voc.locale) {\n throw new Error('A vocabulary must specify a locale');\n }\n\n if (!options?.skipLocaleValidation) {\n Vocabulary.validateLocale(voc.locale);\n }\n\n this.vocabularyManager = vocabularyManager;\n this.content = voc;\n }\n\n /**\n * Returns the namespace for the vocabulary\n * @returns {string} the namespace for this vocabulary\n */\n getNamespace(): string {\n return this.content.namespace;\n }\n\n /**\n * Validates a locale\n * @param {string} locale the locale to validate\n * @throws {Error} if the locale is invalid\n */\n static validateLocale(locale: string) {\n // @ts-ignore\n new Intl.Locale(locale);\n if(locale !== locale.toLowerCase()) {\n throw new Error('Locale should be lowercase with dashes');\n }\n }\n\n /**\n * Returns the locale for the vocabulary\n * @returns {string} the locale for this vocabulary\n */\n getLocale(): string {\n return this.content.locale;\n }\n\n /**\n * Returns the identifier for the vocabulary, composed of the namespace plus the locale\n * @returns {string} the identifier for this vocabulary\n */\n getIdentifier(): string {\n return `${this.getNamespace()}/${this.content.locale}`;\n }\n\n /**\n * Returns all the declarations for this vocabulary\n * @returns {Array} an array of objects\n */\n getTerms(): any[] {\n return this.content.declarations;\n }\n\n /**\n * Gets the terms of Namespace\n * @returns {string} the term or null if it does not exist\n * @private\n */\n getNamespaceTerms(): any {\n const namespaceTerms = Object.entries(this.content).filter(([key]) => key !== 'namespace' && key !== 'locale' && key !== 'declarations');\n return namespaceTerms.length > 0 ? Object.fromEntries(namespaceTerms) : null;\n }\n\n /**\n * Gets the term for a concept, enum or property\n * @param {string} declarationName the name of a concept or enum\n * @param {string} [propertyName] the name of a property (optional)\n * @param {string} [identifier] the identifier of the term (optional)\n * @returns {string|number|boolean|null} the term or null if it does not exist\n */\n getTerm(declarationName?: string, propertyName?: string, identifier?: string): string | number | boolean | null {\n if(!declarationName){\n const namespaceTerms = this.getNamespaceTerms();\n return identifier ? namespaceTerms?.[identifier]:namespaceTerms?.term;\n }\n const decl = this.content.declarations.find((d: any) => Object.keys(d)[0] === declarationName);\n if(!decl) {\n return null;\n }\n if(!propertyName) {\n return identifier ? decl[identifier] : decl[declarationName];\n }\n else {\n const property = decl.properties ? decl.properties.find((d: any) => propertyName in d) : null;\n return property ? identifier ? property[identifier] : property[propertyName] : null;\n }\n }\n\n /**\n * Gets the terms for a concept, enum or property\n * @param {string} declarationName the name of a concept or enum\n * @param {string} [propertyName] the name of a property (optional)\n * @returns {string} the term or null if it does not exist\n */\n getElementTerms(declarationName: string, propertyName?: string): any {\n if(!declarationName){\n return this.getNamespaceTerms();\n }\n const decl = this.content.declarations.find((d: any) => Object.keys(d)[0] === declarationName);\n if(!decl) {\n return null;\n }\n if(!propertyName) {\n return decl;\n }\n else {\n const property = decl.properties ? decl.properties.find((d: any) => propertyName in d) : null;\n return property;\n }\n }\n\n /**\n * Validates a vocabulary against a ModelFile, returning errors for\n * missing and additional terms.\n * @param {ModelFile} modelFile the model file for this vocabulary\n * @returns {*} an object with missingTerms and additionalTerms properties\n */\n validate(modelFile: ModelFile): any {\n const getOwnProperties = (declaration: any) => {\n // ensures we have a valid return, even for scalars and map-declarations\n if(declaration.isMapDeclaration()) {\n return [declaration.getKey(), declaration.getValue()];\n } else {\n return declaration.getOwnProperties?.() ? declaration.getOwnProperties?.() : [];\n }\n };\n\n const getPropertyName = (property: any) => {\n if(property.isKey?.()) {\n return 'KEY';\n } else if(property.isValue?.()) {\n return 'VALUE';\n } else {\n return property.getName();\n }\n };\n\n const checkPropertyExists = (k: any, p: any) => {\n // only reached for keys that getLocalType has already resolved\n const declaration = modelFile.getLocalType(Object.keys(k)[0]) as ClassDeclaration;\n const property = Object.keys(p)[0];\n if(declaration.isMapDeclaration()) {\n if (property === 'KEY') {\n return true;\n } else if(property === 'VALUE') {\n return true;\n } else {\n return false;\n }\n } else {\n return declaration.getOwnProperty(Object.keys(p)[0]);\n }\n };\n\n const result = {\n missingTerms: modelFile.getAllDeclarations().flatMap( (d: Declaration) => this.getTerm(d.getName())\n ? getOwnProperties(d).flatMap( (p: Property) => this.getTerm(d.getName(), getPropertyName(p)) ? null : `${d.getName()}.${getPropertyName(p)}`)\n : d.getName() ).filter( (i: any) => i !== null),\n additionalTerms: this.content.declarations.flatMap( (k: any) => modelFile.getLocalType(Object.keys(k)[0])\n ? Array.isArray(k.properties) ? k.properties.flatMap( (p: any) => checkPropertyExists(k, p) ? null : `${Object.keys(k)[0]}.${Object.keys(p)[0]}`) : null\n : k ).filter( (i: any) => i !== null)\n };\n\n if(!this.content.term){\n result.missingTerms.push('namespace');\n }\n\n return result;\n }\n\n /**\n * Converts the object to JSON\n * @returns {*} the contens of this vocabulary\n */\n toJSON() {\n return this.content;\n }\n}\n\nexport { Vocabulary };\nexport default Vocabulary;\n"],
5
+ "mappings": ";;;;;AAAA;AAsBA,IAAM,aAAN,MAAM,YAAW;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUb,YAAY,mBAAsC,KAAU,SAA8C;AACtG,QAAG,CAAC,mBAAmB;AACnB,YAAM,IAAI,MAAM,qCAAqC;AAAA,IACzD;AACA,QAAG,CAAC,KAAK;AACL,YAAM,IAAI,MAAM,qCAAqC;AAAA,IACzD;AAEA,QAAG,CAAC,IAAI,cAAc;AAClB,YAAM,IAAI,MAAM,0CAA0C;AAAA,IAC9D;AAEA,QAAG,CAAC,IAAI,WAAW;AACf,YAAM,IAAI,MAAM,uCAAuC;AAAA,IAC3D;AAEA,QAAG,CAAC,IAAI,QAAQ;AACZ,YAAM,IAAI,MAAM,oCAAoC;AAAA,IACxD;AAEA,QAAI,CAAC,SAAS,sBAAsB;AAChC,kBAAW,eAAe,IAAI,MAAM;AAAA,IACxC;AAEA,SAAK,oBAAoB;AACzB,SAAK,UAAU;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,eAAuB;AACnB,WAAO,KAAK,QAAQ;AAAA,EACxB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,OAAO,eAAe,QAAgB;AAElC,QAAI,KAAK,OAAO,MAAM;AACtB,QAAG,WAAW,OAAO,YAAY,GAAG;AAChC,YAAM,IAAI,MAAM,wCAAwC;AAAA,IAC5D;AAAA,EACJ;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,YAAoB;AAChB,WAAO,KAAK,QAAQ;AAAA,EACxB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,gBAAwB;AACpB,WAAO,GAAG,KAAK,aAAa,CAAC,IAAI,KAAK,QAAQ,MAAM;AAAA,EACxD;AA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AO,GAAG,EAAE,QAAQ,CAAC,IAAI,gBAAgB,CAAC,CAAC,EAAE,IAC3I,EAAE,QAAQ,CAAE,EAAE,OAAQ,CAAC,MAAW,MAAM,IAAI;AAAA,MAClD,iBAAiB,KAAK,QAAQ,aAAa,QAAS,CAAC,MAAW,UAAU,aAAa,OAAO,KAAK,CAAC,EAAE,CAAC,CAAC,IAClG,MAAM,QAAQ,EAAE,UAAU,IAAI,EAAE,WAAW,QAAS,CAAC,MAAW,oBAAoB,GAAG,CAAC,IAAI,OAAO,GAAG,OAAO,KAAK,CAAC,EAAE,CAAC,CAAC,IAAI,OAAO,KAAK,CAAC,EAAE,CAAC,CAAC,EAAE,IAAI,OAClJ,CAAE,EAAE,OAAQ,CAAC,MAAW,MAAM,IAAI;AAAA,IAC5C;AAEA,QAAG,CAAC,KAAK,QAAQ,MAAK;AAClB,aAAO,aAAa,KAAK,WAAW;AAAA,IACxC;AAEA,WAAO;AAAA,EACX;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,SAAS;AACL,WAAO,KAAK;AAAA,EAChB;AACJ;AAGA,IAAO,qBAAQ;",
6
+ "names": []
7
+ }
@@ -1,10 +1,15 @@
1
1
  import {
2
2
  vocabulary_default
3
- } from "./chunk-CKXQ5OWJ.mjs";
3
+ } from "./chunk-LCSHHQ7B.mjs";
4
+ import {
5
+ validateVocabularyYaml
6
+ } from "./chunk-PFDH5LIN.mjs";
4
7
  import {
5
- browser_default,
6
8
  parseVocabularyYaml
7
- } from "./chunk-DSN2E4MW.mjs";
9
+ } from "./chunk-NPUAPXXA.mjs";
10
+ import {
11
+ browser_default
12
+ } from "./chunk-CSZFRMP7.mjs";
8
13
  import {
9
14
  __commonJS,
10
15
  __toESM,
@@ -1710,14 +1715,16 @@ function camelCaseToSentence(text) {
1710
1715
  var VocabularyManager = class _VocabularyManager {
1711
1716
  /**
1712
1717
  * Create the VocabularyManager
1713
- * @param {*} [options] options to configure vocabulary lookup
1714
- * @param {*} [options.missingTermGenerator] A function to call for missing terms. The function
1718
+ * @param {VocabularyManagerOptions} [options] options to configure vocabulary lookup
1719
+ * @param {Function} [options.missingTermGenerator] A function to call for missing terms. The function
1715
1720
  * should accept namespace, locale, declarationName, propertyName as arguments
1721
+ * @param {boolean} [options.enableVocValidator] When true, validates vocabulary YAML before parsing
1716
1722
  * @constructor
1717
1723
  */
1718
1724
  constructor(options) {
1719
1725
  this.vocabularies = {};
1720
- this.missingTermGenerator = options ? options.missingTermGenerator : null;
1726
+ this.missingTermGenerator = options?.missingTermGenerator ?? null;
1727
+ this.options = options ?? {};
1721
1728
  }
1722
1729
  /**
1723
1730
  * Computes a term in English based on declaration and property name.
@@ -1759,7 +1766,15 @@ var VocabularyManager = class _VocabularyManager {
1759
1766
  if (!contents) {
1760
1767
  throw new Error("Vocabulary contents must be specified");
1761
1768
  }
1762
- const voc = new vocabulary_default(this, options?.enableSafeVocabParsing ? parseVocabularyYaml(contents) : browser_default.parse(contents));
1769
+ if (this.options.enableVocValidator) {
1770
+ const { errors } = validateVocabularyYaml(contents);
1771
+ if (errors.length > 0) {
1772
+ throw new Error(errors.map((e) => e.message).join(", "));
1773
+ }
1774
+ }
1775
+ const useRawParse = this.options.enableVocValidator || !options?.enableSafeVocabParsing;
1776
+ const parsed = useRawParse ? browser_default.parse(contents) : parseVocabularyYaml(contents);
1777
+ const voc = new vocabulary_default(this, parsed, { skipLocaleValidation: !!this.options.enableVocValidator });
1763
1778
  if (this.vocabularies[voc.getIdentifier()]) {
1764
1779
  throw new Error("Vocabulary has already been added.");
1765
1780
  }
@@ -2086,4 +2101,4 @@ export {
2086
2101
  VocabularyManager,
2087
2102
  vocabularymanager_default
2088
2103
  };
2089
- //# sourceMappingURL=chunk-2FE6COGH.mjs.map
2104
+ //# sourceMappingURL=chunk-LF5LTG3B.mjs.map