@shaclmate/compiler 4.0.34 → 4.0.36

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 (147) hide show
  1. package/dist/generators/ts/AbstractCollectionType.d.ts +2 -1
  2. package/dist/generators/ts/AbstractCollectionType.js +14 -13
  3. package/dist/generators/ts/AbstractContainerType.d.ts +2 -0
  4. package/dist/generators/ts/AbstractContainerType.js +20 -0
  5. package/dist/generators/ts/AbstractDateType.d.ts +1 -1
  6. package/dist/generators/ts/AbstractDateType.js +3 -3
  7. package/dist/generators/ts/AbstractLazyObjectType.d.ts +1 -0
  8. package/dist/generators/ts/AbstractLazyObjectType.js +1 -0
  9. package/dist/generators/ts/AbstractLiteralType.d.ts +1 -1
  10. package/dist/generators/ts/AbstractLiteralType.js +2 -2
  11. package/dist/generators/ts/AbstractNamedUnionType.js +33 -20
  12. package/dist/generators/ts/AbstractNumericType.d.ts +1 -8
  13. package/dist/generators/ts/AbstractNumericType.js +2 -16
  14. package/dist/generators/ts/AbstractPrimitiveType.d.ts +1 -0
  15. package/dist/generators/ts/AbstractPrimitiveType.js +2 -1
  16. package/dist/generators/ts/AbstractTermType.d.ts +2 -1
  17. package/dist/generators/ts/AbstractTermType.js +3 -2
  18. package/dist/generators/ts/AbstractType.d.ts +19 -8
  19. package/dist/generators/ts/AbstractUnionType.d.ts +3 -2
  20. package/dist/generators/ts/AbstractUnionType.js +14 -13
  21. package/dist/generators/ts/BigDecimalType.d.ts +3 -2
  22. package/dist/generators/ts/BigDecimalType.js +5 -12
  23. package/dist/generators/ts/BigIntType.js +1 -1
  24. package/dist/generators/ts/BlankNodeType.d.ts +3 -2
  25. package/dist/generators/ts/BlankNodeType.js +6 -5
  26. package/dist/generators/ts/BooleanType.d.ts +1 -2
  27. package/dist/generators/ts/BooleanType.js +2 -16
  28. package/dist/generators/ts/DateTimeType.d.ts +0 -1
  29. package/dist/generators/ts/DateTimeType.js +0 -9
  30. package/dist/generators/ts/DateType.d.ts +0 -1
  31. package/dist/generators/ts/DateType.js +0 -9
  32. package/dist/generators/ts/DefaultValueType.d.ts +2 -1
  33. package/dist/generators/ts/DefaultValueType.js +5 -4
  34. package/dist/generators/ts/IdentifierType.d.ts +3 -2
  35. package/dist/generators/ts/IdentifierType.js +6 -5
  36. package/dist/generators/ts/IriType.d.ts +3 -11
  37. package/dist/generators/ts/IriType.js +6 -5
  38. package/dist/generators/ts/LazyObjectOptionType.d.ts +1 -1
  39. package/dist/generators/ts/LazyObjectOptionType.js +3 -3
  40. package/dist/generators/ts/LazyObjectSetType.d.ts +2 -1
  41. package/dist/generators/ts/LazyObjectSetType.js +3 -3
  42. package/dist/generators/ts/LazyObjectType.d.ts +2 -1
  43. package/dist/generators/ts/LazyObjectType.js +3 -3
  44. package/dist/generators/ts/LiteralType.d.ts +2 -1
  45. package/dist/generators/ts/LiteralType.js +4 -3
  46. package/dist/generators/ts/NamedObjectType.d.ts +2 -1
  47. package/dist/generators/ts/NamedObjectType.js +9 -16
  48. package/dist/generators/ts/NamedObjectUnionType.js +14 -5
  49. package/dist/generators/ts/OptionType.d.ts +4 -3
  50. package/dist/generators/ts/OptionType.js +20 -17
  51. package/dist/generators/ts/Snippets.d.ts +7 -11
  52. package/dist/generators/ts/Snippets.js +46 -74
  53. package/dist/generators/ts/StringType.d.ts +1 -2
  54. package/dist/generators/ts/StringType.js +2 -16
  55. package/dist/generators/ts/TermType.d.ts +2 -1
  56. package/dist/generators/ts/TermType.js +6 -16
  57. package/dist/generators/ts/TsFeature.d.ts +1 -1
  58. package/dist/generators/ts/TsFeature.js +25 -7
  59. package/dist/generators/ts/TsGenerator.d.ts +1 -1
  60. package/dist/generators/ts/TsGenerator.js +66 -8
  61. package/dist/generators/ts/_NamedObjectType/IdentifierProperty.js +2 -1
  62. package/dist/generators/ts/_NamedObjectType/NamedObjectType_createFunctionDeclaration.js +1 -1
  63. package/dist/generators/ts/_NamedObjectType/NamedObjectType_equalsFunctionDeclaration.js +1 -1
  64. package/dist/generators/ts/_NamedObjectType/NamedObjectType_filterFunctionDeclaration.d.ts +2 -1
  65. package/dist/generators/ts/_NamedObjectType/NamedObjectType_filterFunctionDeclaration.js +6 -2
  66. package/dist/generators/ts/_NamedObjectType/NamedObjectType_filterTypeDeclaration.d.ts +2 -1
  67. package/dist/generators/ts/_NamedObjectType/NamedObjectType_filterTypeDeclaration.js +6 -2
  68. package/dist/generators/ts/_NamedObjectType/NamedObjectType_focusSparqlConstructTriplesFunctionDeclaration.js +1 -1
  69. package/dist/generators/ts/_NamedObjectType/NamedObjectType_focusSparqlWherePatternsFunctionDeclaration.js +1 -1
  70. package/dist/generators/ts/_NamedObjectType/NamedObjectType_fromJsonFunctionDeclaration.js +30 -9
  71. package/dist/generators/ts/_NamedObjectType/NamedObjectType_fromRdfResourceFunctionDeclaration.js +6 -6
  72. package/dist/generators/ts/_NamedObjectType/NamedObjectType_fromRdfResourceValuesFunctionDeclaration.js +1 -1
  73. package/dist/generators/ts/_NamedObjectType/NamedObjectType_fromRdfTypeVariableStatement.js +1 -1
  74. package/dist/generators/ts/_NamedObjectType/NamedObjectType_graphqlTypeVariableStatement.js +1 -1
  75. package/dist/generators/ts/_NamedObjectType/NamedObjectType_hashFunctionDeclarations.js +1 -1
  76. package/dist/generators/ts/_NamedObjectType/NamedObjectType_isTypeFunctionDeclaration.d.ts +2 -1
  77. package/dist/generators/ts/_NamedObjectType/NamedObjectType_isTypeFunctionDeclaration.js +6 -2
  78. package/dist/generators/ts/_NamedObjectType/NamedObjectType_jsonParseFunctionDeclaration.js +1 -1
  79. package/dist/generators/ts/_NamedObjectType/NamedObjectType_jsonSchemaFunctionDeclaration.js +1 -1
  80. package/dist/generators/ts/_NamedObjectType/NamedObjectType_jsonTypeAliasDeclaration.js +1 -1
  81. package/dist/generators/ts/_NamedObjectType/NamedObjectType_jsonUiSchemaFunctionDeclaration.js +1 -1
  82. package/dist/generators/ts/_NamedObjectType/NamedObjectType_schemaVariableStatement.d.ts +2 -1
  83. package/dist/generators/ts/_NamedObjectType/NamedObjectType_schemaVariableStatement.js +6 -2
  84. package/dist/generators/ts/_NamedObjectType/NamedObjectType_sparqlConstructQueryFunctionDeclaration.js +1 -1
  85. package/dist/generators/ts/_NamedObjectType/NamedObjectType_sparqlConstructQueryStringFunctionDeclaration.js +1 -1
  86. package/dist/generators/ts/_NamedObjectType/NamedObjectType_toJsonFunctionDeclaration.js +1 -1
  87. package/dist/generators/ts/_NamedObjectType/NamedObjectType_toRdfResourceFunctionDeclaration.js +1 -1
  88. package/dist/generators/ts/_NamedObjectType/NamedObjectType_toStringFunctionDeclarations.js +3 -0
  89. package/dist/generators/ts/_NamedObjectType/NamedObjectType_valueSparqlConstructTriplesFunctionDeclaration.js +1 -1
  90. package/dist/generators/ts/_NamedObjectType/NamedObjectType_valueSparqlWherePatternsFunctionDeclaration.js +1 -1
  91. package/dist/generators/ts/_NamedObjectType/ShaclProperty.js +26 -4
  92. package/dist/generators/ts/_NamedObjectType/identifierTypeDeclarations.js +3 -0
  93. package/dist/generators/ts/_snippets/snippets_ConversionFunction.d.ts +3 -0
  94. package/dist/generators/ts/_snippets/snippets_ConversionFunction.js +4 -0
  95. package/dist/generators/ts/_snippets/snippets_PropertyPath.js +12 -6
  96. package/dist/generators/ts/_snippets/snippets_ValidationFunction.d.ts +3 -0
  97. package/dist/generators/ts/_snippets/snippets_ValidationFunction.js +4 -0
  98. package/dist/generators/ts/_snippets/snippets_convertToArray.d.ts +3 -0
  99. package/dist/generators/ts/_snippets/snippets_convertToArray.js +8 -0
  100. package/dist/generators/ts/_snippets/snippets_convertToBlankNode.js +2 -2
  101. package/dist/generators/ts/_snippets/snippets_convertToIdentifier.js +2 -2
  102. package/dist/generators/ts/_snippets/snippets_convertToIri.js +2 -2
  103. package/dist/generators/ts/_snippets/snippets_convertToLazyObject.js +1 -1
  104. package/dist/generators/ts/_snippets/snippets_convertToLazyObjectOption.js +1 -1
  105. package/dist/generators/ts/_snippets/snippets_convertToLazyObjectSet.js +1 -1
  106. package/dist/generators/ts/_snippets/snippets_convertToLiteral.js +1 -1
  107. package/dist/generators/ts/_snippets/snippets_convertToMaybe.js +3 -3
  108. package/dist/generators/ts/_snippets/snippets_convertWithDefaultValue.js +4 -4
  109. package/dist/generators/ts/_snippets/snippets_identityConversionFunction.d.ts +3 -0
  110. package/dist/generators/ts/_snippets/snippets_identityConversionFunction.js +6 -0
  111. package/dist/generators/ts/_snippets/snippets_identityValidationFunction.d.ts +3 -0
  112. package/dist/generators/ts/_snippets/snippets_identityValidationFunction.js +6 -0
  113. package/dist/generators/ts/_snippets/snippets_validateArray.d.ts +3 -0
  114. package/dist/generators/ts/_snippets/snippets_validateArray.js +13 -0
  115. package/dist/generators/ts/_snippets/snippets_validateMaybe.d.ts +3 -0
  116. package/dist/generators/ts/_snippets/snippets_validateMaybe.js +7 -0
  117. package/dist/generators/ts/graphqlSchemaVariableStatement.js +1 -1
  118. package/dist/generators/ts/objectSetDeclarations.js +7 -10
  119. package/dist/generators/ts/rdfjsDatasetObjectSetClassDeclaration.js +0 -6
  120. package/dist/generators/ts/sparqlObjectSetClassDeclaration.js +0 -6
  121. package/dist/input/generated.d.ts +8 -8
  122. package/dist/input/generated.js +188 -191
  123. package/package.json +2 -2
  124. package/dist/generators/ts/_snippets/snippets_convertToBigDecimal.d.ts +0 -3
  125. package/dist/generators/ts/_snippets/snippets_convertToBigDecimal.js +0 -6
  126. package/dist/generators/ts/_snippets/snippets_convertToBoolean.d.ts +0 -3
  127. package/dist/generators/ts/_snippets/snippets_convertToBoolean.js +0 -6
  128. package/dist/generators/ts/_snippets/snippets_convertToDate.d.ts +0 -3
  129. package/dist/generators/ts/_snippets/snippets_convertToDate.js +0 -6
  130. package/dist/generators/ts/_snippets/snippets_convertToDateTime.d.ts +0 -3
  131. package/dist/generators/ts/_snippets/snippets_convertToDateTime.js +0 -6
  132. package/dist/generators/ts/_snippets/snippets_convertToMutableArray.d.ts +0 -3
  133. package/dist/generators/ts/_snippets/snippets_convertToMutableArray.js +0 -14
  134. package/dist/generators/ts/_snippets/snippets_convertToNumeric.d.ts +0 -3
  135. package/dist/generators/ts/_snippets/snippets_convertToNumeric.js +0 -6
  136. package/dist/generators/ts/_snippets/snippets_convertToObject.d.ts +0 -3
  137. package/dist/generators/ts/_snippets/snippets_convertToObject.js +0 -6
  138. package/dist/generators/ts/_snippets/snippets_convertToReadonlyArray.d.ts +0 -3
  139. package/dist/generators/ts/_snippets/snippets_convertToReadonlyArray.js +0 -14
  140. package/dist/generators/ts/_snippets/snippets_convertToString.d.ts +0 -3
  141. package/dist/generators/ts/_snippets/snippets_convertToString.js +0 -6
  142. package/dist/generators/ts/_snippets/snippets_convertToTerm.d.ts +0 -3
  143. package/dist/generators/ts/_snippets/snippets_convertToTerm.js +0 -6
  144. package/dist/generators/ts/_snippets/snippets_convertToUnion.d.ts +0 -3
  145. package/dist/generators/ts/_snippets/snippets_convertToUnion.js +0 -6
  146. package/dist/generators/ts/unsupportedObjectSetMethodDeclarations.d.ts +0 -12
  147. package/dist/generators/ts/unsupportedObjectSetMethodDeclarations.js +0 -21
