Skip to content

Commit

Permalink
Merge pull request #55 from netglade/fix/string-isuri-validation
Browse files Browse the repository at this point in the history
Fix string validator isUri
  • Loading branch information
petrnymsa authored Apr 15, 2024
2 parents a9e6ae1 + 388c8c8 commit e634904
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 21 deletions.
3 changes: 3 additions & 0 deletions glade_forms/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2.0.1
- **[Fix]**: Fix `isUri()` to handle URL corectly

## 2.0.0
- **[Breaking]**: TextEditingController is no more created automatically. When TextEditingController is used, input's behavior is slightly changed. See README.md for full info.
- **[Breaking]**: GladeInput's controller is now private. Use factory constructors to create input.
Expand Down
2 changes: 0 additions & 2 deletions glade_forms/lib/src/core/convert_error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ class ConvertError<T> extends GladeInputError<T> with EquatableMixin implements
final Object? _convertError;

@override
// TODO(dcm): wait for DCM update.
// ignore: list-all-equatable-fields, wait for DCM update
List<Object?> get props => [input, devError, error, key, _convertError];

String get targetType => T.runtimeType.toString();
Expand Down
2 changes: 1 addition & 1 deletion glade_forms/lib/src/core/glade_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ class GladeInput<T> {
'If type is not nullable, at least one of value or initialValue must be set',
);

final validatorInstance = validator?.call(GladeValidator<T>()) ?? GladeValidator<T>().build();
final validatorInstance = validator?.call(GladeValidator()) ?? GladeValidator<T>().build();

return GladeInput._(
value: (value ?? initialValue) as T,
Expand Down
24 changes: 14 additions & 10 deletions glade_forms/lib/src/validator/string_validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,32 @@ class StringValidator extends GladeValidator<String> {
/// Checks that value is valid URL address.
///
/// [requiresScheme] - if true HTTP(S) is mandatory.
void isUri({
/// Default [key] is [ GladeErrorKeys.stringNotUrl].
void isUrl({
OnValidateError<String>? devError,
bool allowEmpty = false,
bool requiresScheme = false,
Object? key,
Object key = GladeErrorKeys.stringNotUrl,
}) =>
satisfy(
(x) {
if (x.isEmpty) {
(value) {
if (value.isEmpty) {
return allowEmpty;
}

final uri = Uri.tryParse(x);

if (uri == null) return false;
final quantifier = requiresScheme ? '{1}' : '?';

if (requiresScheme) return uri.hasScheme;
final exp = RegExp(
// ignore: unnecessary_string_escapes, regex pattern.
r'^(?:https?:\/\/)' +
quantifier +
r'(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)$',
);

return true;
return exp.hasMatch(value);
},
devError: devError ?? (value) => 'Value "${value ?? 'NULL'}" is not valid URL address',
key: key ?? GladeErrorKeys.stringNotUrl,
key: key,
);

/// Matches provided regex [pattern].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ abstract class GladeValidatorError<T> extends GladeInputError<T> with EquatableM
String get devErrorMessage => devError(value);

@override
List<Object?> get props =>
// ignore: list-all-equatable-fields, wait for DCM update
[value, devError, key, error, isConversionError, isNullError];
List<Object?> get props => [value, devError, key, error, isConversionError, isNullError];

GladeValidatorError({
required this.value,
Expand Down
2 changes: 1 addition & 1 deletion glade_forms/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: glade_forms
description: A universal way to define form validators with support of translations.
version: 2.0.0
version: 2.0.1
repository: https://github.com/netglade/glade_forms
issue_tracker: https://github.com/netglade/glade_forms/issues
screenshots:
Expand Down
12 changes: 8 additions & 4 deletions glade_forms/test/string_validator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void main() {

group('isUrl', () {
test('When value is empty, isUrl() fails', () {
final validator = (StringValidator()..isUri()).build();
final validator = (StringValidator()..isUrl()).build();

final result = validator.validate('');

Expand All @@ -65,13 +65,17 @@ void main() {
for (final testCase in [
('https://x.test.com', true, true),
('http://test.com/test', true, true),
('file://sdsasa.asdas.com', false, true),
('file://sdsasa.asdas.com', false, false),
('file://sdsasa.asdas.com', true, false),
('test.com/asdsa?qqe=sa44%20sda', false, true),
('www.domain.com/asdsa?qqe=sa44%20sda', false, true),
('@x.sada/https://file:sadad', true, false),
('sadscom:/192.168.1.1', false, true),
('sadscom:/192.168.1.1', false, false),
('noturl', false, false),
('noturl', false, false),
]) {
test('When URL is ${testCase.$1}, isUrl(http: ${testCase.$2}) ${testCase.$3 ? 'pass' : 'fails'}', () {
final validator = (StringValidator()..isUri(requiresScheme: testCase.$2)).build();
final validator = (StringValidator()..isUrl(requiresScheme: testCase.$2)).build();

final result = validator.validate(testCase.$1);

Expand Down

0 comments on commit e634904

Please sign in to comment.