Byndyusoft.ValidEnumConverter 1.1.1

dotnet add package Byndyusoft.ValidEnumConverter --version 1.1.1
NuGet\Install-Package Byndyusoft.ValidEnumConverter -Version 1.1.1
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="Byndyusoft.ValidEnumConverter" Version="1.1.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Byndyusoft.ValidEnumConverter --version 1.1.1
#r "nuget: Byndyusoft.ValidEnumConverter, 1.1.1"
#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 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 Nuget Downloads

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:

Package development lifecycle

  • Implement package logic

Maintainers

github.maintain@byndyusoft.com

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .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.

Version Downloads Last updated
1.1.1 484 8/16/2023
1.1.0 1,430 7/26/2021
1.0.1 322 7/16/2021
1.0.0 313 7/16/2021