Skip to content

Commit dfd5492

Browse files
committed
Formatters & Validators #4
1 parent 2bf0cbf commit dfd5492

File tree

5 files changed

+247
-61
lines changed

5 files changed

+247
-61
lines changed

lib/src/api/extensions.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:math';
22

3+
import 'package:collection/collection.dart';
34
import 'package:intl/intl.dart';
45

56
import '../../codelessly_api.dart';
@@ -77,3 +78,22 @@ extension PatternExt on Pattern {
7778
return match.start == 0 && match.end == input.length;
7879
}
7980
}
81+
82+
/// A helper extension that adds additional functionality to
83+
/// [TextInputValidatorModel] iterable.
84+
extension TextInputValidatorModelIterableExt<T extends TextInputValidatorModel>
85+
on Iterable<T> {
86+
/// Returns the first [TextInputValidatorModel] with the given [name].
87+
/// Returns null if no validator is found.
88+
T? byNameOrNull(String name) =>
89+
firstWhereOrNull((model) => model.name == name);
90+
91+
/// Returns the first [TextInputValidatorModel] with the given [name].
92+
T byName(String name) {
93+
final T? model = byNameOrNull(name);
94+
if (model == null) {
95+
throw ArgumentError('No validator found with the name: $name');
96+
}
97+
return model;
98+
}
99+
}

0 commit comments

Comments
 (0)