@@ -15,7 +15,7 @@ export declare abstract class AbstractCollectionType<ItemTypeT extends AbstractC
15
15
  minCount: bigint;
16
16
  mutable: boolean;
17
17
  } & ConstructorParameters<typeof AbstractContainerType<ItemTypeT>>[0]);
18
- get conversionFunction(): AbstractType.ConversionFunction;
18
+ get conversionFunction(): Maybe<AbstractType.ConversionFunction>;
19
19
  get equalsFunction(): Code;
20
20
  get filterFunction(): Code;
21
21
  get filterType(): Code;
@@ -24,6 +24,7 @@ export declare abstract class AbstractCollectionType<ItemTypeT extends AbstractC
24
24
  get mutable(): boolean;
25
25
  get name(): Code;
26
26
  get schemaType(): Code;
27
+ get validationFunction(): Maybe<Code>;
27
28
  protected get schemaObject(): {
28
29
  minCount: number | undefined;
29
30
  item: Code;
@@ -8,8 +8,7 @@ import { Maybe } from "purify-ts";
8
8
  import { invariant } from "ts-invariant";
9
9
  import { Memoize } from "typescript-memoize";
10
10
  import { AbstractContainerType } from "./AbstractContainerType.js";
11
- import { codeEquals } from "./codeEquals.js";
12
- import { code, joinCode } from "./ts-poet-wrapper.js";
11
+ import { code, joinCode, literalOf } from "./ts-poet-wrapper.js";
13
12
  /**
14
13
  * Abstract base class for ListType and SetType.
15
14
  */
@@ -29,7 +28,7 @@ export class AbstractCollectionType extends AbstractContainerType {
29
28
  }
30
29
  }
