-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create
TokenSchedule.FluentValidation
package (#9)
* - initial create `TokenSchedule.FluentValidation` project * - fix validators - use `TokenSchedule.FluentValidation` in `TokenSchedule` * - add `.props` * - fix exist tests - write validator tests * - use `When` * - update `README.md` of `TokenSchedule.FluentValidation` project * - setting README files * - update main readme
- Loading branch information
Showing
17 changed files
with
455 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<Nullable>enable</Nullable> | ||
<Version>1.1.0</Version> | ||
<Authors>ArdenHide, Lomet</Authors> | ||
<Company>The-Poolz</Company> | ||
<RepositoryType>git</RepositoryType> | ||
<RepositoryUrl>https://github.com/The-Poolz/TokenSchedule</RepositoryUrl> | ||
<PackageReleaseNotes>https://github.com/The-Poolz/TokenSchedule/releases/tag/v1.1.0</PackageReleaseNotes> | ||
<PackageLicenseFile>LICENSE</PackageLicenseFile> | ||
<IncludeSymbols>True</IncludeSymbols> | ||
<SymbolPackageFormat>snupkg</SymbolPackageFormat> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\..\LICENSE"> | ||
<Pack>True</Pack> | ||
<PackagePath>\</PackagePath> | ||
</None> | ||
</ItemGroup> | ||
</Project> |
11 changes: 11 additions & 0 deletions
11
src/TokenSchedule.FluentValidation/Models/IValidatedScheduleItem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
|
||
namespace TokenSchedule.FluentValidation.Models | ||
{ | ||
public interface IValidatedScheduleItem | ||
{ | ||
public decimal Ratio { get; } | ||
public DateTime StartDate { get; } | ||
public DateTime? FinishDate { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# TokenSchedule.FluentValidation | ||
|
||
**TokenSchedule.FluentValidation** is a small library that provides validators for schedule items used in token distribution. | ||
It is built on top of [FluentValidation](https://github.com/FluentValidation/FluentValidation) to handle validation dependencies separately. | ||
|
||
## Features | ||
|
||
- **`ScheduleItemValidator`**: Validates a single schedule item (`IValidatedScheduleItem`): | ||
- Ensures `Ratio` is positive (`> 0`). | ||
- Validates that `FinishDate` is either not set or later than `StartDate`. | ||
- **`ScheduleValidator`**: Validates an entire schedule (`IEnumerable<IValidatedScheduleItem>`): | ||
- Schedule cannot be null or empty. | ||
- The sum of all `Ratio` values must be `1.0`. | ||
- The first item in the schedule must have the earliest `StartDate` (treated as the Token Generation Event). | ||
- Each individual item is validated by the `ScheduleItemValidator`. | ||
|
||
## Installation | ||
|
||
You can install this package via the .NET CLI: | ||
|
||
```powershell | ||
dotnet add package TokenSchedule.FluentValidation | ||
``` | ||
|
||
Or via the NuGet Package Manager console: | ||
|
||
```powershell | ||
Install-Package TokenSchedule.FluentValidation | ||
``` | ||
|
||
## Getting Started | ||
|
||
Below is a simple example showing how to use `TokenSchedule.FluentValidation`: | ||
|
||
```csharp | ||
using System; | ||
using System.Collections.Generic; | ||
using FluentValidation; | ||
using TokenSchedule.FluentValidation; | ||
using TokenSchedule.FluentValidation.Models; | ||
|
||
public class MyScheduleItem : IValidatedScheduleItem | ||
{ | ||
public decimal Ratio { get; set; } | ||
public DateTime StartDate { get; set; } | ||
public DateTime? FinishDate { get; set; } | ||
} | ||
|
||
class Program | ||
{ | ||
static void Main() | ||
{ | ||
// Create some schedule items | ||
var scheduleItems = new List<IValidatedScheduleItem> | ||
{ | ||
new MyScheduleItem | ||
{ | ||
Ratio = 0.5m, | ||
StartDate = new DateTime(2024, 1, 1), | ||
FinishDate = new DateTime(2024, 6, 1) | ||
}, | ||
new MyScheduleItem | ||
{ | ||
Ratio = 0.5m, | ||
StartDate = new DateTime(2024, 6, 2), | ||
FinishDate = new DateTime(2024, 12, 31) | ||
} | ||
}; | ||
|
||
// Create an instance of the ScheduleValidator | ||
var validator = new ScheduleValidator(); | ||
|
||
// Validate the schedule | ||
var result = validator.Validate(scheduleItems); | ||
|
||
if (result.IsValid) | ||
{ | ||
Console.WriteLine("Schedule is valid!"); | ||
} | ||
else | ||
{ | ||
Console.WriteLine("Schedule is invalid. Errors:"); | ||
foreach (var error in result.Errors) | ||
{ | ||
Console.WriteLine($"- {error.ErrorMessage}"); | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### Explanation | ||
1. **Define your schedule items** by implementing the `IValidatedScheduleItem` interface. | ||
2. **Create a list of schedule items** that you want to validate. | ||
3. **Use the `ScheduleValidator`** to validate the entire collection. | ||
4. **Handle the validation results**, e.g. by displaying errors or by throwing exceptions. | ||
|
||
## License | ||
|
||
This project is licensed under the MIT License - see the LICENSE file for details. |
20 changes: 20 additions & 0 deletions
20
src/TokenSchedule.FluentValidation/ScheduleItemValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using FluentValidation; | ||
using TokenSchedule.FluentValidation.Models; | ||
|
||
namespace TokenSchedule.FluentValidation | ||
{ | ||
public class ScheduleItemValidator : AbstractValidator<IValidatedScheduleItem> | ||
{ | ||
public ScheduleItemValidator() | ||
{ | ||
RuleFor(item => item) | ||
.Cascade(CascadeMode.Stop) | ||
.NotNull() | ||
.Must(item => item.Ratio > 0) | ||
.WithMessage("Ratio must be positive.") | ||
.Must(item => item.StartDate < item.FinishDate!.Value) | ||
.When(item => item.FinishDate.HasValue) | ||
.WithMessage("End time must be greater than start time."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using System.Linq; | ||
using FluentValidation; | ||
using System.Collections.Generic; | ||
using TokenSchedule.FluentValidation.Models; | ||
|
||
namespace TokenSchedule.FluentValidation | ||
{ | ||
public class ScheduleValidator : AbstractValidator<IEnumerable<IValidatedScheduleItem>> | ||
{ | ||
public ScheduleValidator() | ||
{ | ||
RuleFor(schedule => schedule.ToArray()) | ||
.Cascade(CascadeMode.Stop) | ||
.NotNull() | ||
.NotEmpty() | ||
.WithMessage("Schedule must contain 1 or more elements.") | ||
.Must(schedule => schedule.Sum(item => item.Ratio) == 1.0m) | ||
.WithMessage("The sum of the ratios must be 1.") | ||
.Must(schedule => schedule[0].StartDate == schedule.Min(x => x.StartDate)) | ||
.WithMessage("The first element must be the TGE (Token Generation Event).") | ||
.ForEach(item => item.SetValidator(new ScheduleItemValidator())); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/TokenSchedule.FluentValidation/TokenSchedule.FluentValidation.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Import Project="..\..\buildConf\.props" /> | ||
<PropertyGroup> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<PackageId>TokenSchedule.FluentValidation</PackageId> | ||
<Product>TokenSchedule.FluentValidation</Product> | ||
<PackageReadmeFile>README.md</PackageReadmeFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentValidation" Version="11.11.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Update="README.md"> | ||
<Pack>True</Pack> | ||
<PackagePath>\</PackagePath> | ||
</None> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,23 @@ | ||
using System; | ||
using FluentValidation; | ||
using TokenSchedule.FluentValidation; | ||
using TokenSchedule.FluentValidation.Models; | ||
|
||
namespace TokenSchedule | ||
{ | ||
public class ScheduleItem | ||
public class ScheduleItem : IValidatedScheduleItem | ||
{ | ||
public decimal Ratio { get; } | ||
public DateTime StartDate { get; } | ||
public DateTime? FinishDate { get; } | ||
|
||
public ScheduleItem(decimal ratio, DateTime startDate, DateTime? finishDate = null) | ||
{ | ||
if (finishDate.HasValue && startDate >= finishDate.Value) | ||
{ | ||
throw new ArgumentException("End time must be greater than start time.", nameof(startDate)); | ||
} | ||
if (ratio <= 0) | ||
{ | ||
throw new ArgumentException("Ratio must be positive.", nameof(ratio)); | ||
} | ||
|
||
Ratio = ratio; | ||
StartDate = startDate; | ||
FinishDate = finishDate; | ||
|
||
new ScheduleItemValidator().ValidateAndThrow(this); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<Import Project="..\..\buildConf\.props" /> | ||
<PropertyGroup> | ||
<TargetFramework>netstandard2.1</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<PackageId>TokenSchedule</PackageId> | ||
<Version>1.0.1</Version> | ||
<Authors>Lomet, ArdenHide</Authors> | ||
<Company>The-Poolz</Company> | ||
<Product>TokenSchedule</Product> | ||
<PackageReadmeFile>README.md</PackageReadmeFile> | ||
<RepositoryType>git</RepositoryType> | ||
<RepositoryUrl>https://github.com/The-Poolz/TokenSchedule</RepositoryUrl> | ||
<PackageLicenseFile>LICENSE</PackageLicenseFile> | ||
<IncludeSymbols>True</IncludeSymbols> | ||
<SymbolPackageFormat>snupkg</SymbolPackageFormat> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\..\LICENSE"> | ||
<Pack>True</Pack> | ||
<PackagePath>\</PackagePath> | ||
</None> | ||
<None Include="..\..\README.md"> | ||
<Pack>True</Pack> | ||
<PackagePath>\</PackagePath> | ||
</None> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\TokenSchedule.FluentValidation\TokenSchedule.FluentValidation.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.