assert icon indicating copy to clipboard operation
assert copied to clipboard

add isDateCorrect() assert

Open badmansan opened this issue 5 years ago • 6 comments

I think this assertion will be very useful

badmansan avatar Oct 04 '20 15:10 badmansan

What's the real life use case for this? What is then supposed to be done with the value (which is now known to be a "correct date") and the knowledge about it being a "correct date"?

zerkms avatar Oct 04 '20 23:10 zerkms

What's the real life use case for this? What is then supposed to be done with the value (which is now known to be a "correct date") and the knowledge about it being a "correct date"?

I use this check in api, at the very beginning, when receiving arguments. This allows you to immediately throw a validation error.

badmansan avatar Oct 05 '20 08:10 badmansan

This allows you to immediately throw a validation error.

I'm not sure assertion should be used instead of validation, but that's another discussion.

And what exactly do you do that value (later in your code)?

zerkms avatar Oct 05 '20 08:10 zerkms

And what exactly do you do that value (later in your code)?

I use it in sql queries. If the date is initially incorrect, all further actions do not make sense

badmansan avatar Oct 05 '20 09:10 badmansan

You should really use DateTime instances and convert them to strings yourself, for example like in https://github.com/doctrine/dbal/blob/3622d6d5823ccaba5d768c311aed27383655041f/lib/Doctrine/DBAL/Types/DateType.php#L30-L44

Ocramius avatar Nov 06 '20 14:11 Ocramius

So this is verifying if a specific value (2021-02-28) is on the format (Y-m-d).

I dont see any tests and Im hesitant about the use case. What you really should do is:

$date = \DateTimeImmutable::createFromFormat('Y-m-d', $rawDateInput);
if ($date === false) {
  throw new ValidationError('Wrong date');
}

$mySercice->foo($date);

Ie, as soon as you can, use a DateTimeImmutable, because you know that is safe compared to $rawDateInput.

Nyholm avatar Feb 28 '21 20:02 Nyholm