uno.extensions icon indicating copy to clipboard operation
uno.extensions copied to clipboard

[MVUX] Implement Validation into State.SetAsync and State.UpdateAsync etc.

Open DevTKSS opened this issue 2 months ago • 0 comments

What would you like to be added:

  • State.Validate(AsyncFunc<T,bool> validationOperation) extension should be available for Mvux State and ListState Value Setting / Updating Operations or alternatively a Overload for the existant methods
  • Maybe we could even get a simple overload for ForEachData(
  • would fit greacefully integrate with adding a Uno.Extensions.Validation.Reactive to the existant Uno.Extensions.Validation(.Fluent)

Why is this needed:

Example code that could be used in our Model:

  • await MyIntState.UpdateAsync(oldValue => oldValue -=1, ct)
  • await MyIntState.UpdateAsync(oldValue => oldValue +=1, ct)

There seems no option to implement Validation criteria for this centrally. e.g. what if MyIntState would have allowed/valid Value Range from 1 - 10? There is literally nothing that would be holding us back from getting beyond this Range.

What we currently need to do:

public async Task MoveLeft([FeedParameter(CurrentLane)]int currentLane, CancellationToken ct = default) // if you are lucky, your method doesn't require the CommandParameter argument, so you can utilize the Mvux Attributes like this
{
     // Or if you wouldn't be able to use FeedParameterAttribute
    // var currentLine = await CurrentLine
    if (0 < currentLine <= 10)
    {
        // Update state immutably
        await CurrentLane.UpdateAsync(lastLane => lastLane -= 1, ct);
    }
    
    var newLane = await CurrentLane

     _logger.LogDebug("Player moving left from lane {currentLane} to new lane {newLane}", currentLane, newLane);

}

And now let's assume, your State might be way more used than this here or imagine, you have a ListState in a Business environment Application! You could not have a nice centralized Method like if something like that would exist:

public IState<int> CurrentLane => State.Value(this, () => 1).Validate(ValidateCurrentLaneUpdateAsync);

public async ValueTask<bool> ValidateCurrentLaneUpdateAsync(int newValue, CancellationToken ct)
{
    if (newValue > 10)
    {
       // Log problem?
       // send Message to the User who asked for the update, to tell him about this beeing invalid value range
       Messenger.Send(...);
       return false;
    }
    return true;
}

For which Platform:

  • [x] iOS
  • [x] Android
  • [x] WebAssembly
  • [x] WebAssembly renders for Xamarin.Forms
  • [x] Windows
  • [x] Desktop
  • [ ] Build tasks

Anything else we need to know?

if you could consider adding Validation to the recognized UnoFeatures, that would be really nice too:

  • #2883

DevTKSS avatar Dec 20 '25 22:12 DevTKSS