ALSI.PrimitiveValidation is a lightweight C# library for primitive data validation.
Add ALSI.PrimitiveValidation to your project by adding it as a NuGet package:
dotnet add package ALSI.PrimitiveValidation
This library provides a set of attributes to validate different primitive conditions:
IsBase64Attribute
: Ensures that a property, field, or parameter is a valid Base64-encoded string.PropertyNotNullAttribute
: Ensures that a property, field, or parameter is initialized with a non-null value.StringNotNullOrEmptyAttribute
: Ensures that a property, field, or parameter is not null and not an empty string.
These attributes can be used to decorate model properties or record parameters, enforcing validation automatically via the .NET data annotations framework.
Using this model:
using ALSI.PrimitiveValidation;
public record MyModel(
[property: IsBase64] string EncodedData,
[property: PropertyNotNull] object ImportantProperty,
[property: StringNotNullOrempty] string Name);
We can validate manually, like so:
var model = new MyModel("SGVsbG8sIHdvcmxkIQ==", new (), "Alice");
Validator.ValidateObject(
model,
new ValidationContext(model),
validateAllProperties: true
);