Byndyusoft.ValidEnumConverter
1.1.1
dotnet add package Byndyusoft.ValidEnumConverter --version 1.1.1
NuGet\Install-Package Byndyusoft.ValidEnumConverter -Version 1.1.1
<PackageReference Include="Byndyusoft.ValidEnumConverter" Version="1.1.1" />
paket add Byndyusoft.ValidEnumConverter --version 1.1.1
#r "nuget: Byndyusoft.ValidEnumConverter, 1.1.1"
// Install Byndyusoft.ValidEnumConverter as a Cake Addin #addin nuget:?package=Byndyusoft.ValidEnumConverter&version=1.1.1 // Install Byndyusoft.ValidEnumConverter as a Cake Tool #tool nuget:?package=Byndyusoft.ValidEnumConverter&version=1.1.1
Byndyusoft.ValidEnumConverter
Custom JsonConverter for enums with value validation.
Byndyusoft.ValidEnumConverter |
Installing
dotnet add package Byndyusoft.ValidEnumConverter
Usage
Byndyusoft.ValidEnumConverter designed to properly validate numbers in a string. The System.Text.Json.Serialization library does not correctly validate numbers in a string like "Enum" For example: We have our own enum, let's call it UserRole:
public enum UserRole
{
Administrator = 0,
Agent = 1
}
And there is a Dto object:
public UserInfoDto
{
public int Id {get; set; }
public UserRole Role {get; set; }
}
When using this Dto object in our controller as an accepted class On the front side, the following Json can be sent:
{
"Id": 1,
"UserRole": "589"
}
The non-existent value of our UserRole, it is logical to assume that a regular validator should throw an error with a code of 400, but this does not happen. Therefore, we decided to make our own custom converter. Which validates the numbers in the string correctly
how to use the converter We go to the Setup.cs of our project:
services.AddControllers ()
.AddJsonOptions (options => ...
and add following line
options.JsonSerializerOptions.Converters.AddValidEnumConverter();
After that, all Enum types are correctly converted for you.
Contributing
To contribute, you will need to setup your local environment, see prerequisites. For the contribution and workflow guide, see package development lifecycle.
A detailed overview on how to contribute can be found in the contributing guide.
Prerequisites
Make sure you have installed all of the following prerequisites on your development machine:
- Git - Download & Install Git. OSX and Linux machines typically have this already installed.
- .NET Core (version 3.1 or higher) - Download & Install .NET Core.
Package development lifecycle
- Implement package logic
Maintainers
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 | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.