tsondb 0.2.0 → 0.3.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 (105) hide show
  1. package/lib/ModelContainer.js +9 -7
  2. package/lib/client/api.d.ts +14 -1
  3. package/lib/client/api.js +119 -0
  4. package/lib/client/components/Git.d.ts +2 -0
  5. package/lib/client/components/Git.js +116 -0
  6. package/lib/client/components/Layout.js +2 -1
  7. package/lib/client/components/typeInputs/ArrayTypeInput.js +2 -1
  8. package/lib/client/components/typeInputs/EnumTypeInput.d.ts +13 -0
  9. package/lib/client/components/typeInputs/{utils/EnumDeclField.js → EnumTypeInput.js} +10 -10
  10. package/lib/client/components/typeInputs/IncludeIdentifierTypeInput.js +1 -10
  11. package/lib/client/components/typeInputs/TypeInput.js +3 -0
  12. package/lib/client/components/typeInputs/utils/Markdown.js +1 -32
  13. package/lib/client/hooks/useAPIResource.d.ts +1 -0
  14. package/lib/client/hooks/useAPIResource.js +2 -0
  15. package/lib/client/hooks/useMappedAPIResource.d.ts +1 -0
  16. package/lib/client/hooks/useMappedAPIResource.js +19 -0
  17. package/lib/client/routes/Entity.js +18 -24
  18. package/lib/client/routes/Home.js +3 -12
  19. package/lib/client/utils/typeSkeleton.js +10 -16
  20. package/lib/renderers/jsonschema/index.d.ts +1 -1
  21. package/lib/renderers/jsonschema/index.js +30 -7
  22. package/lib/renderers/jsonschema/render.d.ts +5 -1
  23. package/lib/renderers/jsonschema/render.js +35 -16
  24. package/lib/renderers/ts/index.d.ts +1 -1
  25. package/lib/renderers/ts/index.js +37 -6
  26. package/lib/renderers/ts/render.d.ts +2 -0
  27. package/lib/renderers/ts/render.js +55 -32
  28. package/lib/schema/Node.d.ts +1 -0
  29. package/lib/schema/Node.js +6 -1
  30. package/lib/schema/declarations/Declaration.d.ts +3 -1
  31. package/lib/schema/declarations/Declaration.js +4 -0
  32. package/lib/schema/declarations/EntityDecl.d.ts +3 -0
  33. package/lib/schema/declarations/EnumDecl.d.ts +4 -21
  34. package/lib/schema/declarations/EnumDecl.js +16 -79
  35. package/lib/schema/index.d.ts +1 -0
  36. package/lib/schema/index.js +1 -0
  37. package/lib/schema/types/Type.d.ts +8 -2
  38. package/lib/schema/types/Type.js +57 -11
  39. package/lib/schema/types/generic/ArrayType.d.ts +2 -1
  40. package/lib/schema/types/generic/ArrayType.js +2 -1
  41. package/lib/schema/types/generic/EnumType.d.ts +38 -0
  42. package/lib/schema/types/generic/EnumType.js +96 -0
  43. package/lib/schema/types/generic/ObjectType.d.ts +2 -1
  44. package/lib/schema/types/generic/ObjectType.js +4 -0
  45. package/lib/schema/types/primitives/BooleanType.d.ts +2 -1
  46. package/lib/schema/types/primitives/BooleanType.js +1 -0
  47. package/lib/schema/types/primitives/DateType.d.ts +2 -1
  48. package/lib/schema/types/primitives/DateType.js +1 -0
  49. package/lib/schema/types/primitives/FloatType.d.ts +2 -1
  50. package/lib/schema/types/primitives/FloatType.js +1 -0
  51. package/lib/schema/types/primitives/IntegerType.d.ts +2 -1
  52. package/lib/schema/types/primitives/IntegerType.js +1 -0
  53. package/lib/schema/types/primitives/StringType.d.ts +2 -1
  54. package/lib/schema/types/primitives/StringType.js +1 -0
  55. package/lib/schema/types/references/GenericArgumentIdentifierType.d.ts +2 -1
  56. package/lib/schema/types/references/GenericArgumentIdentifierType.js +1 -0
  57. package/lib/schema/types/references/IncludeIdentifierType.d.ts +5 -3
  58. package/lib/schema/types/references/IncludeIdentifierType.js +15 -2
  59. package/lib/schema/types/references/NestedEntityMapType.d.ts +2 -1
  60. package/lib/schema/types/references/NestedEntityMapType.js +5 -1
  61. package/lib/schema/types/references/ReferenceIdentifierType.d.ts +2 -1
  62. package/lib/schema/types/references/ReferenceIdentifierType.js +2 -1
  63. package/lib/server/api/declarations.d.ts +1 -0
  64. package/lib/server/api/declarations.js +154 -0
  65. package/lib/server/api/git.d.ts +1 -0
  66. package/lib/server/api/git.js +174 -0
  67. package/lib/server/api/index.d.ts +1 -0
  68. package/lib/server/api/index.js +8 -0
  69. package/lib/server/api/instanceOperations.d.ts +6 -0
  70. package/lib/server/api/instanceOperations.js +82 -0
  71. package/lib/server/api/instances.d.ts +1 -0
  72. package/lib/server/api/instances.js +23 -0
  73. package/lib/server/index.d.ts +22 -1
  74. package/lib/server/index.js +11 -165
  75. package/lib/server/init.d.ts +5 -0
  76. package/lib/server/init.js +56 -0
  77. package/lib/shared/api.d.ts +12 -1
  78. package/lib/shared/utils/array.d.ts +19 -0
  79. package/lib/shared/utils/array.js +27 -0
  80. package/lib/shared/utils/git.d.ts +12 -0
  81. package/lib/shared/utils/git.js +98 -0
  82. package/lib/shared/utils/instances.d.ts +10 -0
  83. package/lib/shared/utils/instances.js +8 -1
  84. package/lib/shared/utils/markdown.d.ts +14 -0
  85. package/lib/shared/utils/markdown.js +42 -0
  86. package/lib/shared/utils/object.d.ts +1 -0
  87. package/lib/shared/utils/object.js +4 -0
  88. package/lib/shared/utils/string.d.ts +1 -0
  89. package/lib/shared/utils/string.js +9 -0
  90. package/lib/tsconfig.tsbuildinfo +1 -1
  91. package/lib/utils/git.d.ts +3 -0
  92. package/lib/utils/git.js +12 -0
  93. package/lib/utils/instances.d.ts +3 -2
  94. package/lib/utils/instances.js +9 -2
  95. package/lib/utils/path.d.ts +1 -0
  96. package/lib/utils/path.js +2 -0
  97. package/lib/utils/references.d.ts +7 -0
  98. package/lib/utils/references.js +40 -0
  99. package/lib/utils/render.d.ts +6 -1
  100. package/lib/utils/render.js +27 -1
  101. package/package.json +8 -1
  102. package/public/css/styles.css +200 -1
  103. package/lib/client/components/typeInputs/utils/EnumDeclField.d.ts +0 -13
  104. package/lib/server/instanceOperations.d.ts +0 -7
  105. package/lib/server/instanceOperations.js +0 -67