31
30
  get conversionFunction() {
32
- const itemConversionFunction = this.itemType.conversionFunction;
31
+ const itemConversionFunction = this.itemType.conversionFunction.orDefault(this.itemConversionFunctionDefault);
33
32
  const sourceTypes = [
34
33
  {
35
34
  name: code `readonly (${joinCode(itemConversionFunction.sourceTypes.map((itemSourceType) => code `${itemSourceType.name}`), { on: " | " })})[]`,
@@ -42,10 +41,10 @@ export class AbstractCollectionType extends AbstractContainerType {
42
41
  typeof: "undefined",
43
42
  });
44
43
  }
45
- return {
46
- code: code `${this._mutable ? this.reusables.snippets.convertToMutableArray : this.reusables.snippets.convertToReadonlyArray}(${itemConversionFunction.code})`,
44
+ return Maybe.of({
45
+ code: code `${this.reusables.snippets.convertToArray}(${itemConversionFunction.code}, ${literalOf(!this._mutable)})`,
47
46
  sourceTypes,
48
- };
47
+ });
49
48
  }
50
49
  get equalsFunction() {
51
50
  return code `((left, right) => ${this.reusables.snippets.arrayEquals}(left, right, ${this.itemType.equalsFunction}))`;
@@ -71,6 +70,9 @@ export class AbstractCollectionType extends AbstractContainerType {
71
70
  get schemaType() {
72
71
  return code `${this.reusables.snippets.CollectionSchema}<${this.itemType.schemaType}>`;
73
72
  }
73
+ get validationFunction() {
74
+ return Maybe.of(code `${this.reusables.snippets.validateArray}(${this.itemType.validationFunction.orDefault(this.itemValidationFunctionDefault)}, ${literalOf(!this._mutable)})`);
75
+ }
74
76
  get schemaObject() {
75
77
  return {
76
78
  ...super.schemaObject,
@@ -82,13 +84,9 @@ export class AbstractCollectionType extends AbstractContainerType {
82
84
  if (this.minCount === 0n) {
83
85
  expression = code `(${expression} ?? [])`;
84
86
  }
85
- const valueVariable = code `item`;
86
- const itemFromJsonExpression = this.itemType.fromJsonExpression({
87
- variables: { value: valueVariable },
88
- });
89
- return codeEquals(itemFromJsonExpression, valueVariable)
90
- ? expression
91
- : code `${expression}.map(item => (${itemFromJsonExpression}))`;
87
+ return code `${this.reusables.imports.Either}.sequence<Error, ${this.itemType.name}>(${expression}.map(item => (${this.itemType.fromJsonExpression({
88
+ variables: { value: code `item` },
89
+ })})))`;
92
90
  }
93
91
  graphqlResolveExpression({ variables, }) {
94
92
  return variables.value;
@@ -137,6 +135,9 @@ __decorate([
137
135
  __decorate([
138
136
  Memoize()
139
137
  ], AbstractCollectionType.prototype, "schemaType", null);
138
+ __decorate([
139
+ Memoize()
140
+ ], AbstractCollectionType.prototype, "validationFunction", null);
140
141
  (function (AbstractCollectionType) {
141
142
  AbstractCollectionType.GraphqlType = AbstractContainerType.GraphqlType;
142
143
  AbstractCollectionType.isItemType = AbstractContainerType.isItemType;
@@ -38,6 +38,8 @@ export declare abstract class AbstractContainerType<ItemTypeT extends AbstractCo
38
38
  get recursive(): boolean;
39
39
  get schema(): Code;
40
40
  get toRdfResourceValueTypes(): AbstractType["toRdfResourceValueTypes"];
41
+ protected get itemConversionFunctionDefault(): AbstractType.ConversionFunction;
42
+ protected get itemValidationFunctionDefault(): Code;
41
43
  protected get schemaObject(): {
42
44
  item: Code;
43
45
  kind: Code;
@@ -33,6 +33,20 @@ export class AbstractContainerType extends AbstractType {
33
33
  get toRdfResourceValueTypes() {
34
34
  return this.itemType.toRdfResourceValueTypes;
35
35
  }
36
+ get itemConversionFunctionDefault() {
37
+ return {
38
+ code: code `${this.reusables.snippets.identityConversionFunction}`,
39
+ sourceTypes: [
40
+ {
41
+ name: this.itemType.name,
42
+ typeof: this.itemType.typeofs[0],
43
+ },
44
+ ],
45
+ };
46
+ }
47
+ get itemValidationFunctionDefault() {
48
+ return code `${this.reusables.snippets.identityValidationFunction}`;
49
+ }
36
50
  get schemaObject() {
37
51
  return {
38
52
  ...super.schemaObject,
@@ -43,6 +57,12 @@ export class AbstractContainerType extends AbstractType {
43
57
  __decorate([
44
58
  Memoize()
45
59
  ], AbstractContainerType.prototype, "schema", null);
60
+ __decorate([
61
+ Memoize()
62
+ ], AbstractContainerType.prototype, "itemConversionFunctionDefault", null);
63
+ __decorate([
64
+ Memoize()
65
+ ], AbstractContainerType.prototype, "itemValidationFunctionDefault", null);
46
66
  (function (AbstractContainerType) {
47
67
  AbstractContainerType.GraphqlType = AbstractType.GraphqlType;
48
68
  function isItemType(type) {
@@ -13,7 +13,7 @@ export declare abstract class AbstractDateType extends AbstractPrimitiveType<Dat
13
13
  fromJsonExpression({ variables, }: Parameters<AbstractPrimitiveType<Date>["fromJsonExpression"]>[0]): Code;
14
14
  jsonType(): AbstractPrimitiveType.JsonType;
15
15
  toRdfResourceValuesExpression({ variables, }: Parameters<AbstractPrimitiveType<Date>["toRdfResourceValuesExpression"]>[0]): Code;
16
- protected fromRdfExpressionChain({ variables, }: Parameters<AbstractPrimitiveType<Date>["fromRdfExpressionChain"]>[0]): ReturnType<AbstractPrimitiveType<Date>["fromRdfExpressionChain"]>;
16
+ protected fromRdfResourceValuesExpressionChain({ variables, }: Parameters<AbstractPrimitiveType<Date>["fromRdfResourceValuesExpressionChain"]>[0]): ReturnType<AbstractPrimitiveType<Date>["fromRdfResourceValuesExpressionChain"]>;
17
17
  protected abstract fromRdfResourceValueExpression(variables: {
18
18
  variables: {
19
19
  value: Code;
@@ -17,7 +17,7 @@ export class AbstractDateType extends AbstractPrimitiveType {
17
17
  valueSparqlWherePatternsFunction = code `${this.reusables.snippets.dateSparqlWherePatterns}`;
18
18
  typeofs = ["object"];
19
19
  fromJsonExpression({ variables, }) {
20
- return code `new Date(${variables.value})`;
20
+ return code `${this.reusables.imports.Either}.of<Error, Date>(new Date(${variables.value}))`;
21
21
  }
22
22
  jsonType() {
23
23
  return new AbstractPrimitiveType.JsonType(code `string`);
@@ -25,9 +25,9 @@ export class AbstractDateType extends AbstractPrimitiveType {
25
25
  toRdfResourceValuesExpression({ variables, }) {
26
26
  return code `[${this.reusables.snippets.literalFactory}.date(${variables.value}, ${this.rdfjsTermExpression(this.datatype)})]`;
27
27
  }
28
- fromRdfExpressionChain({ variables, }) {
28
+ fromRdfResourceValuesExpressionChain({ variables, }) {
29
29
  return {
30
- ...super.fromRdfExpressionChain({ variables }),
30
+ ...super.fromRdfResourceValuesExpressionChain({ variables }),
31
31
  languageIn: undefined,
32
32
  preferredLanguages: undefined,
33
33
  valueTo: code `chain(values => values.chainMap(value => ${this.fromRdfResourceValueExpression({
@@ -17,6 +17,7 @@ export declare abstract class AbstractLazyObjectType<PartialTypeT extends Abstra
17
17
  readonly discriminantProperty: AbstractType["discriminantProperty"];
18
18
  readonly mutable = false;
19
19
  readonly typeofs: "object"[];
20
+ readonly validationFunction: Maybe<Code>;
20
21
  constructor({ partialType, resolveType, ...superParameters }: {
21
22
  partialType: PartialTypeT;
22
23
  resolveType: ResolveTypeT;
@@ -17,6 +17,7 @@ export class AbstractLazyObjectType extends AbstractType {
17
17
  discriminantProperty = Maybe.empty();
18
18
  mutable = false;
19
19
  typeofs = ["object"];
20
+ validationFunction = Maybe.empty();
20
21
  constructor({ partialType, resolveType, ...superParameters }) {
21
22
  super(superParameters);
22
23
  this.partialType = partialType;
@@ -19,7 +19,7 @@ export declare abstract class AbstractLiteralType extends AbstractTermType<Liter
19
19
  * For example, a string would be converted to "thestring".
20
20
  */
21
21
  abstract literalExpression(literal: Literal): Code;
22
- protected fromRdfExpressionChain({ variables, }: Parameters<AbstractTermType<Literal, Literal>["fromRdfExpressionChain"]>[0]): ReturnType<AbstractTermType<Literal, Literal>["fromRdfExpressionChain"]>;
22
+ protected fromRdfResourceValuesExpressionChain({ variables, }: Parameters<AbstractTermType<Literal, Literal>["fromRdfResourceValuesExpressionChain"]>[0]): ReturnType<AbstractTermType<Literal, Literal>["fromRdfResourceValuesExpressionChain"]>;
23
23
  }
24
24
  export declare namespace AbstractLiteralType {
25
25
  type ConversionFunction = AbstractTermType.ConversionFunction;
@@ -16,9 +16,9 @@ export class AbstractLiteralType extends AbstractTermType {
16
16
  languageIn: this.languageIn.length > 0 ? this.languageIn.map(literalOf) : undefined,
17
17
  };
18
18
  }
19
- fromRdfExpressionChain({ variables, }) {
19
+ fromRdfResourceValuesExpressionChain({ variables, }) {
20
20
  return {
21
- ...super.fromRdfExpressionChain({ variables }),
21
+ ...super.fromRdfResourceValuesExpressionChain({ variables }),
22
22
  languageIn: this.languageIn.length > 0
23
23
  ? code `chain(values => ${this.reusables.snippets.fromRdfLanguageIn}(values, ${JSON.stringify(this.languageIn)}))`
24
24
  : undefined,
@@ -16,9 +16,10 @@ export class AbstractNamedUnionType extends AbstractUnionType {
16
16
  this._name = name;
17
17
  }
18
18
  get declaration() {
19
- const declarations = [
20
- code `export type ${def(this._name)} = ${this.inlineName};`,
21
- ];
19
+ const declarations = [];
20
+ if (this.configuration.features.has("Object.type")) {
21
+ declarations.push(code `export type ${def(this._name)} = ${this.inlineName};`);
22
+ }
22
23
  const staticModuleDeclarations = Object.entries(this.staticModuleDeclarations);
23
24
  if (staticModuleDeclarations.length > 0) {
24
25
  declarations.push(code `\
@@ -68,22 +69,26 @@ ${joinCode(staticModuleDeclarations
68
69
  }
69
70
  get staticModuleDeclarations() {
70
71
  const staticModuleDeclarations = {};
71
- if (this.configuration.features.has("equals")) {
72
+ if (this.configuration.features.has("Object.equals")) {
72
73
  staticModuleDeclarations[`equals`] =
73
74
  code `export const equals = ${this.inlineEqualsFunction};`;
74
75
  }
75
- staticModuleDeclarations[`Filter`] =
76
- code `export type Filter = ${this.inlineFilterType};`;
77
- staticModuleDeclarations[`filter`] =
78
- code `export const filter = ${this.inlineFilterFunction};`;
79
- if (this.configuration.features.has("hash")) {
76
+ if (this.configuration.features.has("Object.filter")) {
77
+ staticModuleDeclarations[`Filter`] =
78
+ code `export type Filter = ${this.inlineFilterType};`;
79
+ staticModuleDeclarations[`filter`] =
80
+ code `export const filter = ${this.inlineFilterFunction};`;
81
+ }
82
+ if (this.configuration.features.has("Object.hash")) {
80
83
  staticModuleDeclarations[`hash`] =
81
84
  code `export const hash = ${this.inlineHashFunction};`;
82
85
  }
83
- if (this.configuration.features.has("json")) {
84
- staticModuleDeclarations[`Json`] = code `\
85
- ${this.jsonTypeAliasDeclaration}
86
-
86
+ if (this.configuration.features.has("Object.JSON.type")) {
87
+ staticModuleDeclarations[`Json.type`] =
88
+ code `${this.jsonTypeAliasDeclaration}`;
89
+ }
90
+ if (this.configuration.features.has("Object.JSON.schema")) {
91
+ staticModuleDeclarations[`Json.namespace`] = code `\
87
92
  export namespace Json {
88
93
  ${this.jsonSchemaFunctionDeclaration}
89
94
 
@@ -93,26 +98,34 @@ export namespace Json {
93
98
  return ${this.reusables.imports.Right}(jsonSafeParseResult.data);
94
99
  }
95
100
  }`;
101
+ }
102
+ if (this.configuration.features.has("Object.fromJson")) {
96
103
  staticModuleDeclarations[`fromJson`] =
97
104
  code `export const fromJson = ${this.inlineFromJsonFunction};`;
98
- staticModuleDeclarations[`toJson`] =
99
- code `export const toJson = ${this.inlineToJsonFunction};`;
100
105
  }
101
- if (this.configuration.features.has("rdf")) {
106
+ if (this.configuration.features.has("Object.fromRdf")) {
102
107
  staticModuleDeclarations[`fromRdfResourceValues`] =
103
108
  code `export const fromRdfResourceValues: ${this.reusables.snippets.FromRdfResourceValuesFunction}<${this.name}> = ${this.inlineFromRdfResourceValuesFunction};`;
109
+ }
110
+ if (this.configuration.features.has("Object.toJson")) {
111
+ staticModuleDeclarations[`toJson`] =
112
+ code `export const toJson = ${this.inlineToJsonFunction};`;
113
+ }
114
+ if (this.configuration.features.has("Object.toRdf")) {
104
115
  staticModuleDeclarations[`toRdfResourceValues`] =
105
116
  code `export const toRdfResourceValues = ${this.inlineToRdfResourceValuesFunction};`;
106
117
  }
107
- if (this.configuration.features.has("sparql")) {
118
+ if (this.configuration.features.has("Object.SPARQL")) {
108
119
  staticModuleDeclarations[`valueSparqlConstructTriples`] =
109
120
  code `export const valueSparqlConstructTriples: ${this.reusables.snippets.ValueSparqlConstructTriplesFunction}<${this.filterType}, ${this.schemaType}> = ${this.inlineValueSparqlConstructTriplesFunction};`;
110
121
  staticModuleDeclarations[`valueSparqlWherePatterns`] =
111
122
  code `export const valueSparqlWherePatterns: ${this.reusables.snippets.ValueSparqlWherePatternsFunction}<${this.filterType}, ${this.schemaType}> = ${this.inlineValueSparqlWherePatternsFunction};`;
112
123
  }
113
- const syntheticNamePrefix = this.configuration.syntheticNamePrefix;
114
- staticModuleDeclarations[`${syntheticNamePrefix}toString`] =
115
- code `export const ${syntheticNamePrefix}toString = ${this.inlineToStringFunction};`;
124
+ if (this.configuration.features.has("Object.toString")) {
125
+ const syntheticNamePrefix = this.configuration.syntheticNamePrefix;
126
+ staticModuleDeclarations[`${syntheticNamePrefix}toString`] =
127
+ code `export const ${syntheticNamePrefix}toString = ${this.inlineToStringFunction};`;
128
+ }
116
129
  return staticModuleDeclarations;
117
130
  }
118
131
  fromJsonExpression({ variables, }) {
@@ -6,13 +6,6 @@ import { type Code } from "./ts-poet-wrapper.js";
6
6
  export declare abstract class AbstractNumericType<ValueT extends bigint | number> extends AbstractPrimitiveType<ValueT> {
7
7
  readonly hashFunction: Code;
8
8
  abstract readonly kind: "BigIntType" | "FloatType" | "IntType";
9
- get conversionFunction(): {
10
- code: Code;
11
- sourceTypes: {
12
- name: string | Code;
13
- typeof: import("./Typeof.js").Typeof;
14
- }[];
15
- };
16
9
  get filterFunction(): Code;
17
10
  get filterType(): Code;
18
11
  get name(): Code | string;
@@ -20,7 +13,7 @@ export declare abstract class AbstractNumericType<ValueT extends bigint | number
20
13
  get valueSparqlWherePatternsFunction(): Code;
21
14
  jsonSchema(_parameters: Parameters<AbstractPrimitiveType<ValueT>["jsonSchema"]>[0]): Code;
22
15
  toRdfResourceValuesExpression({ variables, }: Parameters<AbstractPrimitiveType<string>["toRdfResourceValuesExpression"]>[0]): Code;
23
- protected fromRdfExpressionChain({ variables, }: Parameters<AbstractPrimitiveType<ValueT>["fromRdfExpressionChain"]>[0]): ReturnType<AbstractPrimitiveType<ValueT>["fromRdfExpressionChain"]>;
16
+ protected fromRdfResourceValuesExpressionChain({ variables, }: Parameters<AbstractPrimitiveType<ValueT>["fromRdfResourceValuesExpressionChain"]>[0]): ReturnType<AbstractPrimitiveType<ValueT>["fromRdfResourceValuesExpressionChain"]>;
24
17
  protected abstract fromRdfResourceValueExpression(variables: {
25
18
  variables: {
26
19
  value: Code;
@@ -12,17 +12,6 @@ import { code, joinCode } from "./ts-poet-wrapper.js";
12
12
  */
13
13
  export class AbstractNumericType extends AbstractPrimitiveType {
14
14
  hashFunction = code `${this.reusables.snippets.hashNumeric}`;
15
- get conversionFunction() {
16
- return {
17
- code: code `${this.reusables.snippets.convertToNumeric}<${this.name}>`,
18
- sourceTypes: [
19
- {
20
- name: this.name,
21
- typeof: this.typeofs[0],
22
- },
23
- ],
24
- };
25
- }
26
15
  get filterFunction() {
27
16
  return code `${this.reusables.snippets.filterNumeric}<${this.typeofs[0]}>`;
28
17
  }
@@ -54,9 +43,9 @@ export class AbstractNumericType extends AbstractPrimitiveType {
54
43
  toRdfResourceValuesExpression({ variables, }) {
55
44
  return code `[${this.reusables.snippets.literalFactory}.${this.typeofs[0]}(${variables.value}, ${this.rdfjsTermExpression(this.datatype)})]`;
56
45
  }
57
- fromRdfExpressionChain({ variables, }) {
46
+ fromRdfResourceValuesExpressionChain({ variables, }) {
58
47
  return {
59
- ...super.fromRdfExpressionChain({ variables }),
48
+ ...super.fromRdfResourceValuesExpressionChain({ variables }),
60
49
  languageIn: undefined,
61
50
  preferredLanguages: undefined,
62
51
  valueTo: code `chain(values => values.chainMap(value => ${this.fromRdfResourceValueExpression({
@@ -65,9 +54,6 @@ export class AbstractNumericType extends AbstractPrimitiveType {
65
54
  };
66
55
  }
67
56
  }
68
- __decorate([
69
- Memoize()
70
- ], AbstractNumericType.prototype, "conversionFunction", null);
71
57
  __decorate([
72
58
  Memoize()
73
59
  ], AbstractNumericType.prototype, "filterFunction", null);
@@ -4,6 +4,7 @@ import { AbstractLiteralType } from "./AbstractLiteralType.js";
4
4
  import { type Code } from "./ts-poet-wrapper.js";
5
5
  export declare abstract class AbstractPrimitiveType<ValueT extends bigint | boolean | Date | string | number> extends AbstractLiteralType {
6
6
  protected readonly datatype: NamedNode;
7
+ readonly conversionFunction: Maybe<AbstractLiteralType.ConversionFunction>;
7
8
  readonly equalsFunction: Code;
8
9
  abstract readonly kind: "BigIntType" | "BooleanType" | "DateTimeType" | "DateType" | "FloatType" | "IntType" | "NumberType" | "StringType";
9
10
  readonly primitiveIn: readonly ValueT[];
@@ -10,6 +10,7 @@ import { AbstractLiteralType } from "./AbstractLiteralType.js";
10
10
  import { code, joinCode } from "./ts-poet-wrapper.js";
11
11
  export class AbstractPrimitiveType extends AbstractLiteralType {
12
12
  datatype;
13
+ conversionFunction = Maybe.empty();
13
14
  equalsFunction = code `${this.reusables.snippets.strictEquals}`;
14
15
  primitiveIn;
15
16
  constructor({ datatype, primitiveIn, ...superParameters }) {
@@ -29,7 +30,7 @@ export class AbstractPrimitiveType extends AbstractLiteralType {
29
30
  };
30
31
  }
31
32
  fromJsonExpression({ variables, }) {
32
- return variables.value;
33
+ return code `${this.reusables.imports.Either}.of<Error, ${this.name}>(${variables.value})`;
33
34
  }
34
35
  graphqlResolveExpression({ variables, }) {
35
36
  return variables.value;
@@ -24,6 +24,7 @@ export declare abstract class AbstractTermType<ConstantTermT extends Literal | N
24
24
  abstract readonly nodeKinds: ReadonlySet<NodeKind>;
25
25
  readonly recursive = false;
26
26
  readonly typeofs: readonly Typeof[];
27
+ readonly validationFunction: Maybe<Code>;
27
28
  constructor({ hasValues, in_, ...superParameters }: {
28
29
  hasValues: readonly ConstantTermT[];
29
30
  in_: readonly ConstantTermT[];
@@ -51,7 +52,7 @@ export declare abstract class AbstractTermType<ConstantTermT extends Literal | N
51
52
  *
52
53
  * Considering the sub-expressions as a record instead of an array allows them to be selectively overridden by subclasses.
53
54
  */
54
- protected fromRdfExpressionChain({ variables, }: Parameters<Type["fromRdfResourceValuesExpression"]>[0]): {
55
+ protected fromRdfResourceValuesExpressionChain({ variables, }: Parameters<Type["fromRdfResourceValuesExpression"]>[0]): {
55
56
  hasValues?: Code;
56
57
  languageIn?: Code;
57
58
  preferredLanguages?: Code;
@@ -28,6 +28,7 @@ export class AbstractTermType extends AbstractType {
28
28
  mutable = false;
29
29
  recursive = false;
30
30
  typeofs = ["object"];
31
+ validationFunction = Maybe.empty();
31
32
  constructor({ hasValues, in_, ...superParameters }) {
32
33
  super(superParameters);
33
34
  this.hasValues = hasValues;
@@ -71,7 +72,7 @@ export class AbstractTermType extends AbstractType {
71
72
  // (this.nodeKinds.has("BlankNode") || this.nodeKinds.has("NamedNode")),
72
73
  // "IdentifierType and LiteralType should override",
73
74
  // );
74
- const chain = this.fromRdfExpressionChain(parameters);
75
+ const chain = this.fromRdfResourceValuesExpressionChain(parameters);
75
76
  const { variables } = parameters;
76
77
  return joinCode([
77
78
  variables.resourceValues,
@@ -99,7 +100,7 @@ export class AbstractTermType extends AbstractType {
99
100
  *
100
101
  * Considering the sub-expressions as a record instead of an array allows them to be selectively overridden by subclasses.
101
102
  */
102
- fromRdfExpressionChain({ variables, }) {
103
+ fromRdfResourceValuesExpressionChain({ variables, }) {
103
104
  let valueToExpression;
104
105
  if (this.in_.length > 0) {
105
106
  valueToExpression = code `value.toTerm([${joinCode(this.in_.map((in_) => this.rdfjsTermExpression(in_)), { on: ", " })}])`;
@@ -17,15 +17,15 @@ export declare abstract class AbstractType {
17
17
  */
18
18
  readonly comment: Maybe<string>;
19
19
  /**
20
- * Function that converts one or more source types to this type and returns Either<Error, ThisType>.
21
- *
22
- * The function takes two parameters:
23
- * - an instance of this.schemaType
24
- * - a value with one of the source types
20
+ * Function that takes a value of one or more source types to this type and returns Either<Error, ThisType>.
25
21
  *
26
22
  * The source types should include this type.
23
+ *
24
+ * The function should not perform validation (e.g., checking array lengths). That will be done by validationFunction in conjunction with this function.
25
+ *
26
+ * If unspecified, uses an identity function (i.e., function identity(value: ThisType): Either<Error, ThisType>).
27
27
  */
28
- abstract readonly conversionFunction: AbstractType.ConversionFunction;
28
+ abstract readonly conversionFunction: Maybe<AbstractType.ConversionFunction>;
29
29
  /**
30
30
  * The declaration of named types.
31
31
  */
@@ -98,6 +98,18 @@ export declare abstract class AbstractType {
98
98
  * JavaScript typeof(s) the type.
99
99
  */
100
100
  abstract readonly typeofs: readonly Typeof[];
101
+ /**
102
+ * Function that takes
103
+ * - a schema of this.schemaType
104
+ * - a value of this.type
105
+ *
106
+ * and validates the value against the schema, returning
107
+ * - Left(Error) if validation fails or
108
+ * - Right(the value) if validatios succeeds
109
+ *
110
+ * If unspecified, uses an identity function (i.e., function identity(schema: unknown, value: ThisType): Either<Error, ThisType>).
111
+ */
112
+ abstract readonly validationFunction: Maybe<Code>;
101
113
  /**
102
114
  * A ValueSparqlConstructTriplesFunction (reference or declaration) that returns an array of sparqljs.Triple's for a property value of this type.
103
115
  *
@@ -135,8 +147,7 @@ export declare abstract class AbstractType {
135
147
  kind: Code;
136
148
  };
137
149
  /**
138
- * An expression that converts this type's JSON type to a value of this type. It doesn't return a Either because the JSON has
139
- * already been validated and converted to the expected JSON type with Zod.
150
+ * An expression that converts this type's JSON type to an Either<Error, ThisType>.
140
151
  */
141
152
  abstract fromJsonExpression(parameters: {
142
153
  variables: {
@@ -10,6 +10,7 @@ export declare abstract class AbstractUnionType<MemberTypeT extends Type> extend
10
10
  private readonly discriminant;
11
11
  private readonly identifierType;
12
12
  readonly recursive: boolean;
13
+ readonly validationFunction: Maybe<Code>;
13
14
  constructor({ identifierType, members, recursive, ...superParameters }: {
14
15
  identifierType: Maybe<BlankNodeType | IdentifierType | IriType>;
15
16
  members: readonly (Pick<AbstractUnionType.Member<MemberTypeT>, "type"> & {
@@ -17,7 +18,7 @@ export declare abstract class AbstractUnionType<MemberTypeT extends Type> extend
17
18
  })[];
18
19
  recursive: boolean;
19
20
  } & ConstructorParameters<typeof AbstractType>[0]);
20
- get conversionFunction(): AbstractType.ConversionFunction;
21
+ get conversionFunction(): Maybe<AbstractType.ConversionFunction>;
21
22
  get discriminantProperty(): Maybe<AbstractType.DiscriminantProperty>;
22
23
  get members(): readonly AbstractUnionType.Member<MemberTypeT>[];
23
24
  get mutable(): boolean;
@@ -30,11 +31,11 @@ export declare abstract class AbstractUnionType<MemberTypeT extends Type> extend
30
31
  protected get inlineFilterType(): Code;
31
32
  protected get inlineFromJsonFunction(): Code;
32
33
  protected get inlineFromRdfResourceValuesFunction(): Code;
34
+ protected get inlineHashFunction(): Code;
33
35
  protected get inlineJsonSchema(): Code;
34
36
  protected get inlineJsonType(): AbstractType.JsonType;
35
37
  protected get inlineName(): Code;
36
38
  protected get inlineToJsonFunction(): Code;
37
- protected get inlineHashFunction(): Code;
38
39
  protected get inlineToRdfResourceValuesFunction(): Code;
39
40
  protected get inlineToStringFunction(): Code;
40
41
  protected get inlineValueSparqlConstructTriplesFunction(): Code;
@@ -14,6 +14,7 @@ export class AbstractUnionType extends AbstractType {
14
14
  discriminant;
15
15
  identifierType;
16
16
  recursive;
17
+ validationFunction = Maybe.empty();
17
18
  constructor({ identifierType, members, recursive, ...superParameters }) {
18
19
  super(superParameters);
19
20
  this.identifierType = identifierType;
@@ -130,8 +131,8 @@ export class AbstractUnionType extends AbstractType {
130
131
  });
131
132
  }
132
133
  get conversionFunction() {
133
- return {
134
- code: code `${this.reusables.snippets.convertToUnion}`,
134
+ return Maybe.of({
135
+ code: code `${this.reusables.snippets.identityConversionFunction}`,
135
136
  sourceTypes: this.discriminant.kind === "typeof"
136
137
  ? this.members.map(({ primaryDiscriminantValue, type }) => ({
137
138
  name: type.name,
@@ -143,7 +144,7 @@ export class AbstractUnionType extends AbstractType {
143
144
  typeof: "object",
144
145
  },
145
146
  ],
146
- };
147
+ });
147
148
  }
148
149
  get discriminantProperty() {
149
150
  switch (this.discriminant.kind) {
@@ -250,12 +251,12 @@ if (filter.on?.[${literalOf(primaryDiscriminantValue)}] !== undefined && ${typeC
250
251
  }
251
252
  get inlineFromJsonFunction() {
252
253
  return code `\
253
- ((value: ${this.jsonType().name}): ${this.name} => {
254
- ${joinCode(this.members.map(({ jsonType, jsonTypeCheck, type, unwrap, wrap }) => code `if (${jsonTypeCheck(code `value`)}) { return ${wrap(type.fromJsonExpression({
254
+ ((value: ${this.jsonType().name}): ${this.reusables.imports.Either}<Error, ${this.name}> => {
255
+ ${joinCode(this.members.map(({ jsonType, jsonTypeCheck, type, unwrap, wrap }) => code `if (${jsonTypeCheck(code `value`)}) { return ${type.fromJsonExpression({
255
256
  variables: {
256
257
  value: code `(${unwrap(code `value`)} as ${jsonType})`,
257
258
  },
258
- }))}; }`))}
259
+ })}.map(value => (${wrap(code `value`)})); }`))}
259
260
 
260
261
  throw new Error("unable to deserialize JSON");
261
262
  })`;
@@ -299,6 +300,13 @@ ${joinCode(this.members.map(({ jsonType, jsonTypeCheck, type, unwrap, wrap }) =>
299
300
  }, null)}.chain(values => values.head());
300
301
  }))
301
302
  ) satisfies ${this.reusables.snippets.FromRdfResourceValuesFunction}<${this.name}>)`;
303
+ }
304
+ get inlineHashFunction() {
305
+ return code `\
306
+ (<HasherT extends ${this.reusables.snippets.Hasher}>(hasher: HasherT, value: ${this.name}): HasherT => {
307
+ ${joinCode(this.members.map(({ type, typeCheck, unwrap }) => code `if (${typeCheck(code `value`)}) { return ${type.hashFunction}(hasher, ${unwrap(code `value`)}); }`))}
308
+ return hasher;
309
+ })`;
302
310
  }
303
311
  get inlineJsonSchema() {
304
312
  const discriminant = this.discriminant; // To get type narrowing to work
@@ -388,13 +396,6 @@ ${joinCode(this.members.map(({ jsonType, jsonTypeCheck, type, unwrap, wrap }) =>
388
396
  ${joinCode(this.members.map(({ typeCheck, typeToJsonExpression, unwrap, wrap }) => code `if (${typeCheck(code `value`)}) { return ${wrap(typeToJsonExpression(unwrap(code `value`)))}; }`))}
389
397
 
390
398
  throw new Error("unable to serialize to JSON");
391
- })`;
392
- }
393
- get inlineHashFunction() {
394
- return code `\
395
- (<HasherT extends ${this.reusables.snippets.Hasher}>(hasher: HasherT, value: ${this.name}): HasherT => {
396
- ${joinCode(this.members.map(({ type, typeCheck, unwrap }) => code `if (${typeCheck(code `value`)}) { return ${type.hashFunction}(hasher, ${unwrap(code `value`)}); }`))}
397
- return hasher;
398
399
  })`;
399
400
  }
400
401
  get inlineToRdfResourceValuesFunction() {
@@ -1,9 +1,10 @@
1
1
  import type { Literal } from "@rdfjs/types";
2
+ import { Maybe } from "purify-ts";
2
3
  import { AbstractLiteralType } from "./AbstractLiteralType.js";
3
4
  import { type Code } from "./ts-poet-wrapper.js";
4
5
  export declare class BigDecimalType extends AbstractLiteralType {
5
6
  readonly name: Code;
6
- readonly conversionFunction: AbstractLiteralType.ConversionFunction;
7
+ readonly conversionFunction: Maybe<AbstractLiteralType.ConversionFunction>;
7
8
  readonly filterFunction: Code;
8
9
  readonly filterType: Code;
9
10
  readonly hashFunction: Code;
@@ -18,6 +19,6 @@ export declare class BigDecimalType extends AbstractLiteralType {
18
19
  literalExpression(literal: Literal): Code;
19
20
  toJsonExpression({ variables, }: Parameters<AbstractLiteralType["toJsonExpression"]>[0]): Code;
20
21
  toRdfResourceValuesExpression({ variables, }: Parameters<AbstractLiteralType["toRdfResourceValuesExpression"]>[0]): Code;
21
- protected fromRdfExpressionChain({ variables, }: Parameters<AbstractLiteralType["fromRdfExpressionChain"]>[0]): ReturnType<AbstractLiteralType["fromRdfExpressionChain"]>;
22
+ protected fromRdfResourceValuesExpressionChain({ variables, }: Parameters<AbstractLiteralType["fromRdfResourceValuesExpressionChain"]>[0]): ReturnType<AbstractLiteralType["fromRdfResourceValuesExpressionChain"]>;
22
23
  }
23
24
  //# sourceMappingURL=BigDecimalType.d.ts.map