TF-4169 Add precheck for HexColor and unit test
This commit is contained in:
@@ -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];
|
||||
|
||||
Reference in New Issue
Block a user