@contember/binding 1.0.0 → 1.1.0-alpha.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.
- package/dist/development/core/DataBinding.js +18 -17
- package/dist/development/core/DataBinding.js.map +1 -1
- package/dist/development/core/MarkerComparator.js +1 -1
- package/dist/development/core/MarkerMerger.js +1 -4
- package/dist/development/core/MarkerMerger.js.map +1 -1
- package/dist/development/core/StateInitializer.js +1 -3
- package/dist/development/core/StateInitializer.js.map +1 -1
- package/dist/development/core/VariableInputTransformer.js +4 -1
- package/dist/development/core/VariableInputTransformer.js.map +1 -1
- package/dist/development/core/operations/EntityOperations.js +1 -1
- package/dist/development/core/schema/Schema.js +3 -0
- package/dist/development/core/schema/Schema.js.map +1 -1
- package/dist/development/coreComponents/EntityListSubTree.js +4 -12
- package/dist/development/coreComponents/EntityListSubTree.js.map +1 -1
- package/dist/development/coreComponents/EntitySubTree.js +4 -22
- package/dist/development/coreComponents/EntitySubTree.js.map +1 -1
- package/dist/development/coreComponents/Field.js +5 -1
- package/dist/development/coreComponents/Field.js.map +1 -1
- package/dist/development/coreComponents/HasMany.js +3 -5
- package/dist/development/coreComponents/HasMany.js.map +1 -1
- package/dist/development/coreComponents/HasOne.js +3 -5
- package/dist/development/coreComponents/HasOne.js.map +1 -1
- package/dist/development/dao/Environment.js +147 -94
- package/dist/development/dao/Environment.js.map +1 -1
- package/dist/development/dao/TreeNodeEnvironmentFactory.js +159 -0
- package/dist/development/dao/TreeNodeEnvironmentFactory.js.map +1 -0
- package/dist/development/queryLanguage/MarkerFactory.js +4 -3
- package/dist/development/queryLanguage/MarkerFactory.js.map +1 -1
- package/dist/development/queryLanguage/Parser.js +18 -6
- package/dist/development/queryLanguage/Parser.js.map +1 -1
- package/dist/production/core/DataBinding.js +18 -8
- package/dist/production/core/DataBinding.js.map +1 -1
- package/dist/production/core/MarkerComparator.js +1 -1
- package/dist/production/core/MarkerMerger.js +1 -4
- package/dist/production/core/MarkerMerger.js.map +1 -1
- package/dist/production/core/StateInitializer.js +1 -3
- package/dist/production/core/StateInitializer.js.map +1 -1
- package/dist/production/core/VariableInputTransformer.js +4 -1
- package/dist/production/core/VariableInputTransformer.js.map +1 -1
- package/dist/production/core/operations/EntityOperations.js +1 -1
- package/dist/production/core/schema/Schema.js +3 -0
- package/dist/production/core/schema/Schema.js.map +1 -1
- package/dist/production/coreComponents/EntityListSubTree.js +4 -12
- package/dist/production/coreComponents/EntityListSubTree.js.map +1 -1
- package/dist/production/coreComponents/EntitySubTree.js +4 -22
- package/dist/production/coreComponents/EntitySubTree.js.map +1 -1
- package/dist/production/coreComponents/Field.js +5 -1
- package/dist/production/coreComponents/Field.js.map +1 -1
- package/dist/production/coreComponents/HasMany.js +3 -5
- package/dist/production/coreComponents/HasMany.js.map +1 -1
- package/dist/production/coreComponents/HasOne.js +3 -5
- package/dist/production/coreComponents/HasOne.js.map +1 -1
- package/dist/production/dao/Environment.js +147 -94
- package/dist/production/dao/Environment.js.map +1 -1
- package/dist/production/dao/TreeNodeEnvironmentFactory.js +159 -0
- package/dist/production/dao/TreeNodeEnvironmentFactory.js.map +1 -0
- package/dist/production/queryLanguage/MarkerFactory.js +4 -3
- package/dist/production/queryLanguage/MarkerFactory.js.map +1 -1
- package/dist/production/queryLanguage/Parser.js +15 -6
- package/dist/production/queryLanguage/Parser.js.map +1 -1
- package/dist/types/accessorTree/MutationRequestResponse.d.ts +4 -0
- package/dist/types/accessorTree/MutationRequestResponse.d.ts.map +1 -1
- package/dist/types/accessorTree/QueryRequestResponse.d.ts +4 -0
- package/dist/types/accessorTree/QueryRequestResponse.d.ts.map +1 -1
- package/dist/types/accessorTree/RequestError.d.ts +9 -1
- package/dist/types/accessorTree/RequestError.d.ts.map +1 -1
- package/dist/types/core/DataBinding.d.ts.map +1 -1
- package/dist/types/core/MarkerMerger.d.ts.map +1 -1
- package/dist/types/core/StateInitializer.d.ts.map +1 -1
- package/dist/types/core/VariableInputTransformer.d.ts.map +1 -1
- package/dist/types/core/schema/Schema.d.ts +2 -0
- package/dist/types/core/schema/Schema.d.ts.map +1 -1
- package/dist/types/core/schema/index.d.ts +0 -1
- package/dist/types/core/schema/index.d.ts.map +1 -1
- package/dist/types/coreComponents/EntityListSubTree.d.ts +1 -1
- package/dist/types/coreComponents/EntityListSubTree.d.ts.map +1 -1
- package/dist/types/coreComponents/EntitySubTree.d.ts +1 -1
- package/dist/types/coreComponents/EntitySubTree.d.ts.map +1 -1
- package/dist/types/coreComponents/Field.d.ts.map +1 -1
- package/dist/types/coreComponents/HasMany.d.ts +1 -1
- package/dist/types/coreComponents/HasMany.d.ts.map +1 -1
- package/dist/types/coreComponents/HasOne.d.ts +1 -1
- package/dist/types/coreComponents/HasOne.d.ts.map +1 -1
- package/dist/types/dao/Environment.d.ts +82 -43
- package/dist/types/dao/Environment.d.ts.map +1 -1
- package/dist/types/dao/TreeNodeEnvironmentFactory.d.ts +19 -0
- package/dist/types/dao/TreeNodeEnvironmentFactory.d.ts.map +1 -0
- package/dist/types/queryLanguage/MarkerFactory.d.ts.map +1 -1
- package/dist/types/queryLanguage/Parser.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +9 -6
- package/src/accessorTree/MutationRequestResponse.ts +1 -0
- package/src/accessorTree/QueryRequestResponse.ts +1 -0
- package/src/accessorTree/RequestError.ts +7 -1
- package/src/core/DataBinding.ts +23 -26
- package/src/core/MarkerMerger.ts +1 -4
- package/src/core/StateInitializer.ts +1 -3
- package/src/core/VariableInputTransformer.ts +7 -4
- package/src/core/schema/Schema.ts +5 -0
- package/src/core/schema/index.ts +0 -1
- package/src/coreComponents/EntityListSubTree.tsx +5 -16
- package/src/coreComponents/EntitySubTree.tsx +17 -32
- package/src/coreComponents/Field.tsx +4 -0
- package/src/coreComponents/HasMany.tsx +4 -6
- package/src/coreComponents/HasOne.tsx +6 -7
- package/src/dao/Environment.ts +221 -126
- package/src/dao/TreeNodeEnvironmentFactory.ts +228 -0
- package/src/queryLanguage/MarkerFactory.ts +5 -3
- package/src/queryLanguage/Parser.ts +24 -11
- package/dist/development/core/schema/SchemaValidator.js +0 -116
- package/dist/development/core/schema/SchemaValidator.js.map +0 -1
- package/dist/types/core/schema/SchemaValidator.d.ts +0 -14
- package/dist/types/core/schema/SchemaValidator.d.ts.map +0 -1
- package/src/core/schema/SchemaValidator.ts +0 -238
|
@@ -28,6 +28,7 @@ import type {
|
|
|
28
28
|
import { assertNever } from '../utils'
|
|
29
29
|
import { QueryLanguage } from './QueryLanguage'
|
|
30
30
|
import { GraphQlLiteral } from '@contember/client'
|
|
31
|
+
import { BindingError } from '../BindingError'
|
|
31
32
|
|
|
32
33
|
export class MarkerFactory {
|
|
33
34
|
private static createSubTreeMarker<
|
|
@@ -191,7 +192,7 @@ export class MarkerFactory {
|
|
|
191
192
|
)
|
|
192
193
|
return this.wrapRelativeEntityFieldMarkers(
|
|
193
194
|
relativeEntityList.hasOneRelationPath,
|
|
194
|
-
environment,
|
|
195
|
+
environment.getParent(),
|
|
195
196
|
this.createEntityFieldMarkersContainer(hasManyRelationMarker),
|
|
196
197
|
)
|
|
197
198
|
}
|
|
@@ -210,7 +211,7 @@ export class MarkerFactory {
|
|
|
210
211
|
return new EntityFieldsWithHoistablesMarker(
|
|
211
212
|
this.wrapRelativeEntityFieldMarkers(
|
|
212
213
|
relativeEntityList.hasOneRelationPath,
|
|
213
|
-
environment,
|
|
214
|
+
environment.getParent(),
|
|
214
215
|
this.createEntityFieldMarkersContainer(hasManyRelationMarker),
|
|
215
216
|
),
|
|
216
217
|
fields.subTrees,
|
|
@@ -236,7 +237,7 @@ export class MarkerFactory {
|
|
|
236
237
|
|
|
237
238
|
return this.wrapRelativeEntityFieldMarkers(
|
|
238
239
|
relativeSingleField.hasOneRelationPath,
|
|
239
|
-
environment,
|
|
240
|
+
environment.getParent(),
|
|
240
241
|
this.createEntityFieldMarkersContainer(getMarker(relativeSingleField)),
|
|
241
242
|
)
|
|
242
243
|
}
|
|
@@ -248,6 +249,7 @@ export class MarkerFactory {
|
|
|
248
249
|
): EntityFieldMarkersContainer {
|
|
249
250
|
for (let i = hasOneRelationPath.length - 1; i >= 0; i--) {
|
|
250
251
|
const marker = this.createHasOneRelationMarker(hasOneRelationPath[i], environment, fields)
|
|
252
|
+
environment = environment.getParent()
|
|
251
253
|
fields = this.createEntityFieldMarkersContainer(marker)
|
|
252
254
|
}
|
|
253
255
|
return fields
|
|
@@ -21,6 +21,7 @@ import type {
|
|
|
21
21
|
import { CacheStore } from './CacheStore'
|
|
22
22
|
import { QueryLanguageError } from './QueryLanguageError'
|
|
23
23
|
import { tokenList, TokenRegExps, tokens } from './tokenList'
|
|
24
|
+
import { BindingError } from '../BindingError'
|
|
24
25
|
|
|
25
26
|
/**
|
|
26
27
|
* TODO:
|
|
@@ -33,7 +34,7 @@ class Parser extends EmbeddedActionsParser {
|
|
|
33
34
|
private static rawInput: string = ''
|
|
34
35
|
private static lexer = new Lexer(tokenList)
|
|
35
36
|
private static parser = new Parser()
|
|
36
|
-
private static environment: Environment =
|
|
37
|
+
private static environment: Environment = Environment.create()
|
|
37
38
|
private static cacheStore: CacheStore = new CacheStore()
|
|
38
39
|
|
|
39
40
|
private qualifiedEntityList = this.RULE<ParsedQualifiedEntityList>('qualifiedEntityList', () => {
|
|
@@ -722,25 +723,37 @@ class Parser extends EmbeddedActionsParser {
|
|
|
722
723
|
return new GraphQlBuilder.GraphQlLiteral(image)
|
|
723
724
|
})
|
|
724
725
|
|
|
725
|
-
private variable = this.RULE<
|
|
726
|
+
private variable = this.RULE<Environment.ResolvedValue>('variable', () => {
|
|
726
727
|
this.CONSUME(tokens.DollarSign)
|
|
727
728
|
const variableName = this.CONSUME(tokens.Identifier).image
|
|
728
729
|
|
|
729
730
|
return this.ACTION(() => {
|
|
730
|
-
if (
|
|
731
|
-
|
|
731
|
+
if (import.meta.env.DEV && variableName === 'rootWhereAsFilter') {
|
|
732
|
+
console.warn('$rootWhereAsFilter is deprecated, use $rootFilter')
|
|
733
|
+
}
|
|
734
|
+
switch (variableName) {
|
|
735
|
+
case 'rootWhereAsFilter':
|
|
736
|
+
case 'rootFilter':
|
|
737
|
+
return Parser.environment.getSubTree().filter
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
if (Parser.environment.hasVariable(variableName)) {
|
|
741
|
+
return Parser.environment.getVariable(variableName)
|
|
742
|
+
}
|
|
743
|
+
if (Parser.environment.hasParameter(variableName)) {
|
|
744
|
+
return Parser.environment.getParameter(variableName)
|
|
732
745
|
}
|
|
733
746
|
if (Parser.environment.hasDimension(variableName)) {
|
|
734
747
|
const dimensionValue = Parser.environment.getDimension(variableName)
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
}
|
|
739
|
-
throw new QueryLanguageError(
|
|
740
|
-
`The variable \$${variableName} resolved to a dimension which exists but contains ${dimensionValue.length} values. It has to contain exactly one. ` +
|
|
748
|
+
if (dimensionValue.length > 1) {
|
|
749
|
+
throw new BindingError(
|
|
750
|
+
`The variable \$${variableName} resolved to a dimension which exists but contains ${dimensionValue.length} values. It has to contain exactly one. ` +
|
|
741
751
|
`Perhaps you forgot to set the 'maxItems' prop of your DimensionsSwitcher?`,
|
|
742
|
-
|
|
752
|
+
)
|
|
753
|
+
}
|
|
754
|
+
return dimensionValue[0]
|
|
743
755
|
}
|
|
756
|
+
|
|
744
757
|
throw new QueryLanguageError(`Undefined variable \$${variableName}.`)
|
|
745
758
|
})
|
|
746
759
|
})
|
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import levenshtein from "js-levenshtein";
|
|
2
|
-
import { BindingError } from "../../BindingError.js";
|
|
3
|
-
import { ErrorLocator } from "../exceptions/ErrorLocator.js";
|
|
4
|
-
import { LocalizedBindingError } from "../exceptions/LocalizedBindingError.js";
|
|
5
|
-
import { FieldMarker } from "../../markers/FieldMarker.js";
|
|
6
|
-
import { HasOneRelationMarker } from "../../markers/HasOneRelationMarker.js";
|
|
7
|
-
import { HasManyRelationMarker } from "../../markers/HasManyRelationMarker.js";
|
|
8
|
-
import { assertNever } from "../../utils/assertNever.js";
|
|
9
|
-
import { EntitySubTreeMarker } from "../../markers/EntitySubTreeMarker.js";
|
|
10
|
-
class SchemaValidator {
|
|
11
|
-
static assertTreeValid(schema, tree) {
|
|
12
|
-
const errors = Array.from(tree.subTrees.values(), (subTree) => Array.from(this.getSubTreeErrors(schema, subTree))).flat();
|
|
13
|
-
if (errors.length) {
|
|
14
|
-
throw new BindingError(`Invalid data tree:
|
|
15
|
-
` + errors.map((error) => ` \u2022 ${error.message} (Error at ${ErrorLocator.locateMarkerPath(error.markerPath)})`).join(`
|
|
16
|
-
`));
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
static *getSubTreeErrors(schema, subTree) {
|
|
20
|
-
const entityName = subTree.entityName;
|
|
21
|
-
const entity = schema.store.entities.get(entityName);
|
|
22
|
-
if (entity === void 0) {
|
|
23
|
-
const type = subTree instanceof EntitySubTreeMarker ? "entity list" : "entity";
|
|
24
|
-
const alternative = this.recommendAlternative(entityName, schema.store.entities.keys());
|
|
25
|
-
yield new LocalizedBindingError(`Invalid ${type} sub tree: Entity '${entityName}' doesn't exist. ${alternative ? `Did you mean '${alternative}'?` : ""}`, [subTree]);
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
yield* this.nestErrorsIn(subTree, this.getMarkersContainerErrors(schema, entity, subTree.fields));
|
|
29
|
-
}
|
|
30
|
-
static *getMarkersContainerErrors(schema, containingEntity, markers) {
|
|
31
|
-
for (const marker of markers.markers.values()) {
|
|
32
|
-
if (marker instanceof FieldMarker) {
|
|
33
|
-
yield* this.getFieldErrors(schema, containingEntity, marker);
|
|
34
|
-
} else if (marker instanceof HasOneRelationMarker || marker instanceof HasManyRelationMarker) {
|
|
35
|
-
yield* this.getRelationErrors(schema, containingEntity, marker);
|
|
36
|
-
} else {
|
|
37
|
-
return assertNever();
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
static *getRelationErrors(schema, containingEntity, marker) {
|
|
42
|
-
const relationName = marker.parameters.field;
|
|
43
|
-
const relation = containingEntity.fields.get(relationName);
|
|
44
|
-
const type = marker instanceof HasOneRelationMarker ? "one" : "many";
|
|
45
|
-
if (relation === void 0) {
|
|
46
|
-
const alternative = this.recommendAlternative(relationName, Array.from(containingEntity.fields).filter(([, field]) => {
|
|
47
|
-
if (field.__typename !== "_Relation") {
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
if (marker instanceof HasOneRelationMarker) {
|
|
51
|
-
return field.type === "OneHasOne" || field.type === "ManyHasOne";
|
|
52
|
-
}
|
|
53
|
-
if (marker instanceof HasManyRelationMarker) {
|
|
54
|
-
return field.type === "OneHasMany" || field.type === "ManyHasMany";
|
|
55
|
-
}
|
|
56
|
-
return assertNever();
|
|
57
|
-
}).map(([fieldName]) => fieldName));
|
|
58
|
-
yield new LocalizedBindingError(`Invalid has-${type} relation '${relationName}' doesn't exist on entity '${containingEntity.name}'. ` + (alternative ? `Did you mean '${alternative}'?` : ""), [marker]);
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
if (relation.__typename === "_Column") {
|
|
62
|
-
yield new LocalizedBindingError(`Invalid has-${type} relation: the name '${relationName}' on entity '${containingEntity.name}' refers to an ordinary scalar field but is being used as a has-${type} relation.`, [marker]);
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
if (marker instanceof HasOneRelationMarker && marker.parameters.reducedBy === void 0 && (relation.type === "OneHasMany" || relation.type === "ManyHasMany")) {
|
|
66
|
-
yield new LocalizedBindingError(`Invalid has-one relation: the name '${relationName}' on entity '${containingEntity.name}' refers to a *has-many* relation but is being used as a has-one.`, [marker]);
|
|
67
|
-
return;
|
|
68
|
-
}
|
|
69
|
-
if ((marker instanceof HasManyRelationMarker || marker instanceof HasOneRelationMarker && marker.parameters.reducedBy !== void 0) && (relation.type === "OneHasOne" || relation.type === "ManyHasOne")) {
|
|
70
|
-
yield new LocalizedBindingError(`Invalid has-many relation: the name '${relationName}' on entity '${containingEntity.name}' refers to a *has-one* relation but is being used as a has-many.`, [marker]);
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
const targetEntityName = relation.targetEntity;
|
|
74
|
-
const targetEntity = schema.store.entities.get(targetEntityName);
|
|
75
|
-
if (targetEntity === void 0) {
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
yield* this.nestErrorsIn(marker, this.getMarkersContainerErrors(schema, targetEntity, marker.fields));
|
|
79
|
-
}
|
|
80
|
-
static *getFieldErrors(schema, containingEntity, marker) {
|
|
81
|
-
const fieldName = marker.fieldName;
|
|
82
|
-
const field = containingEntity.fields.get(fieldName);
|
|
83
|
-
if (field === void 0) {
|
|
84
|
-
const alternative = this.recommendAlternative(fieldName, Array.from(containingEntity.fields).filter(([, field2]) => field2.__typename === "_Column").map(([fieldName2]) => fieldName2));
|
|
85
|
-
yield new LocalizedBindingError(`Field '${fieldName}' doesn't exist on entity '${containingEntity.name}'. ` + (alternative ? `Did you mean '${alternative}'?` : ""), [marker]);
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
if (field.__typename === "_Relation") {
|
|
89
|
-
yield new LocalizedBindingError(`Invalid field: the name '${fieldName}' on entity '${containingEntity.name}' refers to a has-${field.type === "OneHasOne" || field.type === "ManyHasOne" ? "one" : "many"} relation but is being used as a field.`, [marker]);
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
static *getUniqueWhereErrors(schema, containingEntity, where) {
|
|
94
|
-
}
|
|
95
|
-
static *getFilterErrors(schema, containingEntity, filter) {
|
|
96
|
-
}
|
|
97
|
-
static *nestErrorsIn(source, to) {
|
|
98
|
-
for (const error of to) {
|
|
99
|
-
yield error.nestedIn(source);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
static recommendAlternative(original, possibleAlternatives) {
|
|
103
|
-
let bestAlternative = void 0;
|
|
104
|
-
let bestAlternativeDistance = Number.MAX_SAFE_INTEGER;
|
|
105
|
-
for (const alternative of possibleAlternatives) {
|
|
106
|
-
const distance = levenshtein(original, alternative);
|
|
107
|
-
if (distance < bestAlternativeDistance) {
|
|
108
|
-
bestAlternative = alternative;
|
|
109
|
-
bestAlternativeDistance = distance;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
return bestAlternative;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
export { SchemaValidator };
|
|
116
|
-
//# sourceMappingURL=SchemaValidator.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SchemaValidator.js","sources":["../../../../src/core/schema/SchemaValidator.ts"],"sourcesContent":["import levenshtein from 'js-levenshtein'\nimport { BindingError } from '../../BindingError'\nimport {\n\tEntityFieldMarkersContainer,\n\tEntityListSubTreeMarker,\n\tEntitySubTreeMarker,\n\tFieldMarker,\n\tHasManyRelationMarker,\n\tHasOneRelationMarker,\n\tMarkerTreeRoot,\n} from '../../markers'\nimport type { Filter, UniqueWhere } from '../../treeParameters'\nimport { assertNever } from '../../utils'\nimport { ErrorLocator, LocalizedBindingError } from '../exceptions'\nimport type { Schema } from './Schema'\nimport type { SchemaEntity } from './SchemaEntity'\n\nexport class SchemaValidator {\n\tpublic static assertTreeValid(schema: Schema, tree: MarkerTreeRoot) {\n\t\tconst errors = Array.from(tree.subTrees.values(), subTree =>\n\t\t\tArray.from(this.getSubTreeErrors(schema, subTree)),\n\t\t).flat()\n\n\t\tif (errors.length) {\n\t\t\tthrow new BindingError(\n\t\t\t\t`Invalid data tree:\\n` +\n\t\t\t\t\terrors\n\t\t\t\t\t\t.map(error => `\\t• ${error.message} (Error at ${ErrorLocator.locateMarkerPath(error.markerPath)})`)\n\t\t\t\t\t\t.join(`\\n`),\n\t\t\t)\n\t\t}\n\t}\n\n\tprivate static *getSubTreeErrors(\n\t\tschema: Schema,\n\t\tsubTree: EntitySubTreeMarker | EntityListSubTreeMarker,\n\t): Generator<LocalizedBindingError, void> {\n\t\tconst entityName = subTree.entityName\n\t\tconst entity = schema.store.entities.get(entityName)\n\n\t\tif (entity === undefined) {\n\t\t\tconst type = subTree instanceof EntitySubTreeMarker ? 'entity list' : 'entity'\n\t\t\tconst alternative = this.recommendAlternative(entityName, schema.store.entities.keys())\n\n\t\t\tyield new LocalizedBindingError(\n\t\t\t\t`Invalid ${type} sub tree: Entity '${entityName}' doesn't exist. ${\n\t\t\t\t\talternative ? `Did you mean '${alternative}'?` : ''\n\t\t\t\t}`,\n\t\t\t\t[subTree],\n\t\t\t)\n\n\t\t\treturn\n\t\t}\n\n\t\t// TODO check filter\n\t\t// TODO check where\n\t\t// TODO run custom validators\n\n\t\tyield* this.nestErrorsIn(subTree, this.getMarkersContainerErrors(schema, entity, subTree.fields))\n\t}\n\n\tprivate static *getMarkersContainerErrors(\n\t\tschema: Schema,\n\t\tcontainingEntity: SchemaEntity,\n\t\tmarkers: EntityFieldMarkersContainer,\n\t): Generator<LocalizedBindingError, void> {\n\t\tfor (const marker of markers.markers.values()) {\n\t\t\tif (marker instanceof FieldMarker) {\n\t\t\t\tyield* this.getFieldErrors(schema, containingEntity, marker)\n\t\t\t} else if (marker instanceof HasOneRelationMarker || marker instanceof HasManyRelationMarker) {\n\t\t\t\tyield* this.getRelationErrors(schema, containingEntity, marker)\n\t\t\t} else {\n\t\t\t\treturn assertNever(marker)\n\t\t\t}\n\t\t}\n\t}\n\n\tprivate static *getRelationErrors(\n\t\tschema: Schema,\n\t\tcontainingEntity: SchemaEntity,\n\t\tmarker: HasOneRelationMarker | HasManyRelationMarker,\n\t): Generator<LocalizedBindingError, void> {\n\t\tconst relationName = marker.parameters.field\n\t\tconst relation = containingEntity.fields.get(relationName)\n\t\tconst type = marker instanceof HasOneRelationMarker ? 'one' : 'many'\n\n\t\tif (relation === undefined) {\n\t\t\tconst alternative = this.recommendAlternative(\n\t\t\t\trelationName,\n\t\t\t\tArray.from(containingEntity.fields)\n\t\t\t\t\t.filter(([, field]) => {\n\t\t\t\t\t\tif (field.__typename !== '_Relation') {\n\t\t\t\t\t\t\treturn false\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (marker instanceof HasOneRelationMarker) {\n\t\t\t\t\t\t\treturn field.type === 'OneHasOne' || field.type === 'ManyHasOne'\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (marker instanceof HasManyRelationMarker) {\n\t\t\t\t\t\t\treturn field.type === 'OneHasMany' || field.type === 'ManyHasMany'\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn assertNever(marker)\n\t\t\t\t\t})\n\t\t\t\t\t.map(([fieldName]) => fieldName),\n\t\t\t)\n\n\t\t\tyield new LocalizedBindingError(\n\t\t\t\t`Invalid has-${type} relation '${relationName}' doesn't exist on entity '${containingEntity.name}'. ` +\n\t\t\t\t\t(alternative ? `Did you mean '${alternative}'?` : ''),\n\t\t\t\t[marker],\n\t\t\t)\n\t\t\treturn\n\t\t}\n\t\tif (relation.__typename === '_Column') {\n\t\t\tyield new LocalizedBindingError(\n\t\t\t\t`Invalid has-${type} relation: the name '${relationName}' on entity '${containingEntity.name}' ` +\n\t\t\t\t\t`refers to an ordinary scalar field but is being used as a has-${type} relation.`,\n\t\t\t\t[marker],\n\t\t\t)\n\t\t\treturn\n\t\t}\n\t\tif (\n\t\t\tmarker instanceof HasOneRelationMarker &&\n\t\t\tmarker.parameters.reducedBy === undefined &&\n\t\t\t(relation.type === 'OneHasMany' || relation.type === 'ManyHasMany')\n\t\t) {\n\t\t\tyield new LocalizedBindingError(\n\t\t\t\t`Invalid has-one relation: the name '${relationName}' on entity '${containingEntity.name}' ` +\n\t\t\t\t\t`refers to a *has-many* relation but is being used as a has-one.`,\n\t\t\t\t[marker],\n\t\t\t)\n\t\t\treturn\n\t\t}\n\t\tif (\n\t\t\t(marker instanceof HasManyRelationMarker ||\n\t\t\t\t(marker instanceof HasOneRelationMarker && marker.parameters.reducedBy !== undefined)) &&\n\t\t\t(relation.type === 'OneHasOne' || relation.type === 'ManyHasOne')\n\t\t) {\n\t\t\tyield new LocalizedBindingError(\n\t\t\t\t`Invalid has-many relation: the name '${relationName}' on entity '${containingEntity.name}' ` +\n\t\t\t\t\t`refers to a *has-one* relation but is being used as a has-many.`,\n\t\t\t\t[marker],\n\t\t\t)\n\t\t\treturn\n\t\t}\n\n\t\t// TODO check filter\n\t\t// TODO check reducedBy\n\t\t// TODO check expected mutations\n\t\t// TODO run custom validators\n\n\t\tconst targetEntityName = relation.targetEntity\n\t\tconst targetEntity = schema.store.entities.get(targetEntityName)\n\n\t\tif (targetEntity === undefined) {\n\t\t\treturn // ?!? This really shouldn't ever happen.\n\t\t}\n\n\t\tyield* this.nestErrorsIn(marker, this.getMarkersContainerErrors(schema, targetEntity, marker.fields))\n\t}\n\n\tprivate static *getFieldErrors(\n\t\tschema: Schema,\n\t\tcontainingEntity: SchemaEntity,\n\t\tmarker: FieldMarker,\n\t): Generator<LocalizedBindingError, void> {\n\t\tconst fieldName = marker.fieldName\n\t\tconst field = containingEntity.fields.get(fieldName)\n\n\t\tif (field === undefined) {\n\t\t\tconst alternative = this.recommendAlternative(\n\t\t\t\tfieldName,\n\t\t\t\tArray.from(containingEntity.fields)\n\t\t\t\t\t.filter(([, field]) => field.__typename === '_Column')\n\t\t\t\t\t.map(([fieldName]) => fieldName),\n\t\t\t)\n\n\t\t\tyield new LocalizedBindingError(\n\t\t\t\t`Field '${fieldName}' doesn't exist on entity '${containingEntity.name}'. ` +\n\t\t\t\t\t(alternative ? `Did you mean '${alternative}'?` : ''),\n\t\t\t\t[marker],\n\t\t\t)\n\t\t\treturn\n\t\t}\n\t\tif (field.__typename === '_Relation') {\n\t\t\tyield new LocalizedBindingError(\n\t\t\t\t`Invalid field: the name '${fieldName}' on entity '${containingEntity.name}' ` +\n\t\t\t\t\t`refers to a has-${\n\t\t\t\t\t\tfield.type === 'OneHasOne' || field.type === 'ManyHasOne' ? 'one' : 'many'\n\t\t\t\t\t} relation but is being used as a field.`,\n\t\t\t\t[marker],\n\t\t\t)\n\t\t\treturn\n\t\t}\n\n\t\t// TODO check that defaultValue matches the type\n\t\t// TODO run custom validators\n\t}\n\n\tprivate static *getUniqueWhereErrors(\n\t\tschema: Schema,\n\t\tcontainingEntity: SchemaEntity,\n\t\twhere: UniqueWhere,\n\t): Generator<LocalizedBindingError, void> {\n\t\t//\n\t}\n\n\tprivate static *getFilterErrors(\n\t\tschema: Schema,\n\t\tcontainingEntity: SchemaEntity,\n\t\tfilter: Filter,\n\t): Generator<LocalizedBindingError, void> {\n\t\t//\n\t}\n\n\tprivate static *nestErrorsIn(\n\t\tsource: HasOneRelationMarker | HasManyRelationMarker | EntityListSubTreeMarker | EntitySubTreeMarker,\n\t\tto: Generator<LocalizedBindingError, void>,\n\t): Generator<LocalizedBindingError, void> {\n\t\tfor (const error of to) {\n\t\t\tyield error.nestedIn(source)\n\t\t}\n\t}\n\n\tprivate static recommendAlternative(original: string, possibleAlternatives: Iterable<string>): string | undefined {\n\t\tlet bestAlternative: string | undefined = undefined\n\t\tlet bestAlternativeDistance = Number.MAX_SAFE_INTEGER\n\n\t\tfor (const alternative of possibleAlternatives) {\n\t\t\tconst distance = levenshtein(original, alternative)\n\n\t\t\tif (distance < bestAlternativeDistance) {\n\t\t\t\tbestAlternative = alternative\n\t\t\t\tbestAlternativeDistance = distance\n\t\t\t}\n\t\t}\n\t\treturn bestAlternative\n\t}\n}\n"],"names":[],"mappings":"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"}
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { MarkerTreeRoot } from '../../markers';
|
|
2
|
-
import type { Schema } from './Schema';
|
|
3
|
-
export declare class SchemaValidator {
|
|
4
|
-
static assertTreeValid(schema: Schema, tree: MarkerTreeRoot): void;
|
|
5
|
-
private static getSubTreeErrors;
|
|
6
|
-
private static getMarkersContainerErrors;
|
|
7
|
-
private static getRelationErrors;
|
|
8
|
-
private static getFieldErrors;
|
|
9
|
-
private static getUniqueWhereErrors;
|
|
10
|
-
private static getFilterErrors;
|
|
11
|
-
private static nestErrorsIn;
|
|
12
|
-
private static recommendAlternative;
|
|
13
|
-
}
|
|
14
|
-
//# sourceMappingURL=SchemaValidator.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"SchemaValidator.d.ts","sourceRoot":"","sources":["../../../../src/core/schema/SchemaValidator.ts"],"names":[],"mappings":"AAEA,OAAO,EAON,cAAc,EACd,MAAM,eAAe,CAAA;AAItB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AAGtC,qBAAa,eAAe;WACb,eAAe,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,cAAc;IAelE,OAAO,CAAC,MAAM,CAAE,gBAAgB;IA4BhC,OAAO,CAAC,MAAM,CAAE,yBAAyB;IAgBzC,OAAO,CAAC,MAAM,CAAE,iBAAiB;IAmFjC,OAAO,CAAC,MAAM,CAAE,cAAc;IAsC9B,OAAO,CAAC,MAAM,CAAE,oBAAoB;IAQpC,OAAO,CAAC,MAAM,CAAE,eAAe;IAQ/B,OAAO,CAAC,MAAM,CAAE,YAAY;IAS5B,OAAO,CAAC,MAAM,CAAC,oBAAoB;CAcnC"}
|
|
@@ -1,238 +0,0 @@
|
|
|
1
|
-
import levenshtein from 'js-levenshtein'
|
|
2
|
-
import { BindingError } from '../../BindingError'
|
|
3
|
-
import {
|
|
4
|
-
EntityFieldMarkersContainer,
|
|
5
|
-
EntityListSubTreeMarker,
|
|
6
|
-
EntitySubTreeMarker,
|
|
7
|
-
FieldMarker,
|
|
8
|
-
HasManyRelationMarker,
|
|
9
|
-
HasOneRelationMarker,
|
|
10
|
-
MarkerTreeRoot,
|
|
11
|
-
} from '../../markers'
|
|
12
|
-
import type { Filter, UniqueWhere } from '../../treeParameters'
|
|
13
|
-
import { assertNever } from '../../utils'
|
|
14
|
-
import { ErrorLocator, LocalizedBindingError } from '../exceptions'
|
|
15
|
-
import type { Schema } from './Schema'
|
|
16
|
-
import type { SchemaEntity } from './SchemaEntity'
|
|
17
|
-
|
|
18
|
-
export class SchemaValidator {
|
|
19
|
-
public static assertTreeValid(schema: Schema, tree: MarkerTreeRoot) {
|
|
20
|
-
const errors = Array.from(tree.subTrees.values(), subTree =>
|
|
21
|
-
Array.from(this.getSubTreeErrors(schema, subTree)),
|
|
22
|
-
).flat()
|
|
23
|
-
|
|
24
|
-
if (errors.length) {
|
|
25
|
-
throw new BindingError(
|
|
26
|
-
`Invalid data tree:\n` +
|
|
27
|
-
errors
|
|
28
|
-
.map(error => `\t• ${error.message} (Error at ${ErrorLocator.locateMarkerPath(error.markerPath)})`)
|
|
29
|
-
.join(`\n`),
|
|
30
|
-
)
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
private static *getSubTreeErrors(
|
|
35
|
-
schema: Schema,
|
|
36
|
-
subTree: EntitySubTreeMarker | EntityListSubTreeMarker,
|
|
37
|
-
): Generator<LocalizedBindingError, void> {
|
|
38
|
-
const entityName = subTree.entityName
|
|
39
|
-
const entity = schema.store.entities.get(entityName)
|
|
40
|
-
|
|
41
|
-
if (entity === undefined) {
|
|
42
|
-
const type = subTree instanceof EntitySubTreeMarker ? 'entity list' : 'entity'
|
|
43
|
-
const alternative = this.recommendAlternative(entityName, schema.store.entities.keys())
|
|
44
|
-
|
|
45
|
-
yield new LocalizedBindingError(
|
|
46
|
-
`Invalid ${type} sub tree: Entity '${entityName}' doesn't exist. ${
|
|
47
|
-
alternative ? `Did you mean '${alternative}'?` : ''
|
|
48
|
-
}`,
|
|
49
|
-
[subTree],
|
|
50
|
-
)
|
|
51
|
-
|
|
52
|
-
return
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// TODO check filter
|
|
56
|
-
// TODO check where
|
|
57
|
-
// TODO run custom validators
|
|
58
|
-
|
|
59
|
-
yield* this.nestErrorsIn(subTree, this.getMarkersContainerErrors(schema, entity, subTree.fields))
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
private static *getMarkersContainerErrors(
|
|
63
|
-
schema: Schema,
|
|
64
|
-
containingEntity: SchemaEntity,
|
|
65
|
-
markers: EntityFieldMarkersContainer,
|
|
66
|
-
): Generator<LocalizedBindingError, void> {
|
|
67
|
-
for (const marker of markers.markers.values()) {
|
|
68
|
-
if (marker instanceof FieldMarker) {
|
|
69
|
-
yield* this.getFieldErrors(schema, containingEntity, marker)
|
|
70
|
-
} else if (marker instanceof HasOneRelationMarker || marker instanceof HasManyRelationMarker) {
|
|
71
|
-
yield* this.getRelationErrors(schema, containingEntity, marker)
|
|
72
|
-
} else {
|
|
73
|
-
return assertNever(marker)
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
private static *getRelationErrors(
|
|
79
|
-
schema: Schema,
|
|
80
|
-
containingEntity: SchemaEntity,
|
|
81
|
-
marker: HasOneRelationMarker | HasManyRelationMarker,
|
|
82
|
-
): Generator<LocalizedBindingError, void> {
|
|
83
|
-
const relationName = marker.parameters.field
|
|
84
|
-
const relation = containingEntity.fields.get(relationName)
|
|
85
|
-
const type = marker instanceof HasOneRelationMarker ? 'one' : 'many'
|
|
86
|
-
|
|
87
|
-
if (relation === undefined) {
|
|
88
|
-
const alternative = this.recommendAlternative(
|
|
89
|
-
relationName,
|
|
90
|
-
Array.from(containingEntity.fields)
|
|
91
|
-
.filter(([, field]) => {
|
|
92
|
-
if (field.__typename !== '_Relation') {
|
|
93
|
-
return false
|
|
94
|
-
}
|
|
95
|
-
if (marker instanceof HasOneRelationMarker) {
|
|
96
|
-
return field.type === 'OneHasOne' || field.type === 'ManyHasOne'
|
|
97
|
-
}
|
|
98
|
-
if (marker instanceof HasManyRelationMarker) {
|
|
99
|
-
return field.type === 'OneHasMany' || field.type === 'ManyHasMany'
|
|
100
|
-
}
|
|
101
|
-
return assertNever(marker)
|
|
102
|
-
})
|
|
103
|
-
.map(([fieldName]) => fieldName),
|
|
104
|
-
)
|
|
105
|
-
|
|
106
|
-
yield new LocalizedBindingError(
|
|
107
|
-
`Invalid has-${type} relation '${relationName}' doesn't exist on entity '${containingEntity.name}'. ` +
|
|
108
|
-
(alternative ? `Did you mean '${alternative}'?` : ''),
|
|
109
|
-
[marker],
|
|
110
|
-
)
|
|
111
|
-
return
|
|
112
|
-
}
|
|
113
|
-
if (relation.__typename === '_Column') {
|
|
114
|
-
yield new LocalizedBindingError(
|
|
115
|
-
`Invalid has-${type} relation: the name '${relationName}' on entity '${containingEntity.name}' ` +
|
|
116
|
-
`refers to an ordinary scalar field but is being used as a has-${type} relation.`,
|
|
117
|
-
[marker],
|
|
118
|
-
)
|
|
119
|
-
return
|
|
120
|
-
}
|
|
121
|
-
if (
|
|
122
|
-
marker instanceof HasOneRelationMarker &&
|
|
123
|
-
marker.parameters.reducedBy === undefined &&
|
|
124
|
-
(relation.type === 'OneHasMany' || relation.type === 'ManyHasMany')
|
|
125
|
-
) {
|
|
126
|
-
yield new LocalizedBindingError(
|
|
127
|
-
`Invalid has-one relation: the name '${relationName}' on entity '${containingEntity.name}' ` +
|
|
128
|
-
`refers to a *has-many* relation but is being used as a has-one.`,
|
|
129
|
-
[marker],
|
|
130
|
-
)
|
|
131
|
-
return
|
|
132
|
-
}
|
|
133
|
-
if (
|
|
134
|
-
(marker instanceof HasManyRelationMarker ||
|
|
135
|
-
(marker instanceof HasOneRelationMarker && marker.parameters.reducedBy !== undefined)) &&
|
|
136
|
-
(relation.type === 'OneHasOne' || relation.type === 'ManyHasOne')
|
|
137
|
-
) {
|
|
138
|
-
yield new LocalizedBindingError(
|
|
139
|
-
`Invalid has-many relation: the name '${relationName}' on entity '${containingEntity.name}' ` +
|
|
140
|
-
`refers to a *has-one* relation but is being used as a has-many.`,
|
|
141
|
-
[marker],
|
|
142
|
-
)
|
|
143
|
-
return
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// TODO check filter
|
|
147
|
-
// TODO check reducedBy
|
|
148
|
-
// TODO check expected mutations
|
|
149
|
-
// TODO run custom validators
|
|
150
|
-
|
|
151
|
-
const targetEntityName = relation.targetEntity
|
|
152
|
-
const targetEntity = schema.store.entities.get(targetEntityName)
|
|
153
|
-
|
|
154
|
-
if (targetEntity === undefined) {
|
|
155
|
-
return // ?!? This really shouldn't ever happen.
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
yield* this.nestErrorsIn(marker, this.getMarkersContainerErrors(schema, targetEntity, marker.fields))
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
private static *getFieldErrors(
|
|
162
|
-
schema: Schema,
|
|
163
|
-
containingEntity: SchemaEntity,
|
|
164
|
-
marker: FieldMarker,
|
|
165
|
-
): Generator<LocalizedBindingError, void> {
|
|
166
|
-
const fieldName = marker.fieldName
|
|
167
|
-
const field = containingEntity.fields.get(fieldName)
|
|
168
|
-
|
|
169
|
-
if (field === undefined) {
|
|
170
|
-
const alternative = this.recommendAlternative(
|
|
171
|
-
fieldName,
|
|
172
|
-
Array.from(containingEntity.fields)
|
|
173
|
-
.filter(([, field]) => field.__typename === '_Column')
|
|
174
|
-
.map(([fieldName]) => fieldName),
|
|
175
|
-
)
|
|
176
|
-
|
|
177
|
-
yield new LocalizedBindingError(
|
|
178
|
-
`Field '${fieldName}' doesn't exist on entity '${containingEntity.name}'. ` +
|
|
179
|
-
(alternative ? `Did you mean '${alternative}'?` : ''),
|
|
180
|
-
[marker],
|
|
181
|
-
)
|
|
182
|
-
return
|
|
183
|
-
}
|
|
184
|
-
if (field.__typename === '_Relation') {
|
|
185
|
-
yield new LocalizedBindingError(
|
|
186
|
-
`Invalid field: the name '${fieldName}' on entity '${containingEntity.name}' ` +
|
|
187
|
-
`refers to a has-${
|
|
188
|
-
field.type === 'OneHasOne' || field.type === 'ManyHasOne' ? 'one' : 'many'
|
|
189
|
-
} relation but is being used as a field.`,
|
|
190
|
-
[marker],
|
|
191
|
-
)
|
|
192
|
-
return
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
// TODO check that defaultValue matches the type
|
|
196
|
-
// TODO run custom validators
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
private static *getUniqueWhereErrors(
|
|
200
|
-
schema: Schema,
|
|
201
|
-
containingEntity: SchemaEntity,
|
|
202
|
-
where: UniqueWhere,
|
|
203
|
-
): Generator<LocalizedBindingError, void> {
|
|
204
|
-
//
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
private static *getFilterErrors(
|
|
208
|
-
schema: Schema,
|
|
209
|
-
containingEntity: SchemaEntity,
|
|
210
|
-
filter: Filter,
|
|
211
|
-
): Generator<LocalizedBindingError, void> {
|
|
212
|
-
//
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
private static *nestErrorsIn(
|
|
216
|
-
source: HasOneRelationMarker | HasManyRelationMarker | EntityListSubTreeMarker | EntitySubTreeMarker,
|
|
217
|
-
to: Generator<LocalizedBindingError, void>,
|
|
218
|
-
): Generator<LocalizedBindingError, void> {
|
|
219
|
-
for (const error of to) {
|
|
220
|
-
yield error.nestedIn(source)
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
private static recommendAlternative(original: string, possibleAlternatives: Iterable<string>): string | undefined {
|
|
225
|
-
let bestAlternative: string | undefined = undefined
|
|
226
|
-
let bestAlternativeDistance = Number.MAX_SAFE_INTEGER
|
|
227
|
-
|
|
228
|
-
for (const alternative of possibleAlternatives) {
|
|
229
|
-
const distance = levenshtein(original, alternative)
|
|
230
|
-
|
|
231
|
-
if (distance < bestAlternativeDistance) {
|
|
232
|
-
bestAlternative = alternative
|
|
233
|
-
bestAlternativeDistance = distance
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
return bestAlternative
|
|
237
|
-
}
|
|
238
|
-
}
|