csS3ValidateUri function

String? csS3ValidateUri(
  1. String uri, [
  2. CsS3ResourceSelectorI18nStrings s = const CsS3ResourceSelectorI18nStrings()
])

Validates an S3 URI the way Cloudscape's selector does and returns the message for the first rule it breaks, or null when it is valid (an empty URI is valid).

Implementation

String? csS3ValidateUri(String uri, [CsS3ResourceSelectorI18nStrings s = const CsS3ResourceSelectorI18nStrings()]) {
  const scheme = 's3://';
  if (uri.isEmpty) return null;
  if (!uri.startsWith(scheme)) return s.validationPathMustBegin;
  final bucket = csS3SplitUri(uri).$1;
  if (!RegExp(r'^[a-z0-9]').hasMatch(bucket)) return s.validationBucketLowerCase;
  if (RegExp('[A-Z]').hasMatch(bucket)) return s.validationBucketMustNotContain;
  if (bucket.length < 3 || bucket.length > 63) return s.validationBucketLength;
  final dns = RegExp(r'^(([a-z0-9]|[a-z0-9][a-z0-9-]*[a-z0-9])\.)*([a-z0-9]|[a-z0-9][a-z0-9-]*[a-z0-9])$');
  if (!dns.hasMatch(bucket)) return s.validationBucketMustComplyDns;
  return null;
}