TF-4169 Add precheck for HexColor and unit test

This commit is contained in:
dab246
2025-11-27 10:30:59 +07:00
committed by Dat H. Pham
parent 4986c64802
commit e3c076f1fd
4 changed files with 90 additions and 2 deletions
+16 -1
View File
@@ -1,9 +1,24 @@
import 'package:equatable/equatable.dart';
import 'package:quiver/check.dart';
class HexColor with EquatableMixin {
// Only accept #RRGGBB or #AARRGGBB format
static final RegExp _hexPattern =
RegExp(r'^#[0-9a-fA-F]{6}([0-9a-fA-F]{2})?$');
final String value;
HexColor(this.value);
HexColor(this.value) {
checkArgument(value.isNotEmpty, message: 'hex string must not be empty');
checkArgument(
value.startsWith('#'),
message: 'hex string must start with #',
);
checkArgument(
_hexPattern.hasMatch(value),
message: 'invalid hex format: expected #RRGGBB or #AARRGGBB',
);
}
@override
List<Object> get props => [value];