componentsjs 4.3.0 → 5.0.0-beta.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 (57) hide show
  1. package/bin/compile-config.js +1 -1
  2. package/components/context.jsonld +40 -2
  3. package/index.d.ts +1 -0
  4. package/index.js +1 -0
  5. package/lib/construction/ConfigConstructor.js +10 -6
  6. package/lib/construction/argument/ArgumentConstructorHandlerHash.js +3 -2
  7. package/lib/construction/argument/ArgumentConstructorHandlerList.d.ts +11 -0
  8. package/lib/construction/argument/ArgumentConstructorHandlerList.js +19 -0
  9. package/lib/construction/strategy/ConstructionStrategyCommonJsString.js +3 -0
  10. package/lib/construction/strategy/IConstructionStrategy.d.ts +2 -2
  11. package/lib/loading/ComponentRegistry.d.ts +1 -1
  12. package/lib/loading/ComponentRegistry.js +1 -1
  13. package/lib/loading/ComponentRegistryFinalizer.js +18 -3
  14. package/lib/loading/ComponentsManagerBuilder.js +1 -1
  15. package/lib/loading/ConfigRegistry.d.ts +1 -1
  16. package/lib/preprocess/ConfigPreprocessorComponent.d.ts +1 -1
  17. package/lib/preprocess/ConfigPreprocessorComponent.js +17 -12
  18. package/lib/preprocess/ConfigPreprocessorComponentMapped.d.ts +3 -3
  19. package/lib/preprocess/ConfigPreprocessorComponentMapped.js +8 -11
  20. package/lib/preprocess/ParameterHandler.d.ts +2 -2
  21. package/lib/preprocess/ParameterHandler.js +20 -9
  22. package/lib/preprocess/constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerCollectEntries.d.ts +1 -1
  23. package/lib/preprocess/constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerCollectEntries.js +20 -8
  24. package/lib/preprocess/constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerElements.d.ts +1 -1
  25. package/lib/preprocess/constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerElements.js +7 -4
  26. package/lib/preprocess/constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerFields.d.ts +1 -1
  27. package/lib/preprocess/constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerFields.js +14 -8
  28. package/lib/preprocess/constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerKeyValue.d.ts +3 -3
  29. package/lib/preprocess/constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerKeyValue.js +7 -7
  30. package/lib/preprocess/constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerList.d.ts +1 -1
  31. package/lib/preprocess/constructorargumentsmapping/ConstructorArgumentsElementMappingHandlerList.js +6 -21
  32. package/lib/preprocess/constructorargumentsmapping/IConstructorArgumentsElementMappingHandler.d.ts +1 -1
  33. package/lib/preprocess/constructorargumentsmapping/IConstructorArgumentsMapper.d.ts +2 -2
  34. package/lib/preprocess/parameterproperty/IParameterPropertyHandler.d.ts +2 -2
  35. package/lib/preprocess/parameterproperty/ParameterPropertyHandlerDefault.d.ts +6 -3
  36. package/lib/preprocess/parameterproperty/ParameterPropertyHandlerDefault.js +20 -3
  37. package/lib/preprocess/parameterproperty/ParameterPropertyHandlerDefaultScoped.d.ts +2 -2
  38. package/lib/preprocess/parameterproperty/ParameterPropertyHandlerDefaultScoped.js +16 -5
  39. package/lib/preprocess/parameterproperty/ParameterPropertyHandlerFixed.d.ts +5 -3
  40. package/lib/preprocess/parameterproperty/ParameterPropertyHandlerFixed.js +25 -2
  41. package/lib/preprocess/parameterproperty/ParameterPropertyHandlerLazy.d.ts +2 -2
  42. package/lib/preprocess/parameterproperty/ParameterPropertyHandlerLazy.js +10 -3
  43. package/lib/preprocess/parameterproperty/ParameterPropertyHandlerRange.d.ts +15 -4
  44. package/lib/preprocess/parameterproperty/ParameterPropertyHandlerRange.js +141 -25
  45. package/lib/rdf/Iris.js +24 -24
  46. package/lib/rdf/RdfParser.d.ts +2 -2
  47. package/lib/rdf/RdfParser.js +17 -5
  48. package/lib/rdf/RdfStreamIncluder.d.ts +1 -1
  49. package/lib/rdf/RdfStreamIncluder.js +2 -2
  50. package/lib/util/ErrorResourcesContext.d.ts +3 -3
  51. package/lib/util/ErrorResourcesContext.js +6 -1
  52. package/package.json +10 -10
  53. package/CHANGELOG.md +0 -176
  54. package/lib/preprocess/parameterproperty/ParameterPropertyHandlerRequired.d.ts +0 -11
  55. package/lib/preprocess/parameterproperty/ParameterPropertyHandlerRequired.js +0 -23
  56. package/lib/preprocess/parameterproperty/ParameterPropertyHandlerUnique.d.ts +0 -12
  57. package/lib/preprocess/parameterproperty/ParameterPropertyHandlerUnique.js +0 -34
