Files
pipelines/app/lib/features/chat/conversation/chat_question_validation.dart
T

124 lines
4.4 KiB
Dart

/// Validation shared by the question card and its tests.
///
/// These are the web's `formAnswerError` rules plus the browser input
/// constraints the card cannot delegate to Flutter: `pattern`, `format=email`,
/// `format=uri` and `format=date`. The browser's localised `validationMessage`
/// has no portable equivalent, so the fixed English sentences below stand in
/// for it and are documented as an app addition.
library;
import '../../../core/api_gateway.dart';
import 'chat_question_card_model.dart';
const _patternError = 'does not match the required format';
const _emailError = 'is not a valid email address';
const _urlError = 'is not a valid URL';
const _dateError = 'is not a valid date';
/// The web's validation messages, in field order. An empty string is valid.
String chatFormAnswerError(List<JsonMap> fields, Map<String, dynamic> answers) {
for (final field in fields) {
if (!chatFormFieldVisible(field, answers)) continue;
if (_text(field['type']) == 'external') continue;
final key = _text(field['key']);
final value = answers[key];
final missing =
value == null || value == '' || (value is List && value.isEmpty);
final label = _text(field['title']).isEmpty ? key : _text(field['title']);
if (missing) {
if (field['required'] == true) {
return 'Answer $label before sending.';
}
continue;
}
final type = _text(field['type']);
if (type == 'number' || type == 'integer') {
final number = value is num ? value : null;
final finite = number != null && number.isFinite;
final integral =
type != 'integer' ||
(number != null && number is int) ||
(number != null && number == number.roundToDouble());
if (!finite || !integral) return '$label needs a valid $type.';
final minimum = field['minimum'];
final maximum = field['maximum'];
if (minimum is num && number < minimum) {
return '$label must be at least $minimum.';
}
if (maximum is num && number > maximum) {
return '$label must be at most $maximum.';
}
}
if (type == 'boolean' && value is! bool) {
return '$label needs Yes or No.';
}
if (value is String) {
final minLength = field['min_length'];
final maxLength = field['max_length'];
if (minLength is num && value.length < minLength) {
return '$label is too short.';
}
if (maxLength is num && value.length > maxLength) {
return '$label is too long.';
}
final pattern = _text(field['pattern']);
if (pattern.isNotEmpty && !_matchesPattern(pattern, value)) {
return '$label $_patternError.';
}
final formatError = _formatError(_text(field['format']), value);
if (formatError.isNotEmpty) return '$label $formatError.';
}
if (value is List) {
final minItems = field['min_items'];
final maxItems = field['max_items'];
if (minItems is num && value.length < minItems) {
return 'Select at least $minItems choices for $label.';
}
if (maxItems is num && value.length > maxItems) {
return 'Select at most $maxItems choices for $label.';
}
}
}
return '';
}
/// HTML's `pattern` attribute matches the whole value, not a substring.
bool _matchesPattern(String pattern, String value) {
try {
return RegExp('^(?:$pattern)\$').hasMatch(value);
} catch (_) {
// An unparseable server pattern constrains nothing, exactly as an invalid
// `pattern` attribute is ignored by the browser.
return true;
}
}
String _formatError(String format, String value) {
switch (format) {
case 'email':
return RegExp(r'^[^@\s]+@[^@\s]+\.[^@\s]+$').hasMatch(value)
? ''
: _emailError;
case 'uri':
final uri = Uri.tryParse(value);
return uri != null && uri.hasScheme && !RegExp(r'\s').hasMatch(value)
? ''
: _urlError;
case 'date':
return _isDate(value) ? '' : _dateError;
}
return '';
}
bool _isDate(String value) {
final match = RegExp(r'^(\d{4})-(\d{2})-(\d{2})$').firstMatch(value);
if (match == null) return false;
final year = int.parse(match.group(1)!);
final month = int.parse(match.group(2)!);
final day = int.parse(match.group(3)!);
final date = DateTime(year, month, day);
return date.year == year && date.month == month && date.day == day;
}
String _text(Object? value) => '${value ?? ''}'.trim();