<p dir="auto">It would be good to have a validator for JSON date timestamps (as resulting from <code class="notranslate">(new Date()).toJSON()</code> or <code class="notranslate">JSON.stringify(new Date())</code>, e.g. <code class="notranslate">2014-09-13T12:08:06.080Z</code>)</p>
<p dir="auto">It can be useful to validate arrays or objects resulting from the deserialization of a JSON string where some field represents a Date object.</p>
<p dir="auto">A common use case is when you have to write a JSON rest API and you have to handle date fields coming from the request (and it's obviously good to validate these fields before using them on the server side).</p>
<p dir="auto">I have written my own implementation for this and if it seems a good feature to integrate in the symfony core a can submit a pull request and write the related documentation.</p>
<p dir="auto">PS: the validator can also be extended even further and support the entire ISO8601specification (eg. <code class="notranslate">ISO8601DateValidator</code>)</p>
<p dir="auto">At the moment, we have three constraints for validating date/time values:</p>
<ul dir="auto">
<li><code class="notranslate">Date</code> accepts <code class="notranslate">\DateTime</code> instances and 'Y-m-d' strings</li>
<li><code class="notranslate">DateTime</code> accepts <code class="notranslate">\DateTime</code> instances and 'Y-m-d H:i:s' strings</li>
<li><code class="notranslate">Time</code> accepts <code class="notranslate">\DateTime</code> instances and 'H:i:s' strings</li>
</ul>
<p dir="auto">I think that's a bit confusing. Usually, a value should not be a <code class="notranslate">\DateTime</code> <em>or</em> a string, but either of them; if it is a string, it should usually match a specific pattern.</p>
<p dir="auto">Therefore I propose to deprecate all three constraints. The <code class="notranslate">\DateTime</code> case can be solved with the <code class="notranslate">Type</code> constraint already:</p>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="/**
 * @Assert\Type('\DateTime')
 */
private $createdAt;"><pre class="notranslate"><span class="pl-c">/**</span>
<span class="pl-c"> * @Assert\Type('\DateTime')</span>
<span class="pl-c"> */</span>
<span class="pl-k">private</span> <span class="pl-s1"><span class="pl-c1">$</span>createdAt</span>;</pre></div>
<p dir="auto">The second case would be covered by a new <code class="notranslate">Timestamp</code> constraint which accepts a format as argument as <a href="http://de2.php.net/manual/en/function.date.php" rel="nofollow">supported by PHP</a>:</p>
<div class="highlight highlight-text-html-php notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="/**
 * @Assert\Timestamp('Y-m-d')
 */
private $createdAt;

/**
 * @Assert\Timestamp('c')
 */ 
private $iso8601Date;"><pre class="notranslate"><span class="pl-c">/**</span>
<span class="pl-c"> * @Assert\Timestamp('Y-m-d')</span>
<span class="pl-c"> */</span>
<span class="pl-k">private</span> <span class="pl-s1"><span class="pl-c1">$</span>createdAt</span>;

<span class="pl-c">/**</span>
<span class="pl-c"> * @Assert\Timestamp('c')</span>
<span class="pl-c"> */</span> 
<span class="pl-k">private</span> <span class="pl-s1"><span class="pl-c1">$</span>iso8601Date</span>;</pre></div>
<p dir="auto">This would fix <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="42694526" data-permission-text="Title is private" data-url="https://github.com/symfony/symfony/issues/11919" data-hovercard-type="issue" data-hovercard-url="/symfony/symfony/issues/11919/hovercard" href="https://github.com/symfony/symfony/issues/11919">#11919</a> and replace <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="41660699" data-permission-text="Title is private" data-url="https://github.com/symfony/symfony/issues/11821" data-hovercard-type="pull_request" data-hovercard-url="/symfony/symfony/pull/11821/hovercard" href="https://github.com/symfony/symfony/pull/11821">#11821</a>.</p>