@@ -27,15 +27,15 @@ class ConstructorArgumentsElementMappingHandlerKeyValue {
27
27
  return this.handleValue(configRoot, constructorArgs, configElement, mapper);
28
28
  }
29
29
  handleKeyValue(configRoot, constructorArgs, configElement, mapper) {
30
- const ret = mapper.objectLoader.createCompactedResource({});
31
- ret.property.key = constructorArgs.property.key;
32
- for (const value of mapper.getParameterValue(configRoot, constructorArgs.property.value || constructorArgs.property.valueRawReference, configElement, Boolean(constructorArgs.property.valueRawReference))) {
33
- ret.properties.value.push(value);
34
- }
35
- return [ret];
30
+ const value = mapper.getParameterValue(configRoot, constructorArgs.property.value || constructorArgs.property.valueRawReference, configElement, Boolean(constructorArgs.property.valueRawReference));
31
+ return mapper.objectLoader.createCompactedResource(Object.assign({ key: constructorArgs.property.key }, value ? { value } : {}));
36
32
  }
37
33
  handleValue(configRoot, constructorArgs, configElement, mapper) {
38
- return mapper.getParameterValue(configRoot, constructorArgs.property.value || constructorArgs.property.valueRawReference, configElement, Boolean(constructorArgs.property.valueRawReference));
34
+ const value = mapper.getParameterValue(configRoot, constructorArgs.property.value || constructorArgs.property.valueRawReference, configElement, Boolean(constructorArgs.property.valueRawReference));
35
+ if (!value) {
36
+ return mapper.objectLoader.createCompactedResource({ undefined: true });
37
+ }
38
+ return value;
39
39
  }
40
40
  }
41
41
  exports.ConstructorArgumentsElementMappingHandlerKeyValue = ConstructorArgumentsElementMappingHandlerKeyValue;
@@ -6,5 +6,5 @@ import type { IConstructorArgumentsMapper } from './IConstructorArgumentsMapper'
6
6
  */
7
7
  export declare class ConstructorArgumentsElementMappingHandlerList implements IConstructorArgumentsElementMappingHandler {
8
8
  canHandle(configRoot: Resource, constructorArgs: Resource, configElement: Resource, mapper: IConstructorArgumentsMapper): boolean;
9
- handle(configRoot: Resource, constructorArgs: Resource, configElement: Resource, mapper: IConstructorArgumentsMapper): Resource[];
9
+ handle(configRoot: Resource, constructorArgs: Resource, configElement: Resource, mapper: IConstructorArgumentsMapper): Resource;
10
10
  }
@@ -9,39 +9,24 @@ class ConstructorArgumentsElementMappingHandlerList {
9
9
  return Boolean(constructorArgs.list);
10
10
  }
11
11
  handle(configRoot, constructorArgs, configElement, mapper) {
12
- var _a;
13
12
  // Recursively handle all field values.
14
13
  const ret = mapper.objectLoader.createCompactedResource({});
15
14
  ret.list = [];
16
15
  for (const argument of constructorArgs.list) {
17
16
  if (argument.property.fields || argument.property.elements) {
18
- for (const mapped of mapper.applyConstructorArgumentsParameters(configRoot, argument, configElement)) {
19
- ret.list.push(mapped);
20
- }
17
+ ret.list.push(mapper.applyConstructorArgumentsParameters(configRoot, argument, configElement));
21
18
  }
22
19
  else {
23
- const mappeds = mapper.getParameterValue(configRoot, argument, configElement, false);
24
- if (mappeds.length > 0) {
25
- if (((_a = mappeds[0].property.unique) === null || _a === void 0 ? void 0 : _a.value) === 'true') {
26
- // Only add a single value if param was unique
27
- ret.list.push(mappeds[0]);
28
- }
29
- else {
30
- // Add all values as an array if param was not unique
31
- ret.list.push(mapper.objectLoader.createCompactedResource({
32
- elements: mappeds.map(value => mapper.objectLoader.createCompactedResource({ value })),
33
- }));
34
- }
20
+ const value = mapper.getParameterValue(configRoot, argument, configElement, false);
21
+ if (value) {
22
+ ret.list.push(value);
35
23
  }
36
24
  else {
37
- // Explicitly pass a single undefined value if no param value was set
38
- ret.list.push(mapper.objectLoader.createCompactedResource({
39
- undefined: true,
40
- }));
25
+ ret.list.push(mapper.objectLoader.createCompactedResource({ undefined: true }));
41
26
  }
42
27
  }
43
28
  }
44
- return [ret];
29
+ return ret;
45
30
  }
46
31
  }
47
32
  exports.ConstructorArgumentsElementMappingHandlerList = ConstructorArgumentsElementMappingHandlerList;
