Skip to content
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
8 changes: 4 additions & 4 deletions src/Secullum.Validation/CollectionValidation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Secullum.Validation
public class CollectionValidation<T> : IValidation<T, CollectionValidation<T>> where T : class
{
private ICollection<Validation<T>> validationCollection;

public CollectionValidation(ICollection<T> targetCollection) : this(targetCollection, null)
{
}
Expand All @@ -23,7 +23,7 @@ public CollectionValidation(ICollection<T> targetCollection, DbContext dbContext
validationCollection.Add(new Validation<T>(item, dbContext));
}
}

public CollectionValidation<T> HasDisplayText(Expression<Func<T, string>> expression, string displayText)
{
foreach (var validation in validationCollection)
Expand Down Expand Up @@ -140,7 +140,7 @@ public CollectionValidation<T> IsRequired(Expression<Func<T, int?>> expression)
{
validation.IsRequired(expression);
}

return this;
}

Expand Down Expand Up @@ -193,7 +193,7 @@ public CollectionValidation<T> HasMaxLength(Expression<Func<T, string>> expressi

return this;
}

public CollectionValidation<T> IsEmail(Expression<Func<T, string>> expression)
{
foreach (var validation in validationCollection)
Expand Down
2 changes: 1 addition & 1 deletion src/Secullum.Validation/Localization.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static Localization()
englishDictionary.Add(StringTypes.HasMaxLengthMessage, "The field {0} must have at most {1} characters.");
portugueseDictionary.Add(StringTypes.HasMaxLengthMessage, "O campo {0} deve possuir no máximo {1} caracteres.");
spanishDictionary.Add(StringTypes.HasMaxLengthMessage, "El campo {0} debe poseer máximo {1} caracteres.");

englishDictionary.Add(StringTypes.IsEmailMessage, "{0} invalid.");
portugueseDictionary.Add(StringTypes.IsEmailMessage, "{0} inválido.");
spanishDictionary.Add(StringTypes.IsEmailMessage, "{0} no válido.");
Expand Down
6 changes: 3 additions & 3 deletions src/Secullum.Validation/Secullum.Validation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
<PropertyGroup>
<Description>Secullum.Validation</Description>
<AssemblyTitle>Secullum.Validation</AssemblyTitle>
<VersionPrefix>1.1.0</VersionPrefix>
<TargetFramework>netstandard2.0</TargetFramework>
<VersionPrefix>1.1.12764.1</VersionPrefix>
<TargetFramework>net10.0</TargetFramework>
<AssemblyName>Secullum.Validation</AssemblyName>
<PackageId>Secullum.Validation</PackageId>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>git://github.com/Secullum/Secullum.Validation</RepositoryUrl>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.32" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.1" />
</ItemGroup>
</Project>
18 changes: 9 additions & 9 deletions src/Secullum.Validation/Validation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private Validation<T> HasDisplayText(LambdaExpression expression, string display

return this;
}

public Validation<T> IsRequired(Expression<Func<T, string>> expression)
{
ThrowIfNotMemberAccessExpression(expression.Body);
Expand Down Expand Up @@ -192,12 +192,12 @@ public Validation<T> HasMaxLength(Expression<Func<T, string>> expression, int ma
}

var value = expression.Compile()(target);

if (value != null && value.Length > maxLength)
{
AddError((MemberExpression)expression.Body, GetString(HasMaxLengthMessage), maxLength);
}

return this;
}

Expand All @@ -207,7 +207,7 @@ public Validation<T> IsEmail(Expression<Func<T, string>> expression)

var value = expression.Compile()(target);
var regex = new Regex(@"^[a-zA-Z0-9_\.-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-\.]+$");

if (!string.IsNullOrEmpty(value) && !regex.IsMatch(value))
{
AddError((MemberExpression)expression.Body, GetString(IsEmailMessage));
Expand Down Expand Up @@ -267,7 +267,7 @@ private Validation<T> IsUnique(LambdaExpression expression, object propValue)

var idProp = typeof(T).GetTypeInfo().GetDeclaredProperty("Id");
var idValue = idProp.GetValue(target);

// x.Name == "fernando"
var equalExpression = Expression.Equal(
expression.Body,
Expand All @@ -292,7 +292,7 @@ private Validation<T> IsUnique(LambdaExpression expression, object propValue)
{
AddError((MemberExpression)expression.Body, GetString(IsUniqueMessage));
}

return this;
}

Expand All @@ -306,7 +306,7 @@ public Validation<T> IsCpf(Expression<Func<T, string>> expression)
{
AddError((MemberExpression)expression.Body, GetString(IsCpfMessage));
}

return this;
}

Expand Down Expand Up @@ -485,10 +485,10 @@ private void AddError(MemberExpression expression, string message, params object
var formatArgsList = new List<object>();

displayTextDictionary.TryGetValue(expression.Member, out propertyDisplayText);

formatArgsList.Add(propertyDisplayText ?? propertyName);
formatArgsList.AddRange(formatArgs);

errorList.Add(new ValidationError(propertyName, string.Format(message, formatArgsList.ToArray())));
}

Expand Down
16 changes: 8 additions & 8 deletions test/Secullum.Validation.Tests/CollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void IsRequired_GivenValidField_DontReturnErrors()

foreach (var lista in errors)
{
Assert.Equal(0, lista.Count);
Assert.Empty(lista);
}
}

Expand All @@ -37,14 +37,14 @@ public void IsRequired_GivenEmptyField_ReturnError(string name)
var people = new List<Person>();

