validate static method

({Object? error, bool state}) validate(
  1. String? password, {
  2. String delimiter = ':',
})

Validate that a password is correctly formatted by matching the password against a regular expression. By default the delimiter between each hex octet is expected to be a colon (:).

Returns a Dart 3 record type containing state and if poorly formatted, the error thrown.

Implementation

static ({bool state, Object? error}) validate(
  String? password, {
  String delimiter = ':',
}) {
  if (password == null) {
    return (state: false, error: FormatException('Null SecureON password'));
  }

  final delim = delimiter.split('').map((c) => '\\$c').toList().join();
  final regex = r'^([0-9A-Fa-f]{2}' + delim + r'){5}([0-9A-Fa-f]{2})$';
  RegExp exp = RegExp(regex);

  if (exp.hasMatch(password)) {
    return (state: true, error: null);
  }

  return (state: false, error: FormatException('Invalid SecureON password'));
}