Skip to content

Commit

Permalink
- fix validators
Browse files Browse the repository at this point in the history
- use `TokenSchedule.FluentValidation` in `TokenSchedule`
  • Loading branch information
ArdenHide committed Dec 25, 2024
1 parent 4f1adb2 commit 132a199
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 31 deletions.
2 changes: 2 additions & 0 deletions src/TokenSchedule.FluentValidation/ScheduleItemValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ 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.FinishDate.HasValue || item.StartDate < item.FinishDate.Value)
Expand Down
6 changes: 4 additions & 2 deletions src/TokenSchedule.FluentValidation/ScheduleValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ public class ScheduleValidator : AbstractValidator<IEnumerable<IValidatedSchedul
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)
.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))
.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()));
}
Expand Down
16 changes: 6 additions & 10 deletions src/TokenSchedule/ScheduleItem.cs
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);
}
}
}
22 changes: 3 additions & 19 deletions src/TokenSchedule/ScheduleManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Linq;
using FluentValidation;
using System.Collections.Generic;
using TokenSchedule.FluentValidation;

namespace TokenSchedule
{
Expand All @@ -17,25 +19,7 @@ public ScheduleManager(IEnumerable<ScheduleItem> schedule) : this(schedule.ToArr

public ScheduleManager(params ScheduleItem[] schedule)
{
if (schedule == null)
{
throw new ArgumentNullException(nameof(schedule));
}

if (!schedule.Any())
{
throw new ArgumentException("Schedule must contain 1 or more elements.", nameof(schedule));
}

if (schedule.Sum(x => x.Ratio) != 1)
{
throw new ArgumentException("The sum of the ratios must be 1.", nameof(schedule));
}

if (schedule[0].StartDate != schedule.Min(x => x.StartDate))
{
throw new ArgumentException("The first element must be the TGE (Token Generation Event).", nameof(schedule));
}
new ScheduleValidator().ValidateAndThrow(schedule);

Schedule = schedule;
TGE = schedule[0];
Expand Down
4 changes: 4 additions & 0 deletions src/TokenSchedule/TokenSchedule.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@
</None>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TokenSchedule.FluentValidation\TokenSchedule.FluentValidation.csproj" />
</ItemGroup>

</Project>

0 comments on commit 132a199

Please sign in to comment.