Skip to content

Commit

Permalink
feat: Validate iterator query
Browse files Browse the repository at this point in the history
  • Loading branch information
ddeboer committed Jun 15, 2024
1 parent ef0c1ab commit 985c8df
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/iterator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import EventEmitter from 'node:events';
import type {SelectQuery} from 'sparqljs';
import sparqljs, {type SelectQuery, type VariableTerm} from 'sparqljs';
import type Stage from './stage.js';
import type {NamedNode} from '@rdfjs/types';
import {NamedNode} from '@rdfjs/types';
import getSPARQLQuery from './utils/getSPARQLQuery.js';
import {type Bindings} from '@comunica/types';
import getSPARQLQueryString from './utils/getSPARQLQueryString.js';
Expand Down Expand Up @@ -35,6 +35,7 @@ export default class Iterator extends EventEmitter<Events> {
stage.configuration.iterator.batchSize ??
this.query.limit ??
DEFAULT_LIMIT;
this.validateQuery();
this.endpoint = getEndpoint(stage);
this.engine = getEngine(this.endpoint);
if (stage.configuration.iterator.delay !== undefined) {
Expand All @@ -54,7 +55,7 @@ export default class Iterator extends EventEmitter<Events> {
const queryString = getSPARQLQueryString(this.query);
const error = (e: unknown): Error =>
new Error(
`The Iterator did not run succesfully, it could not get the results from the endpoint ${
`The Iterator did not run successfully, it could not get the results from the endpoint ${
this.source
} (offset: ${this.$offset}, limit ${this.query.limit}): ${
(e as Error).message
Expand Down Expand Up @@ -96,4 +97,17 @@ export default class Iterator extends EventEmitter<Events> {
}
}, this.delay);
}

private validateQuery() {
if (
!this.query.variables.find(
v =>
v instanceof sparqljs.Wildcard || (v as VariableTerm).value === 'this'
)
) {
throw new Error(
'The SPARQL query must select either a variable ?this or a wildcard *'
);
}
}
}

0 comments on commit 985c8df

Please sign in to comment.