@@ -1,22 +1,17 @@
1
- import { discriminatorKey } from "../../shared/enum.js";
2
1
  import { Lazy } from "../../utils/lazy.js";
3
2
  import { NodeKind } from "../Node.js";
4
3
  import { serializeTypeParameter, } from "../parameters/TypeParameter.js";
5
- import { getReferencesForType, resolveTypeArgumentsInType, serializeType, validate, } from "../types/Type.js";
6
- import { getNestedDeclarations, getTypeArgumentsRecord, validateDeclName, } from "./Declaration.js";
4
+ import { EnumType, getNestedDeclarationsInEnumType, getReferencesForEnumType, resolveTypeArgumentsInEnumType, serializeEnumType, validateEnumType, } from "../types/generic/EnumType.js";
5
+ import { getTypeArgumentsRecord, validateDeclName, } from "./Declaration.js";
7
6
  export const GenEnumDecl = (sourceUrl, options) => {
8
7
  validateDeclName(options.name);
9
8
  const decl = {
10
9
  ...options,
11
10
  kind: NodeKind.EnumDecl,
12
11
  sourceUrl,
13
- values: Lazy.of(() => {
14
- const type = options.values(...options.parameters);
15
- Object.values(type).forEach(type => {
16
- if (type.type) {
17
- type.type.parent = decl;
18
- }
19
- });
12
+ type: Lazy.of(() => {
13
+ const type = EnumType(options.values(...options.parameters));
14
+ type.parent = decl;
20
15
  return type;
21
16
  }),
22
17
  };
@@ -30,13 +25,9 @@ export const EnumDecl = (sourceUrl, options) => {
30
25
  kind: NodeKind.EnumDecl,
31
26
  sourceUrl,
32
27
  parameters: [],
33
- values: Lazy.of(() => {
34
- const type = options.values();
35
- Object.values(type).forEach(type => {
36
- if (type.type) {
37
- type.type.parent = decl;
38
- }
39
- });
28
+ type: Lazy.of(() => {
29
+ const type = EnumType(options.values());
30
+ type.parent = decl;
40
31
  return type;
41
32
  }),
42
33
  };
@@ -44,72 +35,18 @@ export const EnumDecl = (sourceUrl, options) => {
44
35
  };
45
36
  export { EnumDecl as Enum };
46
37
  export const isEnumDecl = (node) => node.kind === NodeKind.EnumDecl;
47
- export const getNestedDeclarationsInEnumDecl = (addedDecls, decl) => Object.values(decl.values.value).reduce((acc, caseMember) => caseMember.type === null ? acc : getNestedDeclarations(acc, caseMember.type), addedDecls);
48
- export const validateEnumDecl = (helpers, decl, args, value) => {
49
- if (typeof value !== "object" || value === null || Array.isArray(value)) {
50
- return [TypeError(`expected an object, but got ${JSON.stringify(value)}`)];
51
- }
52
- const actualKeys = Object.keys(value);
53
- if (!(discriminatorKey in value) || typeof value[discriminatorKey] !== "string") {
54
- return [
55
- TypeError(`missing required discriminator value at key "${discriminatorKey}" of type string`),
56
- ];
57
- }
58
- const caseName = value[discriminatorKey];
59
- if (!(caseName in decl.values.value)) {
60
- return [TypeError(`discriminator "${caseName}" is not a valid enum case`)];
61
- }
62
- const unknownKeyErrors = actualKeys.flatMap(actualKey => actualKey === discriminatorKey || actualKey in decl.values.value
63
- ? []
64
- : [TypeError(`key "${actualKey}" is not the discriminator key or a valid enum case`)]);
65
- if (unknownKeyErrors.length > 0) {
66
- return unknownKeyErrors;
67
- }
68
- const associatedType = decl.values.value[caseName]?.type;
69
- if (associatedType != null) {
70
- if (!(caseName in value)) {
71
- return [TypeError(`missing required associated value for case "${caseName}"`)];
72
- }
73
- return validate(helpers, resolveTypeArgumentsInType(getTypeArgumentsRecord(decl, args), associatedType), value[caseName]);
74
- }
75
- return [];
76
- };
38
+ export const getNestedDeclarationsInEnumDecl = (addedDecls, decl) => getNestedDeclarationsInEnumType(addedDecls, decl.type.value);
39
+ export const validateEnumDecl = (helpers, decl, args, value) => validateEnumType(helpers, resolveTypeArgumentsInEnumType(getTypeArgumentsRecord(decl, args), decl.type.value), value);
77
40
  export const resolveTypeArgumentsInEnumDecl = (decl, args) => {
78
41
  const resolvedArgs = getTypeArgumentsRecord(decl, args);
79
42
  return EnumDecl(decl.sourceUrl, {
80
43
  ...decl,
81
- values: () => Object.fromEntries(Object.entries(decl.values.value).map(([key, { type, ...caseMember }]) => [
82
- key,
83
- {
84
- ...caseMember,
85
- type: type === null ? null : resolveTypeArgumentsInType(resolvedArgs, type),
86
- },
87
- ])),
44
+ values: () => resolveTypeArgumentsInEnumType(resolvedArgs, decl.type.value).values,
88
45
  });
89
46
  };
90
- export const EnumCaseDecl = (options) => ({
91
- ...options,
92
- kind: NodeKind.EnumCaseDecl,
93
- });
94
- export { EnumCaseDecl as EnumCase };
95
- export const serializeEnumDecl = type => ({
96
- ...type,
97
- values: Object.fromEntries(Object.entries(type.values.value).map(([key, caseMember]) => [
98
- key,
99
- {
100
- ...caseMember,
101
- type: caseMember.type === null ? null : serializeType(caseMember.type),
102
- },
103
- ])),
104
- parameters: type.parameters.map(param => serializeTypeParameter(param)),
47
+ export const serializeEnumDecl = decl => ({
48
+ ...decl,
49
+ type: serializeEnumType(decl.type.value),
50
+ parameters: decl.parameters.map(param => serializeTypeParameter(param)),
105
51
  });
106
- export const getReferencesForEnumDecl = (decl, value) => typeof value === "object" &&
107
- value !== null &&
108
- !Array.isArray(value) &&
109
- discriminatorKey in value &&
110
- typeof value[discriminatorKey] === "string" &&
111
- value[discriminatorKey] in decl.values.value &&
112
- decl.values.value[value[discriminatorKey]]?.type == null &&
113
- value[discriminatorKey] in value
114
- ? getReferencesForType(decl.values.value[value[discriminatorKey]].type, value[value[discriminatorKey]])
115
- : [];
52
+ export const getReferencesForEnumDecl = (decl, value) => getReferencesForEnumType(decl.type.value, value);
@@ -5,6 +5,7 @@ export * from "./declarations/TypeAliasDecl.js";
5
5
  export * from "./Node.js";
6
6
  export * from "./parameters/TypeParameter.js";
7
7
  export * from "./types/generic/ArrayType.js";
8
+ export { EnumCase } from "./types/generic/EnumType.js";
8
9
  export * from "./types/generic/ObjectType.js";
9
10
  export * from "./types/primitives/BooleanType.js";
10
11
  export * from "./types/primitives/DateType.js";
@@ -5,6 +5,7 @@ export * from "./declarations/TypeAliasDecl.js";
5
5
  export * from "./Node.js";
6
6
  export * from "./parameters/TypeParameter.js";
7
7
  export * from "./types/generic/ArrayType.js";
8
+ export { EnumCase } from "./types/generic/EnumType.js";
8
9
  export * from "./types/generic/ObjectType.js";
9
10
  export * from "./types/primitives/BooleanType.js";
10
11
  export * from "./types/primitives/DateType.js";
@@ -2,6 +2,7 @@ import { Decl } from "../declarations/Declaration.js";
2
2
  import { BaseNode, GetReferences, Serializer } from "../Node.js";
3
3
  import { Validator } from "../validation/type.js";
4
4
  import { ArrayType, SerializedArrayType } from "./generic/ArrayType.js";
5
+ import { EnumType, SerializedEnumType } from "./generic/EnumType.js";
5
6
  import { MemberDecl, ObjectType, SerializedMemberDecl, SerializedObjectType } from "./generic/ObjectType.js";
6
7
  import { BooleanType, SerializedBooleanType } from "./primitives/BooleanType.js";
7
8
  import { DateType, SerializedDateType } from "./primitives/DateType.js";
@@ -21,8 +22,8 @@ export interface BaseType extends BaseNode {
21
22
  }
22
23
  export interface SerializedBaseType extends BaseNode {
23
24
  }
24
- export type Type = PrimitiveType | ArrayType | ObjectType | GenericArgumentIdentifierType | ReferenceIdentifierType | IncludeIdentifierType | NestedEntityMapType;
25
- export type SerializedType = SerializedPrimitiveType | SerializedArrayType | SerializedObjectType | SerializedGenericArgumentIdentifierType | SerializedReferenceIdentifierType | SerializedIncludeIdentifierType | SerializedNestedEntityMapType;
25
+ export type Type = PrimitiveType | ArrayType | ObjectType | GenericArgumentIdentifierType | ReferenceIdentifierType | IncludeIdentifierType | NestedEntityMapType | EnumType;
26
+ export type SerializedType = SerializedPrimitiveType | SerializedArrayType | SerializedObjectType | SerializedGenericArgumentIdentifierType | SerializedReferenceIdentifierType | SerializedIncludeIdentifierType | SerializedNestedEntityMapType | SerializedEnumType;
26
27
  export declare const validate: Validator<Type>;
27
28
  export declare const resolveTypeArgumentsInType: <Args extends Record<string, Type>>(args: Args, type: Type) => Type;
28
29
  export declare function walkTypeNodeTree(callbackFn: (type: Type) => void, type: Type): void;
@@ -40,3 +41,8 @@ export declare const findTypeAtPath: (type: Type, path: string[]) => Type | unde
40
41
  export declare const serializeType: Serializer<Type, SerializedType>;
41
42
  export declare const removeParentKey: <T extends BaseType>(type: T) => Omit<T, "parent">;
42
43
  export declare const getReferencesForType: GetReferences<Type>;
44
+ /**
45
+ * Format the structure of a value to always look the same when serialized as JSON.
46
+ */
47
+ export type StructureFormatter<T extends Type> = (type: T, value: unknown) => unknown;
48
+ export declare const formatValue: StructureFormatter<Type>;
@@ -1,17 +1,18 @@
1
1
  import { assertExhaustive } from "../../shared/utils/typeSafety.js";
2
2
  import { isDecl } from "../declarations/Declaration.js";
3
3
  import { NodeKind } from "../Node.js";
4
- import { getReferencesForArrayType, resolveTypeArgumentsInArrayType, serializeArrayType, validateArrayType, } from "./generic/ArrayType.js";
5
- import { getReferencesForObjectType, resolveTypeArgumentsInObjectType, serializeObjectType, validateObjectType, } from "./generic/ObjectType.js";
6
- import { getReferencesForBooleanType, serializeBooleanType, validateBooleanType, } from "./primitives/BooleanType.js";
7
- import { getReferencesForDateType, serializeDateType, validateDateType, } from "./primitives/DateType.js";
8
- import { getReferencesForFloatType, serializeFloatType, validateFloatType, } from "./primitives/FloatType.js";
9
- import { getReferencesForIntegerType, serializeIntegerType, validateIntegerType, } from "./primitives/IntegerType.js";
10
- import { getReferencesForStringType, serializeStringType, validateStringType, } from "./primitives/StringType.js";
11
- import { getReferencesForGenericArgumentIdentifierType, resolveTypeArgumentsInGenericArgumentIdentifierType, serializeGenericArgumentIdentifierType, validateGenericArgumentIdentifierType, } from "./references/GenericArgumentIdentifierType.js";
12
- import { getReferencesForIncludeIdentifierType, resolveTypeArgumentsInIncludeIdentifierType, serializeIncludeIdentifierType, validateIncludeIdentifierType, } from "./references/IncludeIdentifierType.js";
13
- import { getReferencesForNestedEntityMapType, resolveTypeArgumentsInNestedEntityMapType, serializeNestedEntityMapType, validateNestedEntityMapType, } from "./references/NestedEntityMapType.js";
14
- import { getReferencesForReferenceIdentifierType, resolveTypeArgumentsInReferenceIdentifierType, serializeReferenceIdentifierType, validateReferenceIdentifierType, } from "./references/ReferenceIdentifierType.js";
4
+ import { formatArrayValue, getReferencesForArrayType, resolveTypeArgumentsInArrayType, serializeArrayType, validateArrayType, } from "./generic/ArrayType.js";
5
+ import { formatEnumType, getReferencesForEnumType, resolveTypeArgumentsInEnumType, serializeEnumType, validateEnumType, } from "./generic/EnumType.js";
6
+ import { formatObjectValue, getReferencesForObjectType, resolveTypeArgumentsInObjectType, serializeObjectType, validateObjectType, } from "./generic/ObjectType.js";
7
+ import { formatBooleanValue, getReferencesForBooleanType, serializeBooleanType, validateBooleanType, } from "./primitives/BooleanType.js";
8
+ import { formatDateValue, getReferencesForDateType, serializeDateType, validateDateType, } from "./primitives/DateType.js";
9
+ import { formatFloatValue, getReferencesForFloatType, serializeFloatType, validateFloatType, } from "./primitives/FloatType.js";
10
+ import { formatIntegerValue, getReferencesForIntegerType, serializeIntegerType, validateIntegerType, } from "./primitives/IntegerType.js";
11
+ import { formatStringValue, getReferencesForStringType, serializeStringType, validateStringType, } from "./primitives/StringType.js";
12
+ import { formatGenericArgumentIdentifierValue, getReferencesForGenericArgumentIdentifierType, resolveTypeArgumentsInGenericArgumentIdentifierType, serializeGenericArgumentIdentifierType, validateGenericArgumentIdentifierType, } from "./references/GenericArgumentIdentifierType.js";
13
+ import { formatIncludeIdentifierValue, getReferencesForIncludeIdentifierType, resolveTypeArgumentsInIncludeIdentifierType, serializeIncludeIdentifierType, validateIncludeIdentifierType, } from "./references/IncludeIdentifierType.js";
14
+ import { formatNestedEntityMapValue, getReferencesForNestedEntityMapType, resolveTypeArgumentsInNestedEntityMapType, serializeNestedEntityMapType, validateNestedEntityMapType, } from "./references/NestedEntityMapType.js";
15
+ import { formatReferenceIdentifierValue, getReferencesForReferenceIdentifierType, resolveTypeArgumentsInReferenceIdentifierType, serializeReferenceIdentifierType, validateReferenceIdentifierType, } from "./references/ReferenceIdentifierType.js";
15
16
  export const validate = (helpers, type, value) => {
16
17
  switch (type.kind) {
17
18
  case NodeKind.ArrayType:
@@ -36,6 +37,8 @@ export const validate = (helpers, type, value) => {
36
37
  return validateIncludeIdentifierType(helpers, type, value);
37
38
  case NodeKind.NestedEntityMapType:
38
39
  return validateNestedEntityMapType(helpers, type, value);
40
+ case NodeKind.EnumType:
41
+ return validateEnumType(helpers, type, value);
39
42
  default:
40
43
  return assertExhaustive(type);
41
44
  }
@@ -60,6 +63,8 @@ export const resolveTypeArgumentsInType = (args, type) => {
60
63
  return resolveTypeArgumentsInIncludeIdentifierType(args, type);
61
64
  case NodeKind.NestedEntityMapType:
62
65
  return resolveTypeArgumentsInNestedEntityMapType(args, type);
66
+ case NodeKind.EnumType:
67
+ return resolveTypeArgumentsInEnumType(args, type);
63
68
  default:
64
69
  return assertExhaustive(type);
65
70
  }
@@ -84,6 +89,13 @@ export function walkTypeNodeTree(callbackFn, type) {
84
89
  case NodeKind.ReferenceIdentifierType:
85
90
  case NodeKind.IncludeIdentifierType:
86
91
  return callbackFn(type);
92
+ case NodeKind.EnumType:
93
+ callbackFn(type);
94
+ return Object.values(type.values).forEach(value => {
95
+ if (value.type) {
96
+ walkTypeNodeTree(callbackFn, value.type);
97
+ }
98
+ });
87
99
  default:
88
100
  return assertExhaustive(type);
89
101
  }
@@ -139,6 +151,8 @@ export const serializeType = type => {
139
151
  return serializeIncludeIdentifierType(type);
140
152
  case NodeKind.NestedEntityMapType:
141
153
  return serializeNestedEntityMapType(type);
154
+ case NodeKind.EnumType:
155
+ return serializeEnumType(type);
142
156
  default:
143
157
  return assertExhaustive(type);
144
158
  }
@@ -171,6 +185,38 @@ export const getReferencesForType = (type, value) => {
171
185
  return getReferencesForIncludeIdentifierType(type, value);
172
186
  case NodeKind.NestedEntityMapType:
173
187
  return getReferencesForNestedEntityMapType(type, value);
188
+ case NodeKind.EnumType:
189
+ return getReferencesForEnumType(type, value);
190
+ default:
191
+ return assertExhaustive(type);
192
+ }
193
+ };
194
+ export const formatValue = (type, value) => {
195
+ switch (type.kind) {
196
+ case NodeKind.ArrayType:
197
+ return formatArrayValue(type, value);
198
+ case NodeKind.ObjectType:
199
+ return formatObjectValue(type, value);
200
+ case NodeKind.BooleanType:
201
+ return formatBooleanValue(type, value);
202
+ case NodeKind.DateType:
203
+ return formatDateValue(type, value);
204
+ case NodeKind.FloatType:
205
+ return formatFloatValue(type, value);
206
+ case NodeKind.IntegerType:
207
+ return formatIntegerValue(type, value);
208
+ case NodeKind.StringType:
209
+ return formatStringValue(type, value);
210
+ case NodeKind.GenericArgumentIdentifierType:
211
+ return formatGenericArgumentIdentifierValue(type, value);
212
+ case NodeKind.IncludeIdentifierType:
213
+ return formatIncludeIdentifierValue(type, value);
214
+ case NodeKind.NestedEntityMapType:
215
+ return formatNestedEntityMapValue(type, value);
216
+ case NodeKind.ReferenceIdentifierType:
217
+ return formatReferenceIdentifierValue(type, value);
218
+ case NodeKind.EnumType:
219
+ return formatEnumType(type, value);
174
220
  default:
175
221
  return assertExhaustive(type);
176
222
  }
@@ -1,7 +1,7 @@
1
1
  import { GetNestedDeclarations } from "../../declarations/Declaration.js";
2
2
  import { GetReferences, Node, NodeKind, Serializer } from "../../Node.js";
3
3
  import { Validator } from "../../validation/type.js";
4
- import { BaseType, SerializedBaseType, SerializedType, Type } from "../Type.js";
4
+ import { BaseType, SerializedBaseType, SerializedType, StructureFormatter, Type } from "../Type.js";
5
5
  export interface ArrayType<T extends Type = Type> extends BaseType {
6
6
  kind: NodeKind["ArrayType"];
7
7
  minItems?: number;
@@ -28,3 +28,4 @@ export declare const validateArrayType: Validator<ArrayType>;
28
28
  export declare const resolveTypeArgumentsInArrayType: (args: Record<string, Type>, type: ArrayType) => ArrayType;
29
29
  export declare const serializeArrayType: Serializer<ArrayType, SerializedArrayType>;
30
30
  export declare const getReferencesForArrayType: GetReferences<ArrayType>;
31
+ export declare const formatArrayValue: StructureFormatter<ArrayType>;
@@ -4,7 +4,7 @@ import { wrapErrorsIfAny } from "../../../utils/error.js";
4
4
  import { getNestedDeclarations } from "../../declarations/Declaration.js";
5
5
  import { NodeKind } from "../../Node.js";
6
6
  import { validateOption } from "../../validation/options.js";
7
- import { getReferencesForType, removeParentKey, resolveTypeArgumentsInType, serializeType, validate, } from "../Type.js";
7
+ import { formatValue, getReferencesForType, removeParentKey, resolveTypeArgumentsInType, serializeType, validate, } from "../Type.js";
8
8
  export const ArrayType = (items, options = {}) => {
9
9
  const type = {
10
10
  ...options,
@@ -36,3 +36,4 @@ export const serializeArrayType = type => ({
36
36
  items: serializeType(type.items),
37
37
  });
38
38
  export const getReferencesForArrayType = (type, value) => Array.isArray(value) ? value.flatMap(item => getReferencesForType(type.items, item)) : [];
39
+ export const formatArrayValue = (type, value) => Array.isArray(value) ? value.map(item => formatValue(type.items, item)) : value;
@@ -0,0 +1,38 @@
1
+ import { GetNestedDeclarations } from "../../declarations/Declaration.js";
2
+ import { GetReferences, Node, NodeKind, Serializer } from "../../Node.js";
3
+ import { Validator } from "../../validation/type.js";
4
+ import { BaseType, SerializedBaseType, SerializedType, StructureFormatter, Type } from "../Type.js";
5
+ export interface EnumType<T extends Record<string, EnumCaseDecl> = Record<string, EnumCaseDecl>> extends BaseType {
6
+ kind: NodeKind["EnumType"];
7
+ values: T;
8
+ }
9
+ export interface SerializedEnumType<T extends Record<string, SerializedEnumCaseDecl> = Record<string, SerializedEnumCaseDecl>> extends SerializedBaseType {
10
+ kind: NodeKind["EnumType"];
11
+ values: T;
12
+ }
13
+ export declare const EnumType: <T extends Record<string, EnumCaseDecl> = Record<string, EnumCaseDecl>>(values: T) => EnumType<T>;
14
+ export declare const isEnumType: (node: Node) => node is EnumType;
15
+ export declare const getNestedDeclarationsInEnumType: GetNestedDeclarations<EnumType>;
16
+ export declare const validateEnumType: Validator<EnumType>;
17
+ export declare const resolveTypeArgumentsInEnumType: (args: Record<string, Type>, type: EnumType) => EnumType;
18
+ export interface EnumCaseDecl<T extends Type | null = Type | null> {
19
+ kind: NodeKind["EnumCaseDecl"];
20
+ type: T;
21
+ comment?: string;
22
+ isDeprecated?: boolean;
23
+ }
24
+ export interface SerializedEnumCaseDecl<T extends SerializedType | null = SerializedType | null> {
25
+ kind: NodeKind["EnumCaseDecl"];
26
+ type: T;
27
+ comment?: string;
28
+ isDeprecated?: boolean;
29
+ }
30
+ export declare const EnumCaseDecl: <T extends Type | null>(options: {
31
+ type: T;
32
+ comment?: string;
33
+ isDeprecated?: boolean;
34
+ }) => EnumCaseDecl<T>;
35
+ export { EnumCaseDecl as EnumCase };
36
+ export declare const serializeEnumType: Serializer<EnumType, SerializedEnumType>;
37
+ export declare const getReferencesForEnumType: GetReferences<EnumType>;
38
+ export declare const formatEnumType: StructureFormatter<EnumType>;
@@ -0,0 +1,96 @@
1
+ import { discriminatorKey } from "../../../shared/enum.js";
2
+ import { getNestedDeclarations } from "../../declarations/Declaration.js";
3
+ import { NodeKind } from "../../Node.js";
4
+ import { formatValue, getReferencesForType, removeParentKey, resolveTypeArgumentsInType, serializeType, validate, } from "../Type.js";
5
+ export const EnumType = (values) => {
6
+ const type = {
7
+ kind: NodeKind.EnumType,
8
+ values,
9
+ };
10
+ type.values = Object.fromEntries(Object.entries(values).map(([caseName, caseDef]) => [
11
+ caseName,
12
+ { ...caseDef, type: caseDef.type === null ? null : { ...caseDef.type, parent: type } },
13
+ ]));
14
+ return type;
15
+ };
16
+ export const isEnumType = (node) => node.kind === NodeKind.EnumType;
17
+ export const getNestedDeclarationsInEnumType = (addedDecls, type) => Object.values(type.values).reduce((acc, caseMember) => caseMember.type === null ? acc : getNestedDeclarations(acc, caseMember.type), addedDecls);
18
+ export const validateEnumType = (helpers, type, value) => {
19
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
20
+ return [TypeError(`expected an object, but got ${JSON.stringify(value)}`)];
21
+ }
22
+ const actualKeys = Object.keys(value);
23
+ if (!(discriminatorKey in value) || typeof value[discriminatorKey] !== "string") {
24
+ return [
25
+ TypeError(`missing required discriminator value at key "${discriminatorKey}" of type string`),
26
+ ];
27
+ }
28
+ const caseName = value[discriminatorKey];
29
+ if (!(caseName in type.values)) {
30
+ return [TypeError(`discriminator "${caseName}" is not a valid enum case`)];
31
+ }
32
+ const unknownKeyErrors = actualKeys.flatMap(actualKey => actualKey === discriminatorKey || actualKey in type.values
33
+ ? []
34
+ : [TypeError(`key "${actualKey}" is not the discriminator key or a valid enum case`)]);
35
+ if (unknownKeyErrors.length > 0) {
36
+ return unknownKeyErrors;
37
+ }
38
+ const associatedType = type.values[caseName]?.type;
39
+ if (associatedType != null) {
40
+ if (!(caseName in value)) {
41
+ return [TypeError(`missing required associated value for case "${caseName}"`)];
42
+ }
43
+ return validate(helpers, associatedType, value[caseName]);
44
+ }
45
+ return [];
46
+ };
47
+ export const resolveTypeArgumentsInEnumType = (args, type) => EnumType(Object.fromEntries(Object.entries(type.values).map(([key, { type, ...caseMember }]) => [
48
+ key,
49
+ {
50
+ ...caseMember,
51
+ type: type === null ? null : resolveTypeArgumentsInType(args, type),
52
+ },
53
+ ])));
54
+ export const EnumCaseDecl = (options) => ({
55
+ ...options,
56
+ kind: NodeKind.EnumCaseDecl,
57
+ });
58
+ export { EnumCaseDecl as EnumCase };
59
+ export const serializeEnumType = type => ({
60
+ ...removeParentKey(type),
61
+ values: Object.fromEntries(Object.entries(type.values).map(([key, caseMember]) => [
62
+ key,
63
+ {
64
+ ...caseMember,
65
+ type: caseMember.type === null ? null : serializeType(caseMember.type),
66
+ },
67
+ ])),
68
+ });
69
+ export const getReferencesForEnumType = (type, value) => typeof value === "object" &&
70
+ value !== null &&
71
+ !Array.isArray(value) &&
72
+ discriminatorKey in value &&
73
+ typeof value[discriminatorKey] === "string" &&
74
+ value[discriminatorKey] in type.values &&
75
+ type.values[value[discriminatorKey]]?.type == null &&
76
+ value[discriminatorKey] in value
77
+ ? getReferencesForType(type.values[value[discriminatorKey]].type, value[value[discriminatorKey]])
78
+ : [];
79
+ export const formatEnumType = (type, value) => {
80
+ if (typeof value === "object" &&
81
+ value !== null &&
82
+ !Array.isArray(value) &&
83
+ discriminatorKey in value &&
84
+ typeof value[discriminatorKey] === "string") {
85
+ const caseName = value[discriminatorKey];
86
+ const caseValue = value[caseName];
87
+ const caseType = type.values[caseName]?.type;
88
+ return {
89
+ [discriminatorKey]: caseName,
90
+ ...(caseValue == null || caseType == null
91
+ ? {}
92
+ : { [caseName]: formatValue(caseType, caseValue) }),
93
+ };
94
+ }
95
+ return value;
96
+ };
@@ -2,7 +2,7 @@ import { ObjectConstraints } from "../../../shared/validation/object.js";
2
2
  import { GetNestedDeclarations } from "../../declarations/Declaration.js";
3
3
  import { GetReferences, Node, NodeKind, Serializer } from "../../Node.js";
4
4
  import { Validator } from "../../validation/type.js";
5
- import { BaseType, SerializedBaseType, SerializedType, Type } from "../Type.js";
5
+ import { BaseType, SerializedBaseType, SerializedType, StructureFormatter, Type } from "../Type.js";
6
6
  type TConstraint = Record<string, MemberDecl<Type, boolean>>;
7
7
  export interface ObjectType<T extends TConstraint = TConstraint> extends BaseType, ObjectConstraints {
8
8
  kind: NodeKind["ObjectType"];
@@ -49,3 +49,4 @@ export declare const Optional: <T extends Type>(options: {
49
49
  }) => MemberDecl<T, false>;
50
50
  export declare const serializeObjectType: Serializer<ObjectType, SerializedObjectType>;
51
51
  export declare const getReferencesForObjectType: GetReferences<ObjectType>;
52
+ export declare const formatObjectValue: StructureFormatter<ObjectType>;
@@ -1,3 +1,4 @@
1
+ import { sortObjectKeys } from "../../../shared/utils/object.js";
1
2
  import { parallelizeErrors } from "../../../shared/utils/validation.js";
2
3
  import { validateObjectConstraints } from "../../../shared/validation/object.js";
3
4
  import { wrapErrorsIfAny } from "../../../utils/error.js";
@@ -69,3 +70,6 @@ export const serializeObjectType = type => ({
69
70
  export const getReferencesForObjectType = (type, value) => typeof value === "object" && value !== null
70
71
  ? Object.entries(value).flatMap(([key, propValue]) => key in type.properties ? getReferencesForType(type.properties[key].type, propValue) : [])
71
72
  : [];
73
+ export const formatObjectValue = (type, value) => typeof value === "object" && value !== null && !Array.isArray(value)
74
+ ? sortObjectKeys(value, Object.keys(type.properties))
75
+ : value;
@@ -1,6 +1,6 @@
1
1
  import { GetReferences, Node, NodeKind, Serializer } from "../../Node.js";
2
2
  import { Validator } from "../../validation/type.js";
3
- import { BaseType, SerializedBaseType } from "../Type.js";
3
+ import { BaseType, SerializedBaseType, StructureFormatter } from "../Type.js";
4
4
  export interface BooleanType extends BaseType {
5
5
  kind: NodeKind["BooleanType"];
6
6
  }
@@ -13,3 +13,4 @@ export declare const isBooleanType: (node: Node) => node is BooleanType;
13
13
  export declare const validateBooleanType: Validator<BooleanType>;
14
14
  export declare const serializeBooleanType: Serializer<BooleanType, SerializedBooleanType>;
15
15
  export declare const getReferencesForBooleanType: GetReferences<BooleanType>;
16
+ export declare const formatBooleanValue: StructureFormatter<BooleanType>;
@@ -13,3 +13,4 @@ export const validateBooleanType = (_helpers, _type, value) => {
13
13
  };
14
14
  export const serializeBooleanType = type => removeParentKey(type);
15
15
  export const getReferencesForBooleanType = (_type, _value) => [];
16
+ export const formatBooleanValue = (_type, value) => value;
@@ -1,7 +1,7 @@
1
1
  import { DateConstraints } from "../../../shared/validation/date.js";
2
2
  import { GetReferences, Node, NodeKind, Serializer } from "../../Node.js";
3
3
  import { Validator } from "../../validation/type.js";
4
- import { BaseType, SerializedBaseType } from "../Type.js";
4
+ import { BaseType, SerializedBaseType, StructureFormatter } from "../Type.js";
5
5
  export interface DateType extends BaseType, DateConstraints {
6
6
  kind: NodeKind["DateType"];
7
7
  }
@@ -14,3 +14,4 @@ export declare const isDateType: (node: Node) => node is DateType;
14
14
  export declare const validateDateType: Validator<DateType>;
15
15
  export declare const serializeDateType: Serializer<DateType, SerializedDateType>;
16
16
  export declare const getReferencesForDateType: GetReferences<DateType>;
17
+ export declare const formatDateValue: StructureFormatter<DateType>;
@@ -15,3 +15,4 @@ export const validateDateType = (_helpers, type, value) => {
15
15
  };
16
16
  export const serializeDateType = type => removeParentKey(type);
17
17
  export const getReferencesForDateType = (_type, _value) => [];
18
+ export const formatDateValue = (_type, value) => value;
@@ -1,7 +1,7 @@
1
1
  import { RangeBound } from "../../../shared/validation/number.js";
2
2
  import { GetReferences, Node, NodeKind, Serializer } from "../../Node.js";
3
3
  import { Validator } from "../../validation/type.js";
4
- import { BaseType, SerializedBaseType } from "../Type.js";
4
+ import { BaseType, SerializedBaseType, StructureFormatter } from "../Type.js";
5
5
  export interface FloatType extends BaseType {
6
6
  kind: NodeKind["FloatType"];
7
7
  minimum?: RangeBound;
@@ -24,3 +24,4 @@ export declare const isFloatType: (node: Node) => node is FloatType;
24
24
  export declare const validateFloatType: Validator<FloatType>;
25
25
  export declare const serializeFloatType: Serializer<FloatType, SerializedFloatType>;
26
26
  export declare const getReferencesForFloatType: GetReferences<FloatType>;
27
+ export declare const formatFloatValue: StructureFormatter<FloatType>;
@@ -15,3 +15,4 @@ export const validateFloatType = (_helpers, type, value) => {
15
15
  };
16
16
  export const serializeFloatType = type => removeParentKey(type);
17
17
  export const getReferencesForFloatType = (_type, _value) => [];
18
+ export const formatFloatValue = (_type, value) => value;
@@ -1,7 +1,7 @@
1
1
  import { RangeBound } from "../../../shared/validation/number.js";
2
2
  import { GetReferences, Node, NodeKind, Serializer } from "../../Node.js";
3
3
  import { Validator } from "../../validation/type.js";
4
- import { BaseType, SerializedBaseType } from "../Type.js";
4
+ import { BaseType, SerializedBaseType, StructureFormatter } from "../Type.js";
5
5
  export interface IntegerType extends BaseType {
6
6
  kind: NodeKind["IntegerType"];
7
7
  minimum?: RangeBound;
@@ -24,3 +24,4 @@ export declare const isIntegerType: (node: Node) => node is IntegerType;
24
24
  export declare const validateIntegerType: Validator<IntegerType>;
25
25
  export declare const serializeIntegerType: Serializer<IntegerType, SerializedIntegerType>;
26
26
  export declare const getReferencesForIntegerType: GetReferences<IntegerType>;
27
+ export declare const formatIntegerValue: StructureFormatter<IntegerType>;
@@ -19,3 +19,4 @@ export const validateIntegerType = (_helpers, type, value) => {
19
19
  };
20
20
  export const serializeIntegerType = type => removeParentKey(type);
21
21
  export const getReferencesForIntegerType = (_type, _value) => [];
22
+ export const formatIntegerValue = (_type, value) => value;
@@ -1,7 +1,7 @@
1
1
  import { StringConstraints } from "../../../shared/validation/string.js";
2
2
  import { GetReferences, Node, NodeKind, Serializer } from "../../Node.js";
3
3
  import { Validator } from "../../validation/type.js";
4
- import { BaseType, SerializedBaseType } from "../Type.js";
4
+ import { BaseType, SerializedBaseType, StructureFormatter } from "../Type.js";
5
5
  export interface StringType extends BaseType, StringConstraints {
6
6
  kind: NodeKind["StringType"];
7
7
  pattern?: RegExp;
@@ -23,3 +23,4 @@ export declare const isStringType: (node: Node) => node is StringType;
23
23
  export declare const validateStringType: Validator<StringType>;
24
24
  export declare const serializeStringType: Serializer<StringType, SerializedStringType>;
25
25
  export declare const getReferencesForStringType: GetReferences<StringType>;
26
+ export declare const formatStringValue: StructureFormatter<StringType>;
@@ -18,3 +18,4 @@ export const serializeStringType = type => removeParentKey({
18
18
  pattern: type.pattern?.source,
19
19
  });
20
20
  export const getReferencesForStringType = (_type, _value) => [];
21
+ export const formatStringValue = (_type, value) => value;
@@ -1,7 +1,7 @@
1
1
  import { GetReferences, Node, NodeKind, Serializer } from "../../Node.js";
2
2
  import { SerializedTypeParameter, TypeParameter } from "../../parameters/TypeParameter.js";
3
3
  import { Validator } from "../../validation/type.js";
4
- import { BaseType, SerializedBaseType, Type } from "../Type.js";
4
+ import { BaseType, SerializedBaseType, StructureFormatter, Type } from "../Type.js";
5
5
  type TConstraint = TypeParameter;
6
6
  export interface GenericArgumentIdentifierType<T extends TConstraint = TConstraint> extends BaseType {
7
7
  kind: NodeKind["GenericArgumentIdentifierType"];
@@ -19,3 +19,4 @@ export declare const validateGenericArgumentIdentifierType: Validator<GenericArg
19
19
  export declare const resolveTypeArgumentsInGenericArgumentIdentifierType: <T extends TConstraint, Args extends Record<string, Type>>(args: Args, type: GenericArgumentIdentifierType<T>) => Args[T["name"]];
20
20
  export declare const serializeGenericArgumentIdentifierType: Serializer<GenericArgumentIdentifierType, SerializedGenericArgumentIdentifierType>;
21
21
  export declare const getReferencesForGenericArgumentIdentifierType: GetReferences<GenericArgumentIdentifierType>;
22
+ export declare const formatGenericArgumentIdentifierValue: StructureFormatter<GenericArgumentIdentifierType>;
@@ -16,3 +16,4 @@ export const serializeGenericArgumentIdentifierType = type => ({
16
16
  argument: serializeTypeParameter(type.argument),
17
17
  });
18
18
  export const getReferencesForGenericArgumentIdentifierType = (_type, _value) => [];
19
+ export const formatGenericArgumentIdentifierValue = (_type, value) => value;
@@ -1,10 +1,11 @@
1
1
  import { GetNestedDeclarations, SerializedTypeArguments, TypeArguments } from "../../declarations/Declaration.js";
2
- import { EnumCaseDecl, EnumDecl } from "../../declarations/EnumDecl.js";
2
+ import { EnumDecl } from "../../declarations/EnumDecl.js";
3
3
  import { TypeAliasDecl } from "../../declarations/TypeAliasDecl.js";
4
4
  import { GetReferences, Node, NodeKind, Serializer } from "../../Node.js";
5
5
  import { SerializedTypeParameter, TypeParameter } from "../../parameters/TypeParameter.js";
6
6
  import { Validator } from "../../validation/type.js";
7
- import { BaseType, SerializedBaseType, Type } from "../Type.js";
7
+ import { EnumCaseDecl } from "../generic/EnumType.js";
8
+ import { BaseType, SerializedBaseType, StructureFormatter, Type } from "../Type.js";
8
9
  type TConstraint<Params extends TypeParameter[]> = TypeAliasDecl<string, Type, Params> | EnumDecl<string, Record<string, EnumCaseDecl>, Params>;
9
10
  export interface IncludeIdentifierType<Params extends TypeParameter[] = TypeParameter[], T extends TConstraint<Params> = TConstraint<Params>> extends BaseType {
10
11
  kind: NodeKind["IncludeIdentifierType"];
@@ -23,6 +24,7 @@ export { IncludeIdentifierType as IncludeIdentifier };
23
24
  export declare const isIncludeIdentifierType: (node: Node) => node is IncludeIdentifierType;
24
25
  export declare const getNestedDeclarationsInIncludeIdentifierType: GetNestedDeclarations<IncludeIdentifierType>;
25
26
  export declare const validateIncludeIdentifierType: Validator<IncludeIdentifierType>;
26
- export declare const resolveTypeArgumentsInIncludeIdentifierType: (args: Record<string, Type>, type: IncludeIdentifierType) => IncludeIdentifierType;
27
+ export declare const resolveTypeArgumentsInIncludeIdentifierType: (args: Record<string, Type>, type: IncludeIdentifierType) => Type;
27
28
  export declare const serializeIncludeIdentifierType: Serializer<IncludeIdentifierType, SerializedIncludeIdentifierType>;
28
29
  export declare const getReferencesForIncludeIdentifierType: GetReferences<IncludeIdentifierType>;
30
+ export declare const formatIncludeIdentifierValue: StructureFormatter<IncludeIdentifierType>;