@@ -20,5 +20,5 @@ export interface IConstructorArgumentsElementMappingHandler {
20
20
  * @param configElement Part of the config resource to look for parameter instantiations as predicates.
21
21
  * @param mapper Instance of the constructor arguments mapper that can be used to handle recursive args.
22
22
  */
23
- handle: (configRoot: Resource, constructorArgs: Resource, configElement: Resource, mapper: IConstructorArgumentsMapper) => Resource[];
23
+ handle: (configRoot: Resource, constructorArgs: Resource, configElement: Resource, mapper: IConstructorArgumentsMapper) => Resource;
24
24
  }
@@ -16,7 +16,7 @@ export interface IConstructorArgumentsMapper {
16
16
  * @param constructorArgs Object mapping definition inside the constructor arguments.
17
17
  * @param configElement Part of the config resource to look for parameter instantiations as predicates.
18
18
  */
19
- applyConstructorArgumentsParameters: (configRoot: Resource, constructorArgs: Resource, configElement: Resource) => Resource[];
19
+ applyConstructorArgumentsParameters: (configRoot: Resource, constructorArgs: Resource, configElement: Resource) => Resource;
20
20
  /**
21
21
  * Obtain the value(s) of the given parameter in the given config.
22
22
  * @param configRoot The root config resource that we are working in.
@@ -24,5 +24,5 @@ export interface IConstructorArgumentsMapper {
24
24
  * @param configElement Part of the config resource to look for parameter instantiations as predicates.
25
25
  * @param rawValue If the IRI represents a raw string value instead of a parameter reference.
26
26
  */
27
- getParameterValue: (configRoot: Resource, parameter: Resource, configElement: Resource, rawValue: boolean) => Resource[];
27
+ getParameterValue: (configRoot: Resource, parameter: Resource, configElement: Resource, rawValue: boolean) => Resource | undefined;
28
28
  }
@@ -11,7 +11,7 @@ export interface IParameterPropertyHandler {
11
11
  * @param parameter The parameter resource to get the value for.
12
12
  * @param configElement Part of the config resource to look for parameter instantiations as predicates.
13
13
  */
14
- canHandle: (value: Resource[], configRoot: Resource, parameter: Resource, configElement: Resource) => boolean;
14
+ canHandle: (value: Resource | undefined, configRoot: Resource, parameter: Resource, configElement: Resource) => boolean;
15
15
  /**
16
16
  * Transform the given parameter value.
17
17
  * @param value The current parameter value obtained from the config.
@@ -20,5 +20,5 @@ export interface IParameterPropertyHandler {
20
20
  * @param parameter The parameter resource to get the value for.
21
21
  * @param configElement Part of the config resource to look for parameter instantiations as predicates.
22
22
  */
23
- handle: (value: Resource[], configRoot: Resource, parameter: Resource, configElement: Resource) => Resource[];
23
+ handle: (value: Resource | undefined, configRoot: Resource, parameter: Resource, configElement: Resource) => Resource | undefined;
24
24
  }
@@ -1,9 +1,12 @@
1
- import type { Resource } from 'rdf-object';
1
+ import type { Resource, RdfObjectLoader } from 'rdf-object';
2
2
  import type { IParameterPropertyHandler } from './IParameterPropertyHandler';
3
3
  /**
4
4
  * If no value has been set, its default value will be set.
5
5
  */
6
6
  export declare class ParameterPropertyHandlerDefault implements IParameterPropertyHandler {
7
- canHandle(value: Resource[], configRoot: Resource, parameter: Resource, configElement: Resource): boolean;
8
- handle(value: Resource[], configRoot: Resource, parameter: Resource, configElement: Resource): Resource[];
7
+ private readonly objectLoader;
8
+ constructor(objectLoader: RdfObjectLoader);
9
+ canHandle(value: Resource | undefined, configRoot: Resource, parameter: Resource): boolean;
10
+ handle(value: Resource | undefined, configRoot: Resource, parameter: Resource, configElement: Resource): Resource | undefined;
11
+ protected handleValue(value: Resource, configElement: Resource): Resource;
9
12
  }
@@ -1,15 +1,32 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ParameterPropertyHandlerDefault = void 0;
4
+ const Iris_1 = require("../../rdf/Iris");
5
+ const ErrorResourcesContext_1 = require("../../util/ErrorResourcesContext");
4
6
  /**
5
7
  * If no value has been set, its default value will be set.
6
8
  */
