Create a class inheriting from ITypelySpecification
public class TypesSpecification : ITypelySpecification
{
public void Create(ITypelyBuilder builder)
{
builder.OfString().For("FirstName").NotEmpty();
}
}
Usage
var firstName = FirstName.From("Adam");
FirstName.From(""); //Throws ValidationException
if(!FirstName.TryFrom("value", out FirstName instance, out ValidationError? validationError))
{
// Handle error
}
Json Serialization
Serialization using System.Text.Json is supported by default and will only write the underlying value.
ASP.NET Core
To support validation handling and MVC model binding, include Typely.AspNetCore in your projects.
Nuget package
dotnet add package Typely.AspNetCore
Model binding
By default Minimal Apis are supported by implementing a TryParse function for generated types.
Post requests are supported by TypelyJsonConverter.
Other bindings are supported by TypelyTypeConverter.
These are included by default with Typely.Core.
Model state
If you want to add validation errors into the model state of MVC during the binding phase of the request, configure the option below:
Configuration
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers(options => options.UseTypelyModelBinderProvider());
It supports many validation errors without using exceptions.
Validation errors
The following middleware allows you to return neatly structured Json error responses compatible with Microsoft.AspNetCore.Http.HttpValidationProblemDetails.
Configuration
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
// Register this middleware before other middleware components
app.UseTypelyValidationResult();
It works by catching a ValidationException thrown by Typely and returns a list of errors associated to their value object type name as well as the templates used, if you want to modfify the messages in your client application.
Example of response:
{
"templates": {
"ZipCode": [
{
"code": "Matches",
"message": "'{Name}' is not in the correct format. Expected format '{RegularExpression}'.",
"typeName": "ZipCode",
"placeholderValues": {
"RegularExpression": "^((\\d{5}-\\d{4})|(\\d{5})|([A-Z|a-z]\\d[A-Z|a-z]\\d[A-Z|a-z]\\d))$",
"Name": "ZipCode",
"ActualLength": "7"
}
}
]
},
"errors": {
"ZipCode": [
"'ZipCode' is not in the correct format. Expected format '^((\\d{5}-\\d{4})|(\\d{5})|([A-Z|a-z]\\d[A-Z|a-z]\\d[A-Z|a-z]\\d))$'."
]
},
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400
}
OpenAPI
To add support for OpenAPI specs and Swagger UI, include Typely.AspNetCore.Swashbuckle in your projects.
Nuget package
dotnet add package Typely.AspNetCore.Swashbuckle
Configuration
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSwaggerGen(options => options.UseTypelySchemaFilter());
Entity Framework Core
To use your value objects with EF Core, include Typely.EfCore in your projects.
Nuget package
dotnet add package Typely.EfCore
Configuration
Apply Typely conventions to your DbContext to automatically configure the database. By default, the conventions will tell EF Core how to save and load your value objects and set the maximum data length.
If you don't use conventions, you can configure your types manually. Typely will generate a MaxLength property when using a validation that sets the maximum length.