validateValue method

  1. @override
void validateValue(
  1. File value
)
inherited

Validates the parsed value, throwing a FormatException if the value is invalid, or a UsageException if the value is invalid for other reasons.

Subclasses may override this method to perform specific validations. If they do, they must also call the super implementation.

Implementation

@override
void validateValue(final File value) {
  super.validateValue(value);

  final type = FileSystemEntity.typeSync(value.path);
  switch (mode) {
    case PathExistMode.mayExist:
      if (type != FileSystemEntityType.notFound &&
          type != FileSystemEntityType.file) {
        throw UsageException('Path "${value.path}" is not a file', '');
      }
      break;
    case PathExistMode.mustExist:
      if (type == FileSystemEntityType.notFound) {
        throw UsageException('File "${value.path}" does not exist', '');
      }
      if (type != FileSystemEntityType.file) {
        throw UsageException('Path "${value.path}" is not a file', '');
      }
      break;
    case PathExistMode.mustNotExist:
      if (type != FileSystemEntityType.notFound) {
        throw UsageException('Path "${value.path}" already exists', '');
      }
      break;
  }
}