fix(central-ui): ParameterValueForm — raise ValidChanged on schema set so required-field validity is never stale
Claude-Session: https://claude.ai/code/session_01NcMG6xXdsJbmD2wYAgkK29
This commit is contained in:
@@ -77,6 +77,44 @@ else
|
||||
_shapes = ScriptParameterNames.ParseShapes(ParameterDefinitions);
|
||||
_rootSchema = await ParseRootAsync();
|
||||
_topLevelFields = _rootSchema?.Type == "object" ? _rootSchema.Fields : Array.Empty<InboundApiSchemaField>();
|
||||
|
||||
// Report validity for the freshly-(re)parsed schema so a caller gating a Run
|
||||
// button on ValidChanged never starts (or carries over from a prior script)
|
||||
// a stale "valid" reading when required fields are unset. This deliberately
|
||||
// does NOT call Validate()/populate _parseErrors: the component has no
|
||||
// "untouched field" concept -- RenderError paints whatever is in
|
||||
// _parseErrors unconditionally -- so doing that here would immediately
|
||||
// paint every required field red before the user has touched anything. The
|
||||
// silent check below computes the same answer without that side effect;
|
||||
// _parseErrors (and the visible red text) still only appears once editing
|
||||
// drives it through Emit()/Validate() as before.
|
||||
await ValidChanged.InvokeAsync(ComputeIsValid());
|
||||
}
|
||||
|
||||
// Mirrors Validate()'s schema pass (plus any raw-text parse markers already
|
||||
// parked in _parseErrors) WITHOUT writing to _parseErrors, so it can run on
|
||||
// every schema (re)parse without painting untouched required fields red.
|
||||
private bool ComputeIsValid()
|
||||
{
|
||||
if (_parseErrors.Count > 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (_rootSchema is null)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
try
|
||||
{
|
||||
var element = JsonSerializer.SerializeToElement(Values);
|
||||
var errors = new List<string>();
|
||||
_rootSchema.Validate(element, string.Empty, errors);
|
||||
return errors.Count == 0;
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -297,4 +297,32 @@ public class ParameterValueFormTests : BunitContext
|
||||
input.Input("5");
|
||||
Assert.True(valid.Last());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ValidChanged_ReportsFalseOnFirstRender_WhenARequiredFieldIsEmpty()
|
||||
{
|
||||
var valid = new List<bool>();
|
||||
var cut = Render<ParameterValueForm>(p => p
|
||||
.Add(x => x.ParameterDefinitions, """{"type":"object","properties":{"n":{"type":"integer"}},"required":["n"]}""")
|
||||
.Add(x => x.ValidChanged, v => valid.Add(v)));
|
||||
|
||||
// Reported without any input from the user...
|
||||
Assert.False(valid.Last());
|
||||
|
||||
// ...and no red error text is painted for the untouched required field (the
|
||||
// component has no "untouched" concept, so the validity check for
|
||||
// ValidChanged deliberately does not populate _parseErrors on first render).
|
||||
Assert.Empty(cut.FindAll(".text-danger"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ValidChanged_ReportsTrueOnFirstRender_WhenNothingIsRequired()
|
||||
{
|
||||
var valid = new List<bool>();
|
||||
var cut = Render<ParameterValueForm>(p => p
|
||||
.Add(x => x.ParameterDefinitions, """{"type":"object","properties":{"n":{"type":"integer"}}}""")
|
||||
.Add(x => x.ValidChanged, v => valid.Add(v)));
|
||||
|
||||
Assert.True(valid.Last());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user