people.Add(new Person { Name = name });

var errors = new CollectionValidation<Person>(people)
.IsRequired(x => x.Name)
.ToList();

foreach (var lista in errors)
{
Assert.Equal(1, lista.Count);
Assert.Single(lista);
Assert.Equal("Name", lista[0].Property);
}
}
Expand All @@ -66,7 +66,7 @@ public void HasMaxLength_GivenValidField_DontReturnErrors(string name, int maxLe

foreach (var lista in errors)
{
Assert.Equal(0, lista.Count);
Assert.Empty(lista);
}
}

Expand All @@ -76,12 +76,12 @@ public void HasMaxLength_GivenInvalidField_ReturnError()
var people = new List<Person>();

people.Add(new Person { Name = "Fernando" });

var errors = new CollectionValidation<Person>(people)
.HasMaxLength(x => x.Name, 5)
.ToList();

Assert.Equal(1, errors.Count);
Assert.Single(errors);
Assert.Equal("Name", errors[0][0].Property);
}

Expand All @@ -96,7 +96,7 @@ public void HasCustomValidation_GivenFalseCondition_ReturnErrors()
.HasCustomValidation(x => false, "Email", "Preencha o campo Email corretamente")
.ToList();

Assert.Equal(1, errors[0].Count);
Assert.Single(errors[0]);
Assert.Equal("Preencha o campo Email corretamente", errors[0][0].Message);
}

Expand All @@ -115,7 +115,7 @@ public void IsSmallDateTime_GivenValidField_DontReturnErrors(int year, int month
.IsSmallDateTime(x => x.Birth)
.ToList();

Assert.Equal(0, errors[0].Count);
Assert.Empty(errors[0]);
}

}
Expand Down
6 changes: 3 additions & 3 deletions test/Secullum.Validation.Tests/HasCustomValidationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void HasCustomValidation_GivenSimpleFalseCondition_ReturnErrors()

Assert.Equal("Preencha o campo Email corretamente", errors[0].Message);
}

[Fact]
public void HasCustomValidation_GivenTrueCondition_DontReturnErrors()
{
Expand All @@ -41,7 +41,7 @@ public void HasCustomValidation_GivenTrueCondition_DontReturnErrors()
.HasCustomValidation(x => true, "Email", "Preencha o campo Email corretamente")
.ToList();

Assert.Equal(0, errors.Count);
Assert.Empty(errors);
}

[Fact]
Expand All @@ -53,7 +53,7 @@ public void HasCustomValidation_GivenFalseCondition_ReturnErrors()
.HasCustomValidation(x => !((x.Email == "unknown@domain.net" || x.Id == 1) && x.Age > 0), "Email", "Preencha o campo Email corretamente")
.ToList();

Assert.Equal(1, errors.Count);
Assert.Single(errors);
Assert.Equal("Preencha o campo Email corretamente", errors[0].Message);
}
}
Expand Down
8 changes: 4 additions & 4 deletions test/Secullum.Validation.Tests/HasDisplayTextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void HasDisplayText_GivenDisplayTextNullableIntValue_UsesIt()
{
SetCurrentThreadCulture(new CultureInfo("pt-BR"));

var person = new Person() { Zipcode = 42 } ;
var person = new Person() { Zipcode = 42 };

var errors = new Validation<Person>(person)
.HasDisplayText(x => x.Zipcode, "Zipcode")
Expand All @@ -74,7 +74,7 @@ public void HasDisplayText_GivenDisplayTextFloatValue_UsesIt()
{
SetCurrentThreadCulture(new CultureInfo("pt-BR"));

var person = new Person() ;
var person = new Person();

var errors = new Validation<Person>(person)
.HasDisplayText(x => x.Height, "Height")
Expand All @@ -84,13 +84,13 @@ public void HasDisplayText_GivenDisplayTextFloatValue_UsesIt()
Assert.Equal("Height", errors[0].Property);
Assert.Equal($"O campo Height deve ser preenchido com valores entre 100,01 e 250,02.", errors[0].Message);
}

[Fact]
public void HasDisplayText_GivenDisplayTextNullableFloatValue_UsesIt()
{
SetCurrentThreadCulture(new CultureInfo("pt-BR"));

var person = new Person() {Weight = 5};
var person = new Person() { Weight = 5 };

var errors = new Validation<Person>(person)
.HasDisplayText(x => x.Weight, "Weight")
Expand Down
8 changes: 4 additions & 4 deletions test/Secullum.Validation.Tests/HasMaxLengthTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void HasMaxLength_GivenValidField_DontReturnErrors(string name, int maxLe
.HasMaxLength(x => x.Name, maxLength)
.ToList();

Assert.Equal(0, errors.Count);
Assert.Empty(errors);
}

[Fact]
Expand All @@ -30,10 +30,10 @@ public void HasMaxLength_GivenInvalidField_ReturnError()
.HasMaxLength(x => x.Name, 5)
.ToList();

Assert.Equal(1, errors.Count);
Assert.Single(errors);
Assert.Equal("Name", errors[0].Property);
}

[Fact]
public void HasMaxLength_GivenInvalidExpression_ThrowsException()
{
Expand All @@ -46,7 +46,7 @@ public void HasMaxLength_GivenInvalidExpression_ThrowsException()
.ToList();
});
}

[Theory]
[InlineData(0)]
[InlineData(-1)]
Expand Down
Loading