Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding FluentValidation support for input validator #53

Merged
merged 1 commit into from
Apr 27, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,20 @@
<PackageReference Include="CommunityToolkit.Mvvm">
<Version>7.1.2</Version>
</PackageReference>
<PackageReference Include="MADE.Collections" Version="1.5.0" />
<PackageReference Include="MADE.Data.Converters" Version="1.5.0" />
<PackageReference Include="MADE.Data.Validation" Version="1.5.0" />
<PackageReference Include="MADE.Diagnostics" Version="1.5.0" />
<PackageReference Include="MADE.Foundation" Version="1.5.0" />
<PackageReference Include="MADE.Networking" Version="1.5.0" />
<PackageReference Include="MADE.Runtime" Version="1.5.0" />
<PackageReference Include="MADE.Threading" Version="1.5.0" />
<PackageReference Include="FluentValidation">
<Version>10.4.0</Version>
</PackageReference>
<PackageReference Include="MADE.Collections" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Data.Converters" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Data.Validation" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Data.Validation.FluentValidation">
<Version>1.6.0-preview1</Version>
</PackageReference>
<PackageReference Include="MADE.Diagnostics" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Foundation" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Networking" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Runtime" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Threading" Version="1.6.0-preview1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection">
<Version>5.0.2</Version>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
public FluentValidatorCollection<string> FluentValidationValidators { get; } = new(GetFluentValidators());

public string FluentValidationInputText
{
get => fluentValidationInputText;
set => this.SetProperty(ref fluentValidationInputText, value);
}