7
9
  class ParameterPropertyHandlerDefault {
8
- canHandle(value, configRoot, parameter, configElement) {
9
- return Boolean(value.length === 0 && parameter.property.default);
10
+ constructor(objectLoader) {
11
+ this.objectLoader = objectLoader;
12
+ }
13
+ canHandle(value, configRoot, parameter) {
14
+ return Boolean(!value && parameter.property.default);
10
15
  }
11
16
  handle(value, configRoot, parameter, configElement) {
12
- value = parameter.properties.default;
17
+ if (parameter.properties.default.length > 1) {
18
+ throw new ErrorResourcesContext_1.ErrorResourcesContext(`Invalid default value for parameter "${parameter.value}": Only one value can be defined, or an RDF list must be provided`, { parameter });
19
+ }
20
+ return parameter.property.default.list ?
21
+ this.objectLoader.createCompactedResource({
22
+ list: parameter.property.default.list.map(subValue => this.handleValue(subValue, configElement)),
23
+ }) :
24
+ this.handleValue(parameter.property.default, configElement);
25
+ }
26
+ handleValue(value, configElement) {
27
+ if (value.type === 'NamedNode' && value.value === Iris_1.IRIS_RDF.subject) {
28
+ value = this.objectLoader.createCompactedResource(`"${configElement.value}"`);
29
+ }
13
30
  return value;
14
31
  }
15
32
  }
@@ -6,6 +6,6 @@ import type { IParameterPropertyHandler } from './IParameterPropertyHandler';
6
6
  export declare class ParameterPropertyHandlerDefaultScoped implements IParameterPropertyHandler {
7
7
  private readonly objectLoader;
8
8
  constructor(objectLoader: RdfObjectLoader);
9
- canHandle(value: Resource[], configRoot: Resource, parameter: Resource, configElement: Resource): boolean;
10
- handle(value: Resource[], configRoot: Resource, parameter: Resource, configElement: Resource): Resource[];
9
+ canHandle(value: Resource | undefined, configRoot: Resource, parameter: Resource): boolean;
10
+ handle(value: Resource | undefined, configRoot: Resource, parameter: Resource, configElement: Resource): Resource | undefined;
11
11
  }
@@ -9,11 +9,11 @@ class ParameterPropertyHandlerDefaultScoped {
9
9
  constructor(objectLoader) {
10
10
  this.objectLoader = objectLoader;
11
11
  }
12
- canHandle(value, configRoot, parameter, configElement) {
13
- return Boolean(value.length === 0 && parameter.property.defaultScoped);
12
+ canHandle(value, configRoot, parameter) {
13
+ return Boolean(!value && parameter.property.defaultScoped);
14
14
  }
15
15
  handle(value, configRoot, parameter, configElement) {
16
- // Multiple scopes can be defined
16
+ let applyingValue;
17
17
  for (const scoped of parameter.properties.defaultScoped) {
18
18
  // Require defaultScope
19
19
  if (!scoped.property.defaultScope) {
@@ -25,13 +25,24 @@ class ParameterPropertyHandlerDefaultScoped {
25
25
  if (!scoped.property.defaultScopedValue) {
26
26
  throw new ErrorResourcesContext_1.ErrorResourcesContext(`Invalid defaultScoped for parameter "${parameter.value}": Missing defaultScopedValue`, { parameter });
27
27
  }
28
+ // Require RDF list or single value
29
+ if (scoped.properties.defaultScopedValue.length > 1) {
30
+ throw new ErrorResourcesContext_1.ErrorResourcesContext(`Invalid defaultScoped value for parameter "${parameter.value}": Only one defaultScopedValue can be defined, or an RDF list must be provided`, { parameter });
31
+ }
28
32
  // Apply the scope if the config is of the required type (also considering sub-types)
29
33
  if (configRoot.isA(scopeType.term)) {
30
- value.push(...scoped.properties.defaultScopedValue);
34
+ applyingValue = !applyingValue ?
35
+ scoped.property.defaultScopedValue :
36
+ this.objectLoader.createCompactedResource({
37
+ list: [
38
+ ...applyingValue.list || [applyingValue],
39
+ ...scoped.property.defaultScopedValue.list || [scoped.property.defaultScopedValue],
40
+ ],
41
+ });
31
42
  }
32
43
  }
33
44
  }
34
- return value;
45
+ return applyingValue;
35
46
  }
36
47
  }
37
48
  exports.ParameterPropertyHandlerDefaultScoped = ParameterPropertyHandlerDefaultScoped;
@@ -1,9 +1,11 @@
1
- import type { Resource } from 'rdf-object';
1
+ import type { Resource, RdfObjectLoader } from 'rdf-object';
2
2
  import type { IParameterPropertyHandler } from './IParameterPropertyHandler';
3
3
  /**
4
4
  * Irrespective of any set values, prepend the parameter's fixed values.
5
5
  */
6
6
  export declare class ParameterPropertyHandlerFixed implements IParameterPropertyHandler {
7
- canHandle(value: Resource[], configRoot: Resource, parameter: Resource, configElement: Resource): boolean;
8
- handle(value: Resource[], configRoot: Resource, parameter: Resource, configElement: Resource): Resource[];
7
+ private readonly objectLoader;
8
+ constructor(objectLoader: RdfObjectLoader);
9
+ canHandle(value: Resource | undefined, configRoot: Resource, parameter: Resource): boolean;
10
+ handle(value: Resource | undefined, configRoot: Resource, parameter: Resource, configElement: Resource): Resource | undefined;
9
11
  }
@@ -1,15 +1,38 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ParameterPropertyHandlerFixed = void 0;
4
+ const ErrorResourcesContext_1 = require("../../util/ErrorResourcesContext");
4
5
  /**
5
6
  * Irrespective of any set values, prepend the parameter's fixed values.
6
7
  */
7
8
  class ParameterPropertyHandlerFixed {
8
- canHandle(value, configRoot, parameter, configElement) {
9
+ constructor(objectLoader) {
10
+ this.objectLoader = objectLoader;
11
+ }
12
+ canHandle(value, configRoot, parameter) {
9
13
  return Boolean(parameter.property.fixed);
10
14
  }
11
15
  handle(value, configRoot, parameter, configElement) {
12
- value.unshift(...parameter.properties.fixed);
16
+ if (parameter.properties.fixed.length > 1) {
17
+ throw new ErrorResourcesContext_1.ErrorResourcesContext(`Invalid fixed value for parameter "${parameter.value}": Only one value can be defined, or an RDF list must be provided`, { parameter });
18
+ }
19
+ if (value) {
20
+ const fixedValues = parameter.property.fixed.list || [parameter.property.fixed];
21
+ if (value.list) {
22
+ value.list.unshift(...fixedValues);
23
+ }
24
+ else {
25
+ value = this.objectLoader.createCompactedResource({
26
+ list: [
27
+ ...fixedValues,
28
+ value,
29
+ ],
30
+ });
31
+ }
32
+ }
33
+ else {
34
+ value = parameter.property.fixed;
35
+ }
13
36
  return value;
14
37
  }
15
38
  }
@@ -5,6 +5,6 @@ import type { IParameterPropertyHandler } from './IParameterPropertyHandler';
5
5
  * make the value inherit this lazy tag so that it can be handled later.
6
6
  */
7
7
  export declare class ParameterPropertyHandlerLazy implements IParameterPropertyHandler {
8
- canHandle(value: Resource[], configRoot: Resource, parameter: Resource, configElement: Resource): boolean;
9
- handle(value: Resource[], configRoot: Resource, parameter: Resource, configElement: Resource): Resource[];
8
+ canHandle(value: Resource | undefined, configRoot: Resource, parameter: Resource): boolean;
9
+ handle(value: Resource | undefined, configRoot: Resource, parameter: Resource, configElement: Resource): Resource | undefined;
10
10
  }
@@ -6,12 +6,19 @@ exports.ParameterPropertyHandlerLazy = void 0;
6
6
  * make the value inherit this lazy tag so that it can be handled later.
7
7
  */
8
8
  class ParameterPropertyHandlerLazy {
9
- canHandle(value, configRoot, parameter, configElement) {
9
+ canHandle(value, configRoot, parameter) {
10
10
  return Boolean(parameter.property.lazy);
11
11
  }
12
12
  handle(value, configRoot, parameter, configElement) {
13
- for (const subValue of value) {
14
- subValue.property.lazy = parameter.property.lazy;
13
+ if (value) {
14
+ if (value.list) {
15
+ for (const subValue of value.list) {
16
+ subValue.property.lazy = parameter.property.lazy;
17
+ }
18
+ }
19
+ else {
20
+ value.property.lazy = parameter.property.lazy;
21
+ }
15
22
  }
16
23
  return value;
17
24
  }
@@ -6,8 +6,8 @@ import type { IParameterPropertyHandler } from './IParameterPropertyHandler';
6
6
  export declare class ParameterPropertyHandlerRange implements IParameterPropertyHandler {
7
7
  private readonly objectLoader;
8
8
  constructor(objectLoader: RdfObjectLoader);
9
- canHandle(value: Resource[], configRoot: Resource, parameter: Resource, configElement: Resource): boolean;
10
- handle(value: Resource[], configRoot: Resource, parameter: Resource, configElement: Resource): Resource[];
9
+ canHandle(value: Resource | undefined, configRoot: Resource, parameter: Resource): boolean;
10
+ handle(value: Resource | undefined, configRoot: Resource, parameter: Resource, configElement: Resource): Resource | undefined;
11
11
  /**
12
12
  * Apply the given datatype to the given literal.
13
13
  * Checks if the datatype is correct and casts to the correct js type.
@@ -16,6 +16,17 @@ export declare class ParameterPropertyHandlerRange implements IParameterProperty
16
16
  * @param value The value.
17
17
  * @param param The parameter.
18
18
  */
19
- captureType(value: Resource, param: Resource): Resource;
20
- protected throwIncorrectTypeError(value: Resource, parameter: Resource): void;
19
+ captureType(value: Resource | undefined, param: Resource): Resource | undefined;
20
+ /**
21
+ * Apply the given datatype to the given literal.
22
+ * Checks if the datatype is correct and casts to the correct js type.
23
+ * Will throw an error if the type has an invalid value.
24
+ * Will be ignored if the value is not a literal or the type is not recognized.
25
+ * @param value The value.
26
+ * @param param The parameter.
27
+ * @param paramRange The parameter's range.
28
+ */
29
+ hasParamValueValidType(value: Resource | undefined, param: Resource, paramRange: Resource): boolean;
30
+ protected throwIncorrectTypeError(value: Resource | undefined, parameter: Resource): never;
31
+ rangeToDisplayString(paramRange: Resource | undefined): string;
21
32
  }
@@ -10,13 +10,11 @@ class ParameterPropertyHandlerRange {
10
10
  constructor(objectLoader) {
11
11
  this.objectLoader = objectLoader;
12
12
  }
13
- canHandle(value, configRoot, parameter, configElement) {
13
+ canHandle(value, configRoot, parameter) {
14
14
  return Boolean(parameter.property.range);
15
15
  }
16
16
  handle(value, configRoot, parameter, configElement) {
17
- for (const subValue of value) {
18
- this.captureType(subValue, parameter);
19
- }
17
+ this.captureType(value, parameter);
20
18
  return value;
21
19
  }
22
20
  /**
@@ -28,9 +26,32 @@ class ParameterPropertyHandlerRange {
28
26
  * @param param The parameter.
29
27
  */
30
28
  captureType(value, param) {
31
- if (value.type === 'Literal') {
29
+ if (this.hasParamValueValidType(value, param, param.property.range)) {
30
+ return value;
31
+ }
32
+ this.throwIncorrectTypeError(value, param);
33
+ }
34
+ /**
35
+ * Apply the given datatype to the given literal.
36
+ * Checks if the datatype is correct and casts to the correct js type.
37
+ * Will throw an error if the type has an invalid value.
38
+ * Will be ignored if the value is not a literal or the type is not recognized.
39
+ * @param value The value.
40
+ * @param param The parameter.
41
+ * @param paramRange The parameter's range.
42
+ */
43
+ hasParamValueValidType(value, param, paramRange) {
44
+ if (!paramRange) {
45
+ return true;
46
+ }
47
+ if (!value && paramRange.isA('ParameterRangeUndefined')) {
48
+ return true;
49
+ }
50
+ if (value && value.type === 'Literal') {
32
51
  let parsed;
33
- switch (param.property.range.value) {
52
+ switch (paramRange.value) {
53
+ case Iris_1.IRIS_XSD.string:
54
+ return true;
34
55
  case Iris_1.IRIS_XSD.boolean:
35
56
  if (value.value === 'true') {
36
57
  value.term.valueRaw = true;
@@ -39,9 +60,9 @@ class ParameterPropertyHandlerRange {
39
60
  value.term.valueRaw = false;
40
61
  }
41
62
  else {
42
- this.throwIncorrectTypeError(value, param);
63
+ return false;
43
64
  }
44
- break;
65
+ return true;
45
66
  case Iris_1.IRIS_XSD.integer:
46
67
  case Iris_1.IRIS_XSD.number:
47
68
  case Iris_1.IRIS_XSD.int:
@@ -49,45 +70,140 @@ class ParameterPropertyHandlerRange {
49
70
  case Iris_1.IRIS_XSD.long:
50
71
  parsed = Number.parseInt(value.value, 10);
51
72
  if (Number.isNaN(parsed)) {
52
- this.throwIncorrectTypeError(value, param);
73
+ return false;
53
74
  }
54
- else {
55
- // ParseInt also parses floats to ints!
56
- if (String(parsed) !== value.value) {
57
- this.throwIncorrectTypeError(value, param);
58
- }
59
- value.term.valueRaw = parsed;
75
+ // ParseInt also parses floats to ints!
76
+ if (String(parsed) !== value.value) {
77
+ return false;
60
78
  }
61
- break;
79
+ value.term.valueRaw = parsed;
80
+ return true;
62
81
  case Iris_1.IRIS_XSD.float:
63
82
  case Iris_1.IRIS_XSD.decimal:
64
83
  case Iris_1.IRIS_XSD.double:
65
84
  parsed = Number.parseFloat(value.value);
66
85
  if (Number.isNaN(parsed)) {
67
- this.throwIncorrectTypeError(value, param);
86
+ return false;
68
87
  }
69
- else {
70
- value.term.valueRaw = parsed;
71
- }
72
- break;
88
+ value.term.valueRaw = parsed;
89
+ return true;
73
90
  case Iris_1.IRIS_RDF.JSON:
74
91
  try {
75
92
  parsed = JSON.parse(value.value);
76
93
  value.term.valueRaw = parsed;
77
94
  }
78
95
  catch (_a) {
79
- this.throwIncorrectTypeError(value, param);
96
+ return false;
80
97
  }
81
- break;
98
+ return true;
82
99
  }
83
100
  }
84
- return value;
101
+ // Allow IRIs to be casted to strings
102
+ if (value && paramRange && paramRange.value === Iris_1.IRIS_XSD.string && value.type === 'NamedNode') {
103
+ return true;
104
+ }
105
+ if (paramRange && (!value || (!value.isA('Variable') && !value.isA(paramRange.term)))) {
106
+ if (value && paramRange.isA('ParameterRangeArray')) {
107
+ if (!value.list) {
108
+ return false;
109
+ }
110
+ return value.list.every(listElement => this
111
+ .hasParamValueValidType(listElement, param, paramRange.property.parameterRangeValue));
112
+ }
113
+ // Check if the param type is a composed type
114
+ if (paramRange.isA('ParameterRangeUnion')) {
115
+ return paramRange.properties.parameterRangeElements
116
+ .some(child => this.hasParamValueValidType(value, param, child));
117
+ }
118
+ if (paramRange.isA('ParameterRangeIntersection')) {
119
+ return paramRange.properties.parameterRangeElements
120
+ .every(child => this.hasParamValueValidType(value, param, child));
121
+ }
122
+ if (paramRange.isA('ParameterRangeTuple')) {
123
+ if (!value || !value.list) {
124
+ return false;
125
+ }
126
+ // Iterate over list elements and try to match with tuple types
127
+ const listElements = value.list;
128
+ const tupleTypes = paramRange.properties.parameterRangeElements;
129
+ let listIndex = 0;
130
+ let tupleIndex = 0;
131
+ while (listIndex < listElements.length && tupleIndex < tupleTypes.length) {
132
+ if (tupleTypes[tupleIndex].isA('ParameterRangeRest')) {
133
+ // Rest types can match multiple list elements, so only increment index if no match is found.
134
+ if (!this.hasParamValueValidType(listElements[listIndex], param, tupleTypes[tupleIndex].property.parameterRangeValue)) {
135
+ tupleIndex++;
136
+ }
137
+ else {
138
+ listIndex++;
139
+ }
140
+ }
141
+ else {
142
+ if (!this.hasParamValueValidType(listElements[listIndex], param, tupleTypes[tupleIndex])) {
143
+ return false;
144
+ }
145
+ tupleIndex++;
146
+ listIndex++;
147
+ }
148
+ }
149
+ return listIndex === listElements.length &&
150
+ (tupleIndex === tupleTypes.length ||
151
+ (tupleIndex === tupleTypes.length - 1 && tupleTypes[tupleIndex].isA('ParameterRangeRest')));
152
+ }
153
+ if (paramRange.isA('ParameterRangeLiteral')) {
154
+ return Boolean(value && value.term.equals(paramRange.property.parameterRangeValue.term));
155
+ }
156
+ // Check if this param defines a field with sub-params
157
+ if (paramRange.isA('ParameterRangeCollectEntries')) {
158
+ // TODO: Add support for type-checking nested fields with collectEntries
159
+ return true;
160
+ }
161
+ return false;
162
+ }
163
+ return true;
85
164
  }
86
165
  throwIncorrectTypeError(value, parameter) {
87
- throw new ErrorResourcesContext_1.ErrorResourcesContext(`Parameter value "${value.value}" is not of required range type "${parameter.property.range.value}"`, {
166
+ const withTypes = value && value.properties.types.length > 0 ? ` with types "${value.properties.types.map(resource => resource.value)}"` : '';
167
+ // eslint-disable-next-line @typescript-eslint/no-extra-parens
168
+ const valueString = value ? (value.list ? `[${value.list.map(subValue => subValue.value).join(', ')}]` : value.value) : 'undefined';
169
+ throw new ErrorResourcesContext_1.ErrorResourcesContext(`The value "${valueString}"${withTypes} for parameter "${parameter.value}" is not of required range type "${this.rangeToDisplayString(parameter.property.range)}"`, {
170
+ value: value || 'undefined',
88
171
  parameter,
89
172
  });
90
173
  }
174
+ rangeToDisplayString(paramRange) {
175
+ if (!paramRange) {
176
+ return `any`;
177
+ }
178
+ if (paramRange.isA('ParameterRangeUndefined')) {
179
+ return `undefined`;
180
+ }
181
+ if (paramRange.isA('ParameterRangeArray')) {
182
+ return `${this.rangeToDisplayString(paramRange.property.parameterRangeValue)}[]`;
183
+ }
184
+ if (paramRange.isA('ParameterRangeRest')) {
185
+ return `...${this.rangeToDisplayString(paramRange.property.parameterRangeValue)}`;
186
+ }
187
+ if (paramRange.isA('ParameterRangeUnion')) {
188
+ return paramRange.properties.parameterRangeElements
189
+ .map(child => this.rangeToDisplayString(child))
190
+ .join(' | ');
191
+ }
192
+ if (paramRange.isA('ParameterRangeIntersection')) {
193
+ return paramRange.properties.parameterRangeElements
194
+ .map(child => this.rangeToDisplayString(child))
195
+ .join(' & ');
196
+ }
197
+ if (paramRange.isA('ParameterRangeTuple')) {
198
+ return `[${paramRange.properties.parameterRangeElements
199
+ .map(child => this.rangeToDisplayString(child))
200
+ .join(', ')}]`;
201
+ }
202
+ if (paramRange.isA('ParameterRangeLiteral')) {
203
+ return paramRange.property.parameterRangeValue.value;
204
+ }
205
+ return paramRange.value;
206
+ }
91
207
  }
92
208
  exports.ParameterPropertyHandlerRange = ParameterPropertyHandlerRange;
93
209
  //# sourceMappingURL=ParameterPropertyHandlerRange.js.map
package/lib/rdf/Iris.js CHANGED
@@ -4,45 +4,45 @@ exports.IRIS_OWL = exports.PREFIX_OWL = exports.IRIS_DOAP = exports.PREFIX_DOAP
4
4
  const definePrefix = (prefix) => (suffix) => `${prefix}${suffix}`;
5
5
  exports.PREFIX_OO = definePrefix('https://linkedsoftwaredependencies.org/vocabularies/object-oriented#');
6
6
  exports.IRIS_OO = {
7
- Module: exports.PREFIX_OO('Module'),
8
- Class: exports.PREFIX_OO('Class'),
9
- AbstractClass: exports.PREFIX_OO('AbstractClass'),
10
- ComponentInstance: exports.PREFIX_OO('ComponentInstance'),
11
- component: exports.PREFIX_OO('component'),
12
- componentPath: exports.PREFIX_OO('componentPath'),
13
- parameter: exports.PREFIX_OO('parameter'),
7
+ Module: (0, exports.PREFIX_OO)('Module'),
8
+ Class: (0, exports.PREFIX_OO)('Class'),
9
+ AbstractClass: (0, exports.PREFIX_OO)('AbstractClass'),
10
+ ComponentInstance: (0, exports.PREFIX_OO)('ComponentInstance'),
11
+ component: (0, exports.PREFIX_OO)('component'),
12
+ componentPath: (0, exports.PREFIX_OO)('componentPath'),
13
+ parameter: (0, exports.PREFIX_OO)('parameter'),
14
14
  };
15
15
  exports.PREFIX_OM = definePrefix('https://linkedsoftwaredependencies.org/vocabularies/object-mapping#');
16
16
  exports.PREFIX_RDF = definePrefix('http://www.w3.org/1999/02/22-rdf-syntax-ns#');
17
17
  exports.IRIS_RDF = {
18
- subject: exports.PREFIX_RDF('subject'),
19
- object: exports.PREFIX_RDF('object'),
20
- type: exports.PREFIX_RDF('type'),
21
- JSON: exports.PREFIX_RDF('JSON'),
18
+ subject: (0, exports.PREFIX_RDF)('subject'),
19
+ object: (0, exports.PREFIX_RDF)('object'),
20
+ type: (0, exports.PREFIX_RDF)('type'),
21
+ JSON: (0, exports.PREFIX_RDF)('JSON'),
22
22
  };
23
23
  exports.PREFIX_RDFS = definePrefix('http://www.w3.org/2000/01/rdf-schema#');
24
24
  exports.IRIS_RDFS = {
25
- imports: exports.PREFIX_RDFS('seeAlso'),
25
+ imports: (0, exports.PREFIX_RDFS)('seeAlso'),
26
26
  };
27
27
  exports.PREFIX_XSD = definePrefix('http://www.w3.org/2001/XMLSchema#');
28
28
  exports.IRIS_XSD = {
29
- string: exports.PREFIX_XSD('string'),
30
- boolean: exports.PREFIX_XSD('boolean'),
31
- integer: exports.PREFIX_XSD('integer'),
32
- number: exports.PREFIX_XSD('number'),
33
- int: exports.PREFIX_XSD('int'),
34
- byte: exports.PREFIX_XSD('byte'),
35
- long: exports.PREFIX_XSD('long'),
36
- float: exports.PREFIX_XSD('float'),
37
- decimal: exports.PREFIX_XSD('decimal'),
38
- double: exports.PREFIX_XSD('double'),
29
+ string: (0, exports.PREFIX_XSD)('string'),
30
+ boolean: (0, exports.PREFIX_XSD)('boolean'),
31
+ integer: (0, exports.PREFIX_XSD)('integer'),
32
+ number: (0, exports.PREFIX_XSD)('number'),
33
+ int: (0, exports.PREFIX_XSD)('int'),
34
+ byte: (0, exports.PREFIX_XSD)('byte'),
35
+ long: (0, exports.PREFIX_XSD)('long'),
36
+ float: (0, exports.PREFIX_XSD)('float'),
37
+ decimal: (0, exports.PREFIX_XSD)('decimal'),
38
+ double: (0, exports.PREFIX_XSD)('double'),
39
39
  };
40
40
  exports.PREFIX_DOAP = definePrefix('http://usefulinc.com/ns/doap#');
41
41
  exports.IRIS_DOAP = {
42
- name: exports.PREFIX_DOAP('name'),
42
+ name: (0, exports.PREFIX_DOAP)('name'),
43
43
  };
44
44
  exports.PREFIX_OWL = definePrefix('http://www.w3.org/2002/07/owl#');
45
45
  exports.IRIS_OWL = {
46
- Restriction: exports.PREFIX_OWL('Restriction'),
46
+ Restriction: (0, exports.PREFIX_OWL)('Restriction'),
47
47
  };
48
48
  //# sourceMappingURL=Iris.js.map