Skip to content
This repository has been archived by the owner on Jun 21, 2023. It is now read-only.

Refactoring RepositoryForm Validators #2395

Open
wants to merge 1 commit into
base: essentials-publish
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/GitHub.App/SampleData/SampleViewModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public string RepositoryName
set;
}

public ReactivePropertyValidator<string> RepositoryNameValidator
public ReactivePropertyValidator<(string repositoryName, IConnection connection, IAccount account)> SafeRepositoryNameWarningValidator
{
get;
private set;
Expand All @@ -127,7 +127,7 @@ public string SafeRepositoryName
private set;
}

public ReactivePropertyValidator<string> SafeRepositoryNameWarningValidator
public ReactivePropertyValidator<(string repositoryName, IConnection connection, IAccount account)> RepositoryNameValidator
{
get;
private set;
Expand Down
29 changes: 14 additions & 15 deletions src/GitHub.App/ViewModels/Dialog/RepositoryCreationViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,21 @@ public RepositoryCreationViewModel(
.IfContainsInvalidPathChars(Resources.RepositoryCreationClonePathInvalidCharacters)
.IfPathNotRooted(Resources.RepositoryCreationClonePathInvalid);

var nonNullRepositoryName = this.WhenAny(
x => x.RepositoryName,
x => x.BaseRepositoryPath,
(x, y) => x.Value)
.WhereNotNull();

RepositoryNameValidator = ReactivePropertyValidator.ForObservable(nonNullRepositoryName)
.IfNullOrEmpty(Resources.RepositoryNameValidatorEmpty)
.IfTrue(x => x.Length > 100, Resources.RepositoryNameValidatorTooLong)
.IfTrue(IsAlreadyRepoAtPath, Resources.RepositoryNameValidatorAlreadyExists);

SafeRepositoryNameWarningValidator = ReactivePropertyValidator.ForObservable(nonNullRepositoryName)
.Add(repoName =>
var nameValidationConditions = this.WhenAny(
model => model.RepositoryName,
model => model.SelectedAccount,
(repositoryName, account) => (repositoryName: repositoryName.Value, connection: (IConnection) null, account: account.Value))
.Where(tuple => tuple.repositoryName != null);

RepositoryNameValidator = ReactivePropertyValidator.ForObservable(nameValidationConditions)
.IfTrue(tuple => string.IsNullOrEmpty(tuple.repositoryName), Resources.RepositoryNameValidatorEmpty)
.IfTrue(tuple => tuple.repositoryName.Length > 100, Resources.RepositoryNameValidatorTooLong);

SafeRepositoryNameWarningValidator = ReactivePropertyValidator.ForObservable(nameValidationConditions)
.Add(tuple =>
{
var parsedReference = GetSafeRepositoryName(repoName);
return parsedReference != repoName ? String.Format(CultureInfo.CurrentCulture, Resources.SafeRepositoryNameWarning, parsedReference) : null;
var parsedReference = GetSafeRepositoryName(tuple.repositoryName);
return parsedReference != tuple.repositoryName ? String.Format(CultureInfo.CurrentCulture, Resources.SafeRepositoryNameWarning, parsedReference) : null;
});

CreateRepository = InitializeCreateRepositoryCommand();
Expand Down
4 changes: 2 additions & 2 deletions src/GitHub.App/ViewModels/RepositoryFormViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public string RepositoryName
set { this.RaiseAndSetIfChanged(ref repositoryName, value); }
}

public ReactivePropertyValidator<string> RepositoryNameValidator { get; protected set; }
public ReactivePropertyValidator<(string repositoryName, IConnection connection, IAccount account)> RepositoryNameValidator { get; protected set; }

/// <summary>
/// Name of the repository after fixing it to be safe (dashes instead of spaces, etc)
Expand All @@ -62,7 +62,7 @@ public string SafeRepositoryName
get { return safeRepositoryName.Value; }
}

public ReactivePropertyValidator<string> SafeRepositoryNameWarningValidator { get; protected set; }
public ReactivePropertyValidator<(string repositoryName, IConnection connection, IAccount account)> SafeRepositoryNameWarningValidator { get; protected set; }

IAccount selectedAccount;
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,17 @@ public RepositoryPublishViewModel(
if (!string.IsNullOrEmpty(defaultRepositoryName))
RepositoryName = defaultRepositoryName;

this.WhenAny(x => x.SelectedConnection, x => x.SelectedAccount,
(a,b) => true)
.Where(x => RepositoryNameValidator.ValidationResult != null && SafeRepositoryNameWarningValidator.ValidationResult != null)
.Subscribe(async _ =>
{
var name = RepositoryName;
RepositoryName = null;
await RepositoryNameValidator.ResetAsync();
await SafeRepositoryNameWarningValidator.ResetAsync();
RepositoryName = name;
});
// this.WhenAny(x => x.SelectedConnection, x => x.SelectedAccount,
Copy link
Collaborator

@jcansdale jcansdale Jun 28, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should delete these comments!

// (a,b) => true)
// .Where(x => RepositoryNameValidator.ValidationResult != null && SafeRepositoryNameWarningValidator.ValidationResult != null)
// .Subscribe(async _ =>
// {
// var name = RepositoryName;
// RepositoryName = null;
// await RepositoryNameValidator.ResetAsync();
// await SafeRepositoryNameWarningValidator.ResetAsync();
// RepositoryName = name;
// });
}

public ReactiveCommand<Unit, ProgressState> PublishRepository { get; private set; }
Expand Down Expand Up @@ -173,20 +173,22 @@ IObservable<ProgressState> OnPublishRepository()

void InitializeValidation()
{
var nonNullRepositoryName = this.WhenAny(
x => x.RepositoryName,
x => x.Value)
.WhereNotNull();

RepositoryNameValidator = ReactivePropertyValidator.ForObservable(nonNullRepositoryName)
.IfNullOrEmpty(Resources.RepositoryNameValidatorEmpty)
.IfTrue(x => x.Length > 100, Resources.RepositoryNameValidatorTooLong);

SafeRepositoryNameWarningValidator = ReactivePropertyValidator.ForObservable(nonNullRepositoryName)
.Add(repoName =>
var nameValidationConditions = this.WhenAny(model => model.RepositoryName,
model => model.SelectedConnection,
model => model.SelectedAccount,
(repositoryName, connection, account) => (repositoryName: repositoryName.Value,
connection: connection.Value, account: account.Value))
.Where(tuple => tuple.repositoryName != null);

RepositoryNameValidator = ReactivePropertyValidator.ForObservable(nameValidationConditions)
.IfTrue(tuple => string.IsNullOrEmpty(tuple.repositoryName), Resources.RepositoryNameValidatorEmpty)
jcansdale marked this conversation as resolved.
Show resolved Hide resolved
.IfTrue(tuple => tuple.repositoryName.Length > 100, Resources.RepositoryNameValidatorTooLong);

SafeRepositoryNameWarningValidator = ReactivePropertyValidator.ForObservable(nameValidationConditions)
.Add(tuple =>
{
var parsedReference = GetSafeRepositoryName(repoName);
return parsedReference != repoName ? String.Format(CultureInfo.CurrentCulture, Resources.SafeRepositoryNameWarning, parsedReference) : null;
var parsedReference = GetSafeRepositoryName(tuple.repositoryName);
return parsedReference != tuple.repositoryName ? String.Format(CultureInfo.CurrentCulture, Resources.SafeRepositoryNameWarning, parsedReference) : null;
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/GitHub.Exports.Reactive/ViewModels/IRepositoryForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public interface IRepositoryForm : IViewModel
/// dashes.
/// </summary>
string SafeRepositoryName { get; }
ReactivePropertyValidator<string> RepositoryNameValidator { get; }
ReactivePropertyValidator<string> SafeRepositoryNameWarningValidator { get; }
ReactivePropertyValidator<(string repositoryName, IConnection connection, IAccount account)> RepositoryNameValidator { get; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the code, I can't see where we're using the connection or account for validation. I wonder if once upon a time we were checking for an existing repository with the same name as part of the validation? It looks like this check is now done and the error surfaced when the user attempts to create the repository.

I think we might be able to simplify this to use just the repositoryName. I'm wondering if the buggy code was actually completely obsolete. ;-)

ReactivePropertyValidator<(string repositoryName, IConnection connection, IAccount account)> SafeRepositoryNameWarningValidator { get; }

string Description { get; set; }

Expand Down