private static IEnumerable<AbstractValidator<string>> GetFluentValidators()
{
var requiredValidator = new InlineValidator<string>();
requiredValidator
.RuleFor(x => x)
.NotEmpty()
.WithMessage("A value is required.");

var maxLengthValidator = new InlineValidator<string>();
maxLengthValidator
.RuleFor(x => x.Length)
.LessThanOrEqualTo(16)
.WithMessage("The length of the value must be less than or equal to 16 characters.");

return new[] { requiredValidator, maxLengthValidator };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Page
x:Class="MADE.Samples.Features.Samples.Pages.InputValidatorPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:made="using:MADE.UI.Controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:win="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">

<Grid>
<made:InputValidator
x:Name="FluentValidationValidator"
Input="{x:Bind FluentValdiationTextBox.Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Validators="{x:Bind FluentValidationValidators}">
<TextBox
x:Name="FluentValdiationTextBox"
Header="TextBox with InputValidator using FluentValidation"
Text="{x:Bind FluentValidationInputText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</made:InputValidator>
</Grid>
</Page>
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<ScrollViewer Grid.Row="2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
Expand All @@ -75,6 +76,7 @@

<samples:SampleControl
Grid.Row="1"
Margin="0,0,0,32"
CodeSource="InputValidator/InputValidatorDatePickerCode.txt"
SampleName="A DatePicker wrapped with the InputValidator with a required value and date between the last 7 days and the next 7 days"
XamlSource="InputValidator/InputValidatorDatePickerXaml.txt">
Expand All @@ -90,6 +92,25 @@
</made:InputValidator>
</samples:SampleControl.Sample>
</samples:SampleControl>

<samples:SampleControl
Grid.Row="2"
Margin="0,0,0,32"
CodeSource="InputValidator/InputValidatorFluentValidationCode.txt"
SampleName="An input validator using FluentValidation validators"
XamlSource="InputValidator/InputValidatorFluentValidationXaml.txt">
<samples:SampleControl.Sample>
<made:InputValidator
x:Name="FluentValidationValidator"
Input="{x:Bind FluentValdiationTextBox.Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Validators="{x:Bind ViewModel.FluentValidationValidators}">
<TextBox
x:Name="FluentValdiationTextBox"
Header="TextBox with InputValidator using FluentValidation"
Text="{x:Bind ViewModel.FluentValidationInputText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
</made:InputValidator>
</samples:SampleControl.Sample>
</samples:SampleControl>
</Grid>
</ScrollViewer>
</Grid>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
namespace MADE.Samples.Features.Samples.ViewModels
{
using System;
using System.Collections.Generic;
using CommunityToolkit.Mvvm.Messaging;
using FluentValidation;
using MADE.Data.Validation;
using MADE.Data.Validation.Validators;
using MADE.UI.Views.Navigation;
Expand All @@ -11,26 +13,57 @@ public class InputValidatorPageViewModel : PageViewModel
{
private string inputText;
private DateTimeOffset? inputDate;
private string fluentValidationInputText;

public InputValidatorPageViewModel(INavigationService navigationService, IMessenger messenger)
: base(navigationService, messenger)
{
}

public ValidatorCollection InputTextValidators { get; } = new ValidatorCollection { new RequiredValidator(), new MaxLengthValidator(16) };
public ValidatorCollection InputTextValidators { get; } =
new ValidatorCollection { new RequiredValidator(), new MaxLengthValidator(16) };

public string InputText
{
get => inputText;
set => this.SetProperty(ref inputText, value);
}

public ValidatorCollection InputDateValidators { get; } = new ValidatorCollection { new RequiredValidator(), new BetweenValidator(DateTimeOffset.Now.AddDays(-7), DateTimeOffset.Now.AddDays(7)) };
public ValidatorCollection InputDateValidators { get; } = new ValidatorCollection
{
new RequiredValidator(),
new BetweenValidator(DateTimeOffset.Now.AddDays(-7), DateTimeOffset.Now.AddDays(7))
};

public DateTimeOffset? InputDate
{
get => inputDate;
set => this.SetProperty(ref inputDate, value);
}

public FluentValidatorCollection<string> FluentValidationValidators { get; } = new(GetFluentValidators());

public string FluentValidationInputText
{
get => fluentValidationInputText;
set => this.SetProperty(ref fluentValidationInputText, value);
}

private static IEnumerable<AbstractValidator<string>> GetFluentValidators()
{
var requiredValidator = new InlineValidator<string>();
requiredValidator
.RuleFor(x => x)
.NotEmpty()
.WithMessage("A value is required.");

var maxLengthValidator = new InlineValidator<string>();
maxLengthValidator
.RuleFor(x => x.Length)
.LessThanOrEqualTo(16)
.WithMessage("The length of the value must be less than or equal to 16 characters.");

return new[] { requiredValidator, maxLengthValidator };
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@
<Generator>
</Generator>
</Content>
<Content Include="$(MSBuildThisFileDirectory)Features\Samples\Assets\InputValidator\InputValidatorFluentValidationCode.txt">
<SubType>Designer</SubType>
<Generator>
</Generator>
</Content>
<Content Include="$(MSBuildThisFileDirectory)Features\Samples\Assets\InputValidator\InputValidatorTextBoxCode.txt">
<SubType>Designer</SubType>
<Generator>
Expand All @@ -111,6 +116,11 @@
<Generator>
</Generator>
</Content>
<Content Include="$(MSBuildThisFileDirectory)Features\Samples\Assets\InputValidator\InputValidatorFluentValidationXaml.txt">
<SubType>Designer</SubType>
<Generator>
</Generator>
</Content>
<Content Include="$(MSBuildThisFileDirectory)Features\Samples\Assets\InputValidator\InputValidatorTextBoxXaml.txt">
<SubType>Designer</SubType>
<Generator>
Expand Down
22 changes: 14 additions & 8 deletions samples/MADE.Samples/MADE.Samples.UWP/MADE.Samples.UWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@
<PackageReference Include="CommunityToolkit.Mvvm">
<Version>7.1.2</Version>
</PackageReference>
<PackageReference Include="MADE.Collections" Version="1.5.0" />
<PackageReference Include="MADE.Data.Converters" Version="1.5.0" />
<PackageReference Include="MADE.Data.Validation" Version="1.5.0" />
<PackageReference Include="MADE.Diagnostics" Version="1.5.0" />
<PackageReference Include="MADE.Foundation" Version="1.5.0" />
<PackageReference Include="MADE.Networking" Version="1.5.0" />
<PackageReference Include="MADE.Runtime" Version="1.5.0" />
<PackageReference Include="MADE.Threading" Version="1.5.0" />
<PackageReference Include="FluentValidation">
<Version>10.4.0</Version>
</PackageReference>
<PackageReference Include="MADE.Collections" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Data.Converters" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Data.Validation" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Data.Validation.FluentValidation">
<Version>1.6.0-preview1</Version>
</PackageReference>
<PackageReference Include="MADE.Diagnostics" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Foundation" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Networking" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Runtime" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Threading" Version="1.6.0-preview1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection">
<Version>5.0.2</Version>
</PackageReference>
Expand Down
18 changes: 10 additions & 8 deletions samples/MADE.Samples/MADE.Samples.Wasm/MADE.Samples.Wasm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,16 @@
<ItemGroup>
<!-- Note that for WebAssembly version 1.1.1 of the console logger required -->
<PackageReference Include="CommunityToolkit.Mvvm" Version="7.1.2" />
<PackageReference Include="MADE.Collections" Version="1.5.0" />
<PackageReference Include="MADE.Data.Converters" Version="1.5.0" />
<PackageReference Include="MADE.Data.Validation" Version="1.5.0" />
<PackageReference Include="MADE.Diagnostics" Version="1.5.0" />
<PackageReference Include="MADE.Foundation" Version="1.5.0" />
<PackageReference Include="MADE.Networking" Version="1.5.0" />
<PackageReference Include="MADE.Runtime" Version="1.5.0" />
<PackageReference Include="MADE.Threading" Version="1.5.0" />
<PackageReference Include="FluentValidation" Version="10.4.0" />
<PackageReference Include="MADE.Collections" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Data.Converters" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Data.Validation" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Data.Validation.FluentValidation" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Diagnostics" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Foundation" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Networking" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Runtime" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Threading" Version="1.6.0-preview1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.2" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.Windows.Compatibility" Version="5.0.0" />
Expand Down
22 changes: 14 additions & 8 deletions samples/MADE.Samples/MADE.Samples.iOS/MADE.Samples.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,20 @@
<PackageReference Include="CommunityToolkit.Mvvm">
<Version>7.1.2</Version>
</PackageReference>
<PackageReference Include="MADE.Collections" Version="1.5.0" />
<PackageReference Include="MADE.Data.Converters" Version="1.5.0" />
<PackageReference Include="MADE.Data.Validation" Version="1.5.0" />
<PackageReference Include="MADE.Diagnostics" Version="1.5.0" />
<PackageReference Include="MADE.Foundation" Version="1.5.0" />
<PackageReference Include="MADE.Networking" Version="1.5.0" />
<PackageReference Include="MADE.Runtime" Version="1.5.0" />
<PackageReference Include="MADE.Threading" Version="1.5.0" />
<PackageReference Include="FluentValidation">
<Version>10.4.0</Version>
</PackageReference>
<PackageReference Include="MADE.Collections" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Data.Converters" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Data.Validation" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Data.Validation.FluentValidation">
<Version>1.6.0-preview1</Version>
</PackageReference>
<PackageReference Include="MADE.Diagnostics" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Foundation" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Networking" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Runtime" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Threading" Version="1.6.0-preview1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection">
<Version>5.0.2</Version>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MADE.Collections" Version="1.5.0" />
<PackageReference Include="MADE.Data.Validation" Version="1.5.0" />
<PackageReference Include="MADE.Collections" Version="1.6.0-preview1" />
<PackageReference Include="MADE.Data.Validation" Version="1.6.0-preview1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MADE.Data.Validation" Version="1.5.0" />
<PackageReference Include="MADE.Data.Validation" Version="1.6.0-preview1" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/MADE.UI.Controls.Validator/IInputValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public interface IInputValidator
/// <summary>
/// Gets or sets the validators to run on the input.
/// </summary>
ValidatorCollection Validators { get; set; }
IValidatorCollection Validators { get; set; }
}
}
8 changes: 4 additions & 4 deletions src/MADE.UI.Controls.Validator/InputValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public partial class InputValidator : ContentControl, IInputValidator
/// </summary>
public static readonly DependencyProperty ValidatorsProperty = DependencyProperty.Register(
nameof(Validators),
typeof(ValidatorCollection),
typeof(IValidatorCollection),
typeof(InputValidator),
new PropertyMetadata(default(ValidatorCollection), (o, args) => ((InputValidator)o).InvokeValidators()));
new PropertyMetadata(default(IValidatorCollection), (o, args) => ((InputValidator)o).InvokeValidators()));

/// <summary>
/// Identifies the <see cref="FeedbackMessageStyle"/> dependency property.
Expand Down Expand Up @@ -79,9 +79,9 @@ public object Input
/// <summary>
/// Gets or sets the validators to run on the input.
/// </summary>
public ValidatorCollection Validators
public IValidatorCollection Validators
{
get => (ValidatorCollection)this.GetValue(ValidatorsProperty);
get => (IValidatorCollection)this.GetValue(ValidatorsProperty);
set => this.SetValue(ValidatorsProperty, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MADE.Data.Validation" Version="1.5.0" />
<PackageReference Include="MADE.Data.Validation" Version="1.6.0-preview1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MADE.Data.Converters" Version="1.5.0" />
<PackageReference Include="MADE.Data.Converters" Version="1.6.0-preview1" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/MADE.UI.Styling/MADE.UI.Styling.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MADE.Data.Validation" Version="1.5.0" />
<PackageReference Include="MADE.Data.Validation" Version="1.6.0-preview1" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/MADE.UI.ViewManagement/MADE.UI.ViewManagement.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MADE.Foundation" Version="1.5.0" />
<PackageReference Include="MADE.Foundation" Version="1.6.0-preview1" />
</ItemGroup>

<ItemGroup>
Expand Down