Skip to content

Commit

Permalink
Add OcpiValidator
Browse files Browse the repository at this point in the history
These validators will use a specific OCPI version to validate it's entities
  • Loading branch information
YuriyDurov committed Sep 19, 2023
1 parent 6b7129c commit 9848462
Show file tree
Hide file tree
Showing 24 changed files with 100 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace OCPI.Contracts;

internal partial class OcpiAdditionalGeolocationValidator : ActionValidator<OcpiAdditionalGeolocation>
internal partial class OcpiAdditionalGeolocationValidator : OcpiValidator<OcpiAdditionalGeolocation>
{
public OcpiAdditionalGeolocationValidator(ActionType actionType) : base(actionType)
public OcpiAdditionalGeolocationValidator(ActionType actionType, OcpiVersion ocpiVersion) : base(actionType, ocpiVersion)
{
JsonRuleFor(x => x.Latitude)
.NotEmpty()
Expand All @@ -16,7 +16,7 @@ public OcpiAdditionalGeolocationValidator(ActionType actionType) : base(actionTy
.ValidLongitude();

JsonRuleFor(x => x.Name!)
.SetValidator(new OcpiDisplayTextValidator(actionType))
.SetValidator(new OcpiDisplayTextValidator(actionType, ocpiVersion))
.When(x => x.Name is not null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace OCPI.Contracts;

internal class OcpiBusinessDetailsValidator : ActionValidator<OcpiBusinessDetails>
internal class OcpiBusinessDetailsValidator : OcpiValidator<OcpiBusinessDetails>
{
public OcpiBusinessDetailsValidator(ActionType actionType) : base(actionType)
public OcpiBusinessDetailsValidator(ActionType actionType, OcpiVersion ocpiVersion) : base(actionType, ocpiVersion)
{
JsonRuleFor(x => x.Name)
.NotEmpty()
Expand All @@ -15,6 +15,6 @@ public OcpiBusinessDetailsValidator(ActionType actionType) : base(actionType)
.ValidUrl();

JsonRuleFor(x => x.Logo!)
.SetValidator(new OcpiImageValidator(actionType));
.SetValidator(new OcpiImageValidator(actionType, ocpiVersion));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace OCPI.Contracts;

internal class OcpiDisplayTextValidator : ActionValidator<OcpiDisplayText>
internal class OcpiDisplayTextValidator : OcpiValidator<OcpiDisplayText>
{
public OcpiDisplayTextValidator(ActionType actionType) : base(actionType)
public OcpiDisplayTextValidator(ActionType actionType, OcpiVersion ocpiVersion) : base(actionType, ocpiVersion)
{
JsonRuleFor(x => x.Language)
.NotEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace OCPI.Contracts;

internal partial class OcpiGeolocationValidator : ActionValidator<OcpiGeolocation>
internal partial class OcpiGeolocationValidator : OcpiValidator<OcpiGeolocation>
{
public OcpiGeolocationValidator(ActionType actionType) : base(actionType)
public OcpiGeolocationValidator(ActionType actionType, OcpiVersion ocpiVersion) : base(actionType, ocpiVersion)
{
JsonRuleFor(x => x.Latitude)
.NotEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace OCPI.Contracts;

internal class OcpiImageValidator : ActionValidator<OcpiImage>
internal class OcpiImageValidator : OcpiValidator<OcpiImage>
{
public OcpiImageValidator(ActionType actionType) : base(actionType)
public OcpiImageValidator(ActionType actionType, OcpiVersion ocpiVersion) : base(actionType, ocpiVersion)
{
JsonRuleFor(x => x.Url)
.NotEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace OCPI.Contracts;

internal class OcpiPriceValidator : ActionValidator<OcpiPrice>
internal class OcpiPriceValidator : OcpiValidator<OcpiPrice>
{
public OcpiPriceValidator(ActionType actionType) : base(actionType)
public OcpiPriceValidator(ActionType actionType, OcpiVersion ocpiVersion) : base(actionType, ocpiVersion)
{
JsonRuleFor(x => x.ExclVat)
.NotEmpty();
Expand Down
23 changes: 23 additions & 0 deletions src/OCPI.Net.Validation/Validators/Base/OcpiValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using FluentValidation;

namespace OCPI.Contracts;

internal abstract class OcpiValidator<T> : ActionValidator<T>
{
protected readonly OcpiVersion OcpiVersion;

protected OcpiValidator(ActionType actionType, OcpiVersion ocpiVersion) : base(actionType)
{
OcpiVersion = ocpiVersion;
}

public IConditionBuilder When(OcpiVersion ocpiVersion, Action action)
{
return When((T x) => OcpiVersion == ocpiVersion, action);
}

public IConditionBuilder Unless(OcpiVersion ocpiVersion, Action action)
{
return Unless((T x) => OcpiVersion == ocpiVersion, action);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace OCPI.Contracts;

internal class OcpiCredentialsRoleValidator : ActionValidator<OcpiCredentialsRole>
internal class OcpiCredentialsRoleValidator : OcpiValidator<OcpiCredentialsRole>
{
public OcpiCredentialsRoleValidator(ActionType actionType) : base(actionType)
public OcpiCredentialsRoleValidator(ActionType actionType, OcpiVersion ocpiVersion) : base(actionType, ocpiVersion)
{
JsonRuleFor(x => x.CountryCode)
.NotEmpty()
Expand All @@ -23,6 +23,6 @@ public OcpiCredentialsRoleValidator(ActionType actionType) : base(actionType)
.NotEmpty();

JsonRuleFor(x => x.BusinessDetails!)
.SetValidator(new OcpiBusinessDetailsValidator(actionType));
.SetValidator(new OcpiBusinessDetailsValidator(actionType, ocpiVersion));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace OCPI.Validation;

internal class OcpiCredentialsValidator : ActionValidator<OcpiCredentials>
internal class OcpiCredentialsValidator : OcpiValidator<OcpiCredentials>
{
public OcpiCredentialsValidator(ActionType actionType) : base(actionType)
public OcpiCredentialsValidator(ActionType actionType, OcpiVersion ocpiVersion) : base(actionType, ocpiVersion)
{
JsonRuleFor(x => x.Token)
.NotEmpty()
Expand All @@ -22,6 +22,6 @@ public OcpiCredentialsValidator(ActionType actionType) : base(actionType)
.NotEmpty();

RuleForEach(x => x.Roles!)
.SetValidator(new OcpiCredentialsRoleValidator(actionType));
.SetValidator(new OcpiCredentialsRoleValidator(actionType, ocpiVersion));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

namespace OCPI.Contracts;

internal class OcpiEnergyMixValidator : ActionValidator<OcpiEnergyMix>
internal class OcpiEnergyMixValidator : OcpiValidator<OcpiEnergyMix>
{
public OcpiEnergyMixValidator(ActionType actionType) : base(actionType)
public OcpiEnergyMixValidator(ActionType actionType, OcpiVersion ocpiVersion) : base(actionType, ocpiVersion)
{
JsonRuleFor(x => x.IsGreenEnergy)
.NotEmpty();

RuleForEach(x => x.EnergySources)
.SetValidator(new OcpiEnergySourceValidator(actionType));
.SetValidator(new OcpiEnergySourceValidator(actionType, ocpiVersion));

RuleForEach(x => x.EnvironmentalImpact)
.SetValidator(new OcpiEnvironmentalImpactValidator(actionType));
.SetValidator(new OcpiEnvironmentalImpactValidator(actionType, ocpiVersion));

JsonRuleFor(x => x.SupplierName)
.MaximumLength(64);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace OCPI.Contracts;

internal class OcpiEnergySourceValidator : ActionValidator<OcpiEnergySource>
internal class OcpiEnergySourceValidator : OcpiValidator<OcpiEnergySource>
{
public OcpiEnergySourceValidator(ActionType actionType) : base(actionType)
public OcpiEnergySourceValidator(ActionType actionType, OcpiVersion ocpiVersion) : base(actionType, ocpiVersion)
{
JsonRuleFor(x => x.Source)
.NotEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace OCPI.Contracts;

internal class OcpiEnvironmentalImpactValidator : ActionValidator<OcpiEnvironmentalImpact>
internal class OcpiEnvironmentalImpactValidator : OcpiValidator<OcpiEnvironmentalImpact>
{
public OcpiEnvironmentalImpactValidator(ActionType actionType) : base(actionType)
public OcpiEnvironmentalImpactValidator(ActionType actionType, OcpiVersion ocpiVersion) : base(actionType, ocpiVersion)
{
JsonRuleFor(x => x.Category)
.NotEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace OCPI.Contracts;

internal class OcpiConnectorValidator : ActionValidator<OcpiConnector>
internal class OcpiConnectorValidator : OcpiValidator<OcpiConnector>
{
public OcpiConnectorValidator(ActionType actionType) : base(actionType)
public OcpiConnectorValidator(ActionType actionType, OcpiVersion ocpiVersion) : base(actionType, ocpiVersion)
{
Unless(ActionType.Patch, () =>
{
Expand Down
12 changes: 6 additions & 6 deletions src/OCPI.Net.Validation/Validators/Location/OcpiEvseValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace OCPI.Contracts;

internal class OcpiEvseValidator : ActionValidator<OcpiEvse>
internal class OcpiEvseValidator : OcpiValidator<OcpiEvse>
{
public OcpiEvseValidator(ActionType actionType) : base(actionType)
public OcpiEvseValidator(ActionType actionType, OcpiVersion ocpiVersion) : base(actionType, ocpiVersion)
{
Unless(ActionType.Patch, () =>
{
Expand All @@ -26,7 +26,7 @@ public OcpiEvseValidator(ActionType actionType) : base(actionType)
.ValidEnum();

RuleForEach(x => x.StatusSchedule)
.SetValidator(new OcpiStatusScheduleValidator(actionType));
.SetValidator(new OcpiStatusScheduleValidator(actionType, ocpiVersion));

RuleForEach(x => x.Capabilities)
.ValidEnum();
Expand All @@ -35,18 +35,18 @@ public OcpiEvseValidator(ActionType actionType) : base(actionType)
.MaximumLength(4);

JsonRuleFor(x => x.Coordinates!)
.SetValidator(new OcpiGeolocationValidator(actionType));
.SetValidator(new OcpiGeolocationValidator(actionType, ocpiVersion));

JsonRuleFor(x => x.PhysicalReference);

RuleForEach(x => x.Directions)
.SetValidator(new OcpiDisplayTextValidator(actionType));
.SetValidator(new OcpiDisplayTextValidator(actionType, ocpiVersion));

RuleForEach(x => x.ParkingRestrictions)
.ValidEnum();

RuleForEach(x => x.Images)
.SetValidator(new OcpiImageValidator(actionType));
.SetValidator(new OcpiImageValidator(actionType, ocpiVersion));

JsonRuleFor(x => x.LastUpdated)
.NotEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace OCPI.Contracts;

internal partial class OcpiExceptionalPeriodValidator : ActionValidator<OcpiExceptionalPeriod>
internal partial class OcpiExceptionalPeriodValidator : OcpiValidator<OcpiExceptionalPeriod>
{
public OcpiExceptionalPeriodValidator(ActionType actionType) : base(actionType)
public OcpiExceptionalPeriodValidator(ActionType actionType, OcpiVersion ocpiVersion) : base(actionType, ocpiVersion)
{
JsonRuleFor(x => x.PeriodBegin)
.NotEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

namespace OCPI.Contracts;

internal partial class OcpiHoursValidator : ActionValidator<OcpiHours>
internal partial class OcpiHoursValidator : OcpiValidator<OcpiHours>
{
public OcpiHoursValidator(ActionType actionType) : base(actionType)
public OcpiHoursValidator(ActionType actionType, OcpiVersion ocpiVersion) : base(actionType, ocpiVersion)
{
JsonRuleFor(x => x.TwentyFourSeven)
.NotEmpty();

RuleForEach(x => x.RegularHours)
.SetValidator(new OcpiRegularHoursValidator(actionType));
.SetValidator(new OcpiRegularHoursValidator(actionType, ocpiVersion));

RuleForEach(x => x.ExceptionalOpenings)
.SetValidator(new OcpiExceptionalPeriodValidator(actionType));
.SetValidator(new OcpiExceptionalPeriodValidator(actionType, ocpiVersion));

RuleForEach(x => x.ExceptionalClosings)
.SetValidator(new OcpiExceptionalPeriodValidator(actionType));
.SetValidator(new OcpiExceptionalPeriodValidator(actionType, ocpiVersion));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace OCPI.Contracts;

internal partial class OcpiLocationValidator : ActionValidator<OcpiLocation>
internal partial class OcpiLocationValidator : OcpiValidator<OcpiLocation>
{
public OcpiLocationValidator(ActionType actionType) : base(actionType)
public OcpiLocationValidator(ActionType actionType, OcpiVersion ocpiVersion) : base(actionType, ocpiVersion)
{
Unless(ActionType.Patch, () =>
{
Expand All @@ -30,7 +30,7 @@ public OcpiLocationValidator(ActionType actionType) : base(actionType)
.MaximumLength(36);

RuleForEach(x => x.PublishAllowedTo)
.SetValidator(new OcpiPublishTokenTypeValidator(actionType));
.SetValidator(new OcpiPublishTokenTypeValidator(actionType, ocpiVersion));

JsonRuleFor(x => x.Name)
.MaximumLength(255);
Expand All @@ -51,10 +51,10 @@ public OcpiLocationValidator(ActionType actionType) : base(actionType)
.MaximumLength(3);

JsonRuleFor(x => x.Coordinates!)
.SetValidator(new OcpiGeolocationValidator(actionType));
.SetValidator(new OcpiGeolocationValidator(actionType, ocpiVersion));

RuleForEach(x => x.RelatedLocations)
.SetValidator(new OcpiAdditionalGeolocationValidator(actionType));
.SetValidator(new OcpiAdditionalGeolocationValidator(actionType, ocpiVersion));

// TODO: Specific to OCPI 2.1.1
//JsonRuleFor(x => x.Type)
Expand All @@ -64,19 +64,19 @@ public OcpiLocationValidator(ActionType actionType) : base(actionType)
.ValidEnum();

RuleForEach(x => x.Evses)
.SetValidator(new OcpiEvseValidator(actionType));
.SetValidator(new OcpiEvseValidator(actionType, ocpiVersion));

RuleForEach(x => x.Directions)
.SetValidator(new OcpiDisplayTextValidator(actionType));
.SetValidator(new OcpiDisplayTextValidator(actionType, ocpiVersion));

JsonRuleFor(x => x.Operator!)
.SetValidator(new OcpiBusinessDetailsValidator(actionType));
.SetValidator(new OcpiBusinessDetailsValidator(actionType, ocpiVersion));

JsonRuleFor(x => x.Suboperator!)
.SetValidator(new OcpiBusinessDetailsValidator(actionType));
.SetValidator(new OcpiBusinessDetailsValidator(actionType, ocpiVersion));

JsonRuleFor(x => x.Owner!)
.SetValidator(new OcpiBusinessDetailsValidator(actionType));
.SetValidator(new OcpiBusinessDetailsValidator(actionType, ocpiVersion));

RuleForEach(x => x.Facilities)
.ValidEnum();
Expand All @@ -85,13 +85,13 @@ public OcpiLocationValidator(ActionType actionType) : base(actionType)
.MaximumLength(255);

JsonRuleFor(x => x.OpeningTimes!)
.SetValidator(new OcpiHoursValidator(actionType));
.SetValidator(new OcpiHoursValidator(actionType, ocpiVersion));

RuleForEach(x => x.Images)
.SetValidator(new OcpiImageValidator(actionType));
.SetValidator(new OcpiImageValidator(actionType, ocpiVersion));

JsonRuleFor(x => x.EnergyMix!)
.SetValidator(new OcpiEnergyMixValidator(actionType));
.SetValidator(new OcpiEnergyMixValidator(actionType, ocpiVersion));

JsonRuleFor(x => x.LastUpdated)
.NotEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace OCPI.Contracts;

internal partial class OcpiPublishTokenTypeValidator : ActionValidator<OcpiPublishTokenType>
internal partial class OcpiPublishTokenTypeValidator : OcpiValidator<OcpiPublishTokenType>
{
public OcpiPublishTokenTypeValidator(ActionType actionType) : base(actionType)
public OcpiPublishTokenTypeValidator(ActionType actionType, OcpiVersion ocpiVersion) : base(actionType, ocpiVersion)
{
JsonRuleFor(x => x.Uid)
.MaximumLength(36);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

namespace OCPI.Contracts;

internal partial class OcpiRegularHoursValidator : ActionValidator<OcpiRegularHours>
internal partial class OcpiRegularHoursValidator : OcpiValidator<OcpiRegularHours>
{
private const string _timePeriodRegexValue = "([0-1][0-9]|2[0-3]):[0-5][0-9]";

[GeneratedRegex(_timePeriodRegexValue)]
private static partial Regex TimePeriodRegex();

public OcpiRegularHoursValidator(ActionType actionType) : base(actionType)
public OcpiRegularHoursValidator(ActionType actionType, OcpiVersion ocpiVersion) : base(actionType, ocpiVersion)
{
JsonRuleFor(x => x.Weekday)
.NotEmpty()
Expand Down
Loading

0 comments on commit 9848462

Please sign in to comment.