Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Oxpecker.ModelValidation #33

Merged
merged 9 commits into from
Nov 26, 2024
Merged

Introduce Oxpecker.ModelValidation #33

merged 9 commits into from
Nov 26, 2024

Conversation

Lanayx
Copy link
Owner

@Lanayx Lanayx commented Nov 25, 2024

Validation model based on standard ASP.NET Core validation mechanism based on System.ComponentModel.DataAnnotations attributes.

Comment on lines +17 to +22
match dict.TryGetValue(memberName) with
| true, value -> value.Add(error.ErrorMessage)
| false, _ ->
let arrayList = ResizeArray(1)
arrayList.Add(error.ErrorMessage)
dict[memberName] <- arrayList
Copy link

@habib-sadullaev habib-sadullaev Nov 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as an option

Suggested change
match dict.TryGetValue(memberName) with
| true, value -> value.Add(error.ErrorMessage)
| false, _ ->
let arrayList = ResizeArray(1)
arrayList.Add(error.ErrorMessage)
dict[memberName] <- arrayList
if not (dict.Contains(memberName)) then dict[memberName] <- ResizeArray(1)
dict[memberName].Add(error.ErrorMessage)

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will cause more dict access

```fsharp
[<RequireQualifiedAccess>]
type ModelState<'T> =
| Empty

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe NotValidated to prevent any collision.
However, if you require qualified access this is fine too

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, RequireQualifiedAccess resolves any collisions

Comment on lines 37 to 46
member this.Value(f: 'T -> string | null) =
match this with
| Empty -> null
| Valid model -> f model
| Invalid(model, _) -> f model
member this.BoolValue(f: 'T -> bool) =
match this with
| Empty -> false
| Valid model -> f model
| Invalid(model, _) -> f model

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
member this.Value(f: 'T -> string | null) =
match this with
| Empty -> null
| Valid model -> f model
| Invalid(model, _) -> f model
member this.BoolValue(f: 'T -> bool) =
match this with
| Empty -> false
| Valid model -> f model
| Invalid(model, _) -> f model
private member this.Apply(f: 'T -> 'U) =
match this with
| Empty -> Unchecked.defaultof<'U>
| Valid model -> f model
| Invalid(model, _) -> f model
member this.Value(f: 'T -> string | null) = this.Apply f
member this.BoolValue(f: 'T -> bool) = this.Apply f

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I get the duplication point, I like the readability of the current implementation more (and it doesn't use Unchecked module)

type ValidationErrors(errors: ResizeArray<ValidationResult>) =
let errorDict =
lazy
(let dict = Dictionary<string, ResizeArray<string|null>>()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to do this nullable?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes
image

| Empty
| Valid of 'T
| Invalid of InvalidModel<'T>
member this.Value(f: 'T -> string|null) =
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be better/more idiomatic as an Option<string>? Curious why the nullability here. I don't write F# in my day job, so I might be missing some nuance.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, the nuance is that it should be mostly used to set value attribute of the input and that has type string | null for performance and DX reasons. So I tried to match types here to avoid unnecessary conversions.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that makes a lot of sense! Thanks for the explanation

@Lanayx Lanayx merged commit 0c20a51 into develop Nov 26, 2024
2 of 4 checks passed
@Lanayx Lanayx deleted the validation branch November 26, 2024 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants