expressappframework-404223-backend-web-api-service-validate-data-sent-to-web-api-endpoints.md
This topic shows how you can validate user input if you use DevExpress Web API Service for data access.
The following technologies enable data validation:
XPO | EF CoreThese ORM tools allow you to define your Business Model together with validation attributes.XAF Validation ModuleAllows you to apply 10+ predefined rules or any number of custom validation rules to your data objects. Enforces those rules in XAF applications.
Even if you don’t use predefined validation attributes in your data model, you can still use DevExpress Web API Service to validate user input. You can add custom data validation logic if you extend the basic implementation described in this article.
Note
This option of our Web API Service ships as part of the DevExpress Universal Subscription.
If you use DevExpress Web API Service endpoints to manage data and need to enable validation, use a specially designed IValidator service available in the following namespace: DevExpress.Persistent.Validation.
You can use the following methods to enable the service.
If you use the Template Kit to create your Backend Web API project, you can enable the Validation module the the Additional Modules section. For additional information, refer to the following topic: Create a Standalone Web API Application.
Install the DevExpress.ExpressApp.Validation.Blazor NuGet package.
Register the Validation module and required services in Startup.cs. Use the Web API or XAF Application builder:
File: MySolution.WebApi\Startup.cs (MySolution.Blazor.Server\Startup.cs)
services.AddXafWebApi(builder => {
//..
builder.Modules
.AddValidation(options => {
//...
})
//...
}, Configuration);
services.AddXaf(Configuration, builder => {
//..
builder.Modules
.AddValidation(options => {
//...
})
//...
}
For performance optimization, CRUD endpoints don’t initiate data validation: you need to add that functionality.
In a most basic scenario, you access your data using CRUD endpoints. When a user changes data, you enforce specified validation attributes in your data model.
The following example shows how you can enable such functionality. The code implements a custom IDataService. This service runs validation before it commits an object space (IObjectSpace).
For details about custom data services, please see the following article: Execute Custom Operations on Endpoint Requests.
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Core;
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.WebApi.Services;
using DevExpress.Persistent.Validation;
// ...
public class CustomDataService : DataService {
readonly IValidator validator;
public CustomDataService(IObjectSpaceFactory objectSpaceFactory,
ITypesInfo typesInfo, IValidator validator)
: base(objectSpaceFactory, typesInfo) {
this.validator = validator;
}
protected override IObjectSpace CreateObjectSpace(Type objectType) {
IObjectSpace objectSpace = base.CreateObjectSpace(objectType);
objectSpace.Committing += ObjectSpace_Committing;
return objectSpace;
}
private void ObjectSpace_Committing(object? sender,
System.ComponentModel.CancelEventArgs e) {
IObjectSpace os = (IObjectSpace)sender!;
var validationResult = validator.RuleSet.ValidateAllTargets(
os, os.ModifiedObjects, DefaultContexts.Save
);
if(validationResult.ValidationOutcome == ValidationOutcome.Error) {
throw new ValidationException(validationResult);
}
}
}
You can use the HttpRequestMessage API to change the request locale. The invalid validation results you obtain from the service will use the locale you specified.
// ...
httpClient.DefaultRequestHeaders.Add("Accept-Language", "de-DE");
// ...
This example checks validation rules for XAF’s standard ApplicationUser class. XAF declares this class in all new projects that use the Security feature.
The ApplicationUser class applies a RuleRequiredField attribute to the UserName property. In other words, you cannot create a new user with an empty name.
The test tries to create an ApplicationUser object with an empty UserName property and receives a validation error.
This example uses the IDataService implementation demonstrated above. You can find the same test included in our MainDemo.EFCore demo application.
Follow the steps below to add a test to your solution:
{SolutionName}.Blazor.Server project.Microsoft.AspNetCore.Mvc.Testing package reference.See Also
Consume the DevExpress Backend Web API from JavaScript with Svelte (Part 4. Edit and Validate Data)