Repository navigation
fix: fix validation checks and preserve error messages in basic catalog #1045
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
Changes from all commits
793e709
d4bc88d
12db51a
d665149
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,9 +8,9 @@ import 'package:json_schema_builder/json_schema_builder.dart'; | |
| import '../../model/a2ui_schemas.dart'; | ||
| import '../../model/catalog_item.dart'; | ||
| import '../../model/data_model.dart'; | ||
| import '../../model/validation_helper.dart'; | ||
| import '../../primitives/simple_items.dart'; | ||
| import '../../widgets/widget_utilities.dart'; | ||
| import 'widget_helpers.dart'; | ||
|
|
||
| final _schema = S.object( | ||
| description: 'A selectable checkbox used for boolean toggles with a label.', | ||
|
|
@@ -61,14 +61,14 @@ final checkBox = CatalogItem( | |
| value: checkBoxData.label, | ||
| builder: (context, label) { | ||
| // Wrap the checkbox in validation | ||
| return StreamBuilder<bool>( | ||
| stream: itemContext.dataContext.evaluateConditionStream( | ||
| checksToExpression(checkBoxData.checks), | ||
| return StreamBuilder<String?>( | ||
| stream: ValidationHelper.validateStream( | ||
| checkBoxData.checks, | ||
| itemContext.dataContext, | ||
| ), | ||
| initialData: true, | ||
| builder: (context, snapshot) { | ||
| final bool isValid = snapshot.data ?? true; | ||
| final bool isError = !isValid; | ||
| final String? errorMessage = snapshot.data; | ||
| final isError = errorMessage != null; | ||
|
|
||
| return ListTileTheme.merge( | ||
| child: BoundBool( | ||
|
|
@@ -92,9 +92,9 @@ final checkBox = CatalogItem( | |
| ); | ||
| } | ||
| }, | ||
| subtitle: isError | ||
| subtitle: errorMessage != null | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit CheckBox now uses both |
||
| ? Text( | ||
| 'Invalid value', | ||
| errorMessage, | ||
| style: TextStyle( | ||
| color: Theme.of(context).colorScheme.error, | ||
| fontSize: 12, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -147,11 +147,20 @@ Object? checksToExpression(List<JsonMap>? checks) { | |
| return true; | ||
| } | ||
|
|
||
| final List<Object?> conditions = checks | ||
| .map((c) => c['condition']) | ||
| .where((c) => c != null) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
After the migration, the only use of |
||
| .toList(); | ||
| if (conditions.isEmpty) { | ||
| return true; | ||
| } | ||
| if (conditions.length == 1) { | ||
| return conditions.first; | ||
| } | ||
|
|
||
| // Combine all checks into a single 'and' condition | ||
| return { | ||
| 'functionCall': { | ||
| 'call': 'and', | ||
| 'args': {'values': checks.map((c) => c['condition']).toList()}, | ||
| }, | ||
| 'call': 'and', | ||
| 'args': {'values': conditions}, | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -115,6 +115,16 @@ class DataContext implements cf.ExecutionContext { | |||||||||
| if (value.containsKey('call')) { | ||||||||||
| return _evaluateFunctionCall(value as JsonMap); | ||||||||||
| } | ||||||||||
| if (value.containsKey('functionCall') && value['functionCall'] is Map) { | ||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should
|
||||||||||
| return _evaluateStream(value['functionCall']); | ||||||||||
| } | ||||||||||
| } | ||||||||||
| if (value is List) { | ||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. List recursion applies to every This fixes This may be what we want for lists of dynamic values, but it's a behavior change beyond validation. Could you either limit the recursion to function args or note it in the CHANGELOG, and add a |
||||||||||
| if (value.isEmpty) return Stream.value(<Object?>[]); | ||||||||||
| final List<Stream<Object?>> itemStreams = value | ||||||||||
| .map(_evaluateStream) | ||||||||||
| .toList(); | ||||||||||
| return itemStreams.combineLatestAll(); | ||||||||||
| } | ||||||||||
| if (value is Stream) return value.cast<Object?>(); | ||||||||||
| return Stream.value(value); | ||||||||||
|
|
@@ -123,6 +133,9 @@ class DataContext implements cf.ExecutionContext { | |||||||||
| Stream<Object?> _evaluateFunctionCall(JsonMap callDefinition) { | ||||||||||
| final name = callDefinition['call'] as String?; | ||||||||||
| if (name == null) { | ||||||||||
| genUiLogger.warning( | ||||||||||
| 'Function call missing "call" property: $callDefinition', | ||||||||||
| ); | ||||||||||
| return Stream.value(null); | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -167,12 +180,33 @@ class DataContext implements cf.ExecutionContext { | |||||||||
| if (condition == null) return Stream.value(false); | ||||||||||
| if (condition is bool) return Stream.value(condition); | ||||||||||
|
|
||||||||||
| if (condition is Map && | ||||||||||
| !condition.containsKey('path') && | ||||||||||
| !condition.containsKey('call') && | ||||||||||
| !condition.containsKey('functionCall')) { | ||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The guard accepts any map that has a Suggest checking |
||||||||||
| genUiLogger.warning('Invalid condition expression: $condition'); | ||||||||||
| return Stream.value(false); | ||||||||||
| } | ||||||||||
|
|
||||||||||
| final Stream<Object?> resultStream = _evaluateStream(condition); | ||||||||||
| return resultStream.map((v) { | ||||||||||
| if (v is bool) return v; | ||||||||||
| return v != null; | ||||||||||
| }); | ||||||||||
| return resultStream.map(isTruthy); | ||||||||||
| } | ||||||||||
| } | ||||||||||
|
|
||||||||||
| /// Helper to determine truthiness according to A2UI expression semantics. | ||||||||||
| bool isTruthy(Object? value) { | ||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||
| if (value is bool) return value; | ||||||||||
| if (value == null) return false; | ||||||||||
| if (value is String) return value.isNotEmpty; | ||||||||||
| if (value is num) return value != 0 && !value.isNaN; | ||||||||||
| if (value is List) return value.isNotEmpty; | ||||||||||
| if (value is Map) { | ||||||||||
| if (value.containsKey('valid') && value['valid'] is bool) { | ||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where do As far as I can tell, no function in genui returns a map with a |
||||||||||
| return value['valid'] as bool; | ||||||||||
| } | ||||||||||
| return value.isNotEmpty; | ||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nested malformed expressions pass again Now that a non-empty map is truthy, a typo inside
The A literal map isn't a valid |
||||||||||
| } | ||||||||||
| return false; | ||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unknown non-null types are now falsy Before this PR, any non-null value was truthy. Now anything that isn't bool/String/num/List/Map returns On the broader change: |
||||||||||
| } | ||||||||||
|
|
||||||||||
| /// Resolves a context map definition against a [DataContext]. | ||||||||||
|
|
||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Describe behavior, not internals
The entry names
DataContext._evaluateStream, a private method, and describes the implementation. Suggested wording, aimed at people using the package: