Currently, the dataSize command can fail with BadValue error due to shard key validation if it is run on a stale mongos after the collection is resharded with a different shard key. This is because the mongos validates the command's keyPattern with its stale ChunkManager before sending requests to shards. Since the command fails with BadValue, nothing causes the mongos to refresh so the command continues to fail after that.
To resolve this, we should make each shard validate "shardKey", "min" and "max" by itself. That way, if the mongos is stale, the dataSize command would fail with StaleConfig (instead of BadValue) in the initial try. This error would cause the stale mongos to refresh and the command to succeed when the mongos retries .