Meigs2
Meigs2
CC#
Created by Meigs2 on 9/18/2022 in #help
Composing a validation function using functional concepts
public static Validation<Version> Validate(Version version)
{
return version.ToOption()
.Match(
Some: v =>
IsValidMajorVersion(v)
.Bind(IsValidMinorVersion)
.Bind(IsValidPatchVersion),
None: () => Invalid("Version cannot be null"));
}

private static Validation<Version> IsValidMajorVersion(Version version) => version.Major >= 0
? Valid(version)
: Invalid<Version>($"Major version must be greater than or equal to 0, but was {version.Major}");

private static Validation<Version> IsValidMinorVersion(Version minor) => minor.Minor.Match(
Some: m => m >= 0
? Valid(minor)
: Invalid<Version>($"Minor version must be greater than or equal to 0, but was {m}"),
None: () => Valid(minor));

private static Validation<Version> IsValidPatchVersion(Version patch) =>
patch.Patch.Match(
Some: p => p >= 0
? Valid(patch)
: Invalid<Version>($"Patch version must be greater than or equal to 0, but was {p}"),
None: () => Valid(patch));
public static Validation<Version> Validate(Version version)
{
return version.ToOption()
.Match(
Some: v =>
IsValidMajorVersion(v)
.Bind(IsValidMinorVersion)
.Bind(IsValidPatchVersion),
None: () => Invalid("Version cannot be null"));
}

private static Validation<Version> IsValidMajorVersion(Version version) => version.Major >= 0
? Valid(version)
: Invalid<Version>($"Major version must be greater than or equal to 0, but was {version.Major}");

private static Validation<Version> IsValidMinorVersion(Version minor) => minor.Minor.Match(
Some: m => m >= 0
? Valid(minor)
: Invalid<Version>($"Minor version must be greater than or equal to 0, but was {m}"),
None: () => Valid(minor));

private static Validation<Version> IsValidPatchVersion(Version patch) =>
patch.Patch.Match(
Some: p => p >= 0
? Valid(patch)
: Invalid<Version>($"Patch version must be greater than or equal to 0, but was {p}"),
None: () => Valid(patch));
7 replies
CC#
Created by Meigs2 on 9/18/2022 in #help
Composing a validation function using functional concepts
I shouldnt be validating on the individual items, I should be composing around the Version.
7 replies
CC#
Created by Meigs2 on 9/18/2022 in #help
Composing a validation function using functional concepts
oh, I think I got it!
7 replies
CC#
Created by Meigs2 on 9/18/2022 in #help
Composing a validation function using functional concepts
but I feel like the composition is not great in the Validate function, with all the nesting.
7 replies
CC#
Created by Meigs2 on 9/18/2022 in #help
Composing a validation function using functional concepts
So far I have:
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))
);
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))
);
7 replies
CC#
Created by Meigs2 on 9/18/2022 in #help
Composing a validation function using functional concepts
I hope that makes some sense
7 replies