public static Validation<Version> Validate(Version version)
{
return version.ToOption().Match(
Some: v => IsValidMajorVersion(v.Major).Match(
Valid: _ => IsValidMinorVersion(v.Minor).Match(
Valid: _ => IsValidPatchVersion(v.Minor).Match(
Valid: _ => Valid(v),
Invalid: e => Invalid(e)),
Invalid: e => Invalid(e)),
Invalid: e => Invalid(e)),
None: () => Invalid("Version is not valid"));
}
private static Validation<int> IsValidMajorVersion(int major) =>
major.ToSome().Map(x => x < 0 ? Invalid("Major version must be greater than or equal to 0") : Valid(x)).Value;
private static Validation<Option<int>> IsValidMinorVersion(Option<int> minor) =>
minor.Match(
None: () => Valid(Option<int>.None),
Some: x => x < 0 ? Invalid("Minor version must be greater than or equal to 0") : Valid(Option.Some(x))
);
private static Validation<Option<int>> IsValidPatchVersion(Option<int> patch) =>
patch.Match(
None: () => Valid(Option<int>.None),
Some: x => x < 0 ? Invalid("Patch version must be greater than or equal to 0") : Valid(Option.Some(x))
);