Phema.Validation.AspNetCore
1.1.3
The owner has unlisted this package.
This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package Phema.Validation.AspNetCore --version 1.1.3
NuGet\Install-Package Phema.Validation.AspNetCore -Version 1.1.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Phema.Validation.AspNetCore" Version="1.1.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Phema.Validation.AspNetCore --version 1.1.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Phema.Validation.AspNetCore, 1.1.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Phema.Validation.AspNetCore as a Cake Addin #addin nuget:?package=Phema.Validation.AspNetCore&version=1.1.3 // Install Phema.Validation.AspNetCore as a Cake Tool #tool nuget:?package=Phema.Validation.AspNetCore&version=1.1.3
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Phema.Validation
C# tiny, fast and customizable validation library
- Core library
- Extensions
- Conditions
- Tests
- AspNetCore integration
Validation
- Model
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
- Validation
public class PersonValidation : IValidation<Person>
{
public void Validate(IValidationContext validationContext, Person person)
{
validationContext.When(person, p => p.Name)
.IsNullOrWhitespace()
.AddError<PersonValidationComponent>(c => c.NameMustBeSet);
validationContext.When(person, p => p.Name)
.IsLess(2)
.AddError<PersonValidationComponent>(c => c.NameIsShort);
validationContext.When(person, p => p.Name)
.IsGreater(20)
.AddError<PersonValidationComponent>(c => c.NameIsLong);
validationContext.When(person, p => p.Age)
.IsNotGreater(18)
.AddError<PersonValidationComponent>(c => c.Underage);
}
}
- ValidationComponent
public class PersonValidationComponent : IValidationComponent<Person, PersonValidation>
{
public PersonValidationComponent()
{
NameMustBeSet = new ValidationMessage(() => "Name must be set");
NameIsShort = new ValidationMessage(() => "Name must be at least two characters");
NameIsLong = new ValidationMessage(() => "Name must be not longer twenty characters");
Underage = new ValidationMessage(() => "Age must be greater 18");
}
public ValidationMessage NameMustBeSet { get; set; }
public ValidationMessage NameIsShort { get; set; }
public ValidationMessage NameIsLong { get; set; }
public ValidationMessage Underage { get; set; }
}
- Glue all together
services.AddPhemaValidation(validation => validation.Add<Person, PersonValidation, PersonValidationComponent>());
Using core
var validationContext = new ValidationContext();
// validationContext.When(nameof(model.Name), model.Name)
validationContext.When("key", "value")
.Is(value => true)
.Add(() => new ValidationMessage(() => "template"));
var error = Assert.Single(validationContext.Errors);
Assert.Equal("key", error.Key);
Assert.Equal("template", error.Message);
- This is a lightweight zero-dependent validation core
- You can add to
Is
callback any condition you want or even multiple of them (they will join usingOR
) - You can store validation messages somewhere and pass them to
Add
callback or use extensions - In
Errors
property you will find key (Property name orDataMember
name override) and message (rendered after passing toAdd
callback) - You can use parameterized validation messages adding arguments after in
Add
and placeholders ({0}, {1}) in template
Using extensions
var person = new Person
{
Name = null
};
var validationContext = new ValidationContext();
validationContext.When(person, p => p.Name)
.IsNullOrWhitespace()
.Add(() => new ValidationMessage(() => "Is null or whitespace"));
var error = Assert.Single(validationContext.Errors);
Assert.Equal("Name", error.Key);
Assert.Equal("Is null or whitespace", error.Message);
- You can validate model value using expression like example above, or just passing
T
- You can use predefined conditions like
IsNot
,IsNull
,IsEmpty
, etc. - You can use
Throw
unstead orAdd
to stop execution flow. That will throwValidationConditionException
withError
property - You can ensure that validation context is valid or throw
ValidationContextException
withErrors
property by usingEnsureIsValid
- You can validate that key in not presented in error using
IsValid<T>(t => t.Key)
or just passing string key - You can use typed parameters (up to 2 for now) using
Add<TArgument>(...)
orThrow<TArgument>(...)
Using aspnetcore
public class TestModel
{
public string Name { get; set; }
public int Age { get; set; }
}
public class TestValidation : Validation<TestModel>
{
private readonly TestValidationComponent component;
public TestValidation(TestValidationComponent component)
{
this.component = component;
}
protected override void Validate(IValidationContext validationContext, TestModel model)
{
validationContext.When(model, m => m.Name)
.IsNull()
.Add(() => component.NameIsNull);
validationContext.When(model, m => m.Age)
.IsInRange(0, 17)
.Add(() => component.IsUnderage);
}
}
public class TestValidationComponent : ValidationComponent<TestModel, TestValidation>
{
public TestValidationComponent()
{
NameIsNull = Register(() => "Name is null");
IsUnderage = Register(() => "Is underage");
}
public ValidationMessage NameIsNull { get; }
public ValidationMessage IsUnderage { get; }
}
// Startup
services.AddValidation(
validation =>
validation.Add<TestModel, TestValidation, TestValidationComponent>());
- You have to use
mvc
for validation, because validation uses filters for it - You can inject
IValidationContext
to any part of your application - You can override key by using
[DataMember(Name = "key")]
attribute - You can add
Validation
withoutValidationComponent
orIValidationComponent
withoutValidation
, but i prefer not to do that because of responsibility - If your validation context will be invalid after controller action executed, result will be substituted by validation messages. Try to check validation messages added explicitly by injection of
IValidationContext
before submiting changes
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.2 is compatible. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETCoreApp 2.2
- Microsoft.AspNetCore.Mvc (>= 2.2.0)
- Phema.Validation.Extensions (>= 1.1.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|