Skip to content

Commit 0ac86e6

Browse files
authored
Merge pull request #14 from champ96k/Add-compile-time-query-validation
Add compile-time query validation
2 parents 3edf660 + 9c2cc4c commit 0ac86e6

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

lib/src/query/query_engine.dart

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,27 @@ class Query<T> {
1414
this.limit,
1515
this.offset,
1616
this.aggregations = const [],
17-
});
17+
}) {
18+
_validateQuery();
19+
}
1820
final List<QueryPredicate<T>> predicates;
1921
final List<QuerySort<T>> sorts;
2022
final int? limit;
2123
final int? offset;
2224
final List<QueryAggregation<T>> aggregations;
2325

26+
void _validateQuery() {
27+
if (limit != null && limit! < 0) {
28+
throw ArgumentError('Limit must be non-negative');
29+
}
30+
if (offset != null && offset! < 0) {
31+
throw ArgumentError('Offset must be non-negative');
32+
}
33+
if (limit != null && offset != null && limit! + offset! < 0) {
34+
throw ArgumentError('Limit + offset must be non-negative');
35+
}
36+
}
37+
2438
Query<T> where(QueryPredicate<T> predicate) {
2539
return Query(
2640
predicates: [...predicates, predicate],
@@ -97,6 +111,7 @@ class QueryEngine {
97111

98112
/// Execute a query and return the results
99113
Future<List<T>> query<T>(Query<T> query) async {
114+
query._validateQuery();
100115
final results = <T>[];
101116

102117
// Get all items of type T from storage
@@ -135,6 +150,7 @@ class QueryEngine {
135150

136151
/// Watch for changes matching a query
137152
Stream<R> watch<T, R>(Query<T> query) {
153+
query._validateQuery();
138154
final stream = _changeController.stream
139155
.where((event) => event.type == T)
140156
.map((event) => event.value as T)

0 commit comments

Comments
 (0)