Skip to content

Commit

Permalink
Dates are no longer considered empty
Browse files Browse the repository at this point in the history
  • Loading branch information
ansman committed Apr 16, 2015
1 parent 4045257 commit e45697d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
32 changes: 32 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@
<li><a href="#utilities-format">format</a></li>
<li><a href="#utilities-get-deep-object-value">getDeepObjectValue</a></li>
<li><a href="#utilities-is-array">isArray</a></li>
<li><a href="#utilities-is-date">isDate</a></li>
<li><a href="#utilities-is-defined">isDefined</a></li>
<li><a href="#utilities-is-dom-element">isDomElement</a></li>
<li><a href="#utilities-is-empty">isEmpty</a></li>
Expand All @@ -398,6 +399,7 @@
<li>
<a href="#changelog">Changelog</a>
<ul>
<li><a href="#changelog-wip">WIP</a></li>
<li><a href="#changelog-0-7-0">0.7.0</a></li>
<li><a href="#changelog-0-6-1">0.6.1</a></li>
<li><a href="#changelog-0-6-0">0.6.0</a></li>
Expand Down Expand Up @@ -1741,6 +1743,23 @@ <h2>Utilities</h2>

validate.isArray([]);
// =&gt; true</code></pre>
</div>
<div id="utilities-is-date">
<div class="signature">
<b>isDate</b>
<code>validate.isDate(value)</code>
</div>
<p class="description">
Check if the given value is a <code>Date</code> instance.
</p>
<pre><code class="javascript">validate.isDate(new Date());
// =&gt; true

validate.isDate(null);
// =&gt; false

validate.isDate({});
// =&gt; false</code></pre>
</div>
<div id="utilities-is-defined">
<div class="signature">
Expand Down Expand Up @@ -1977,6 +1996,19 @@ <h2>Utilities</h2>
</div>
<div id="changelog">
<h2>Changelog</h2>
<div id="changelog-wip">
<h3>
<b class="version">WIP</b>
</h3>
<ul>
<li>
Fixed an issue where dates would not be accepted by the presence
validator due to an issue in <code>validate.isEmpty</code>.
Thanks <a href="https://github.com/ansman/validate.js/issues/48" target="_blank">Yun Jia</a>
for reporting this.
</li>
</ul>
</div>
<div id="changelog-0-7-0">
<h3>
<b class="version">0.7.0</b>
Expand Down
18 changes: 18 additions & 0 deletions specs/validate-helpers-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,18 @@ describe("validate", function() {
});
});

describe("isDate", function() {
it("returns true for dates", function() {
expect(validate.isDate(new Date())).toBe(true);
});

it("returns false for non dates", function() {
expect(validate.isDate(new XDate())).toBe(false);
expect(validate.isDate(Date.now())).toBe(false);
expect(validate.isDate({})).toBe(false);
});
});

describe("isEmpty", function() {
it("considers null and undefined values empty", function() {
expect(validate.isEmpty(null)).toBe(true);
Expand Down Expand Up @@ -790,6 +802,12 @@ describe("validate", function() {
expect(validate.isEmpty(false)).toBe(false);
expect(validate.isEmpty(0)).toBe(false);
});

it("considers date non empty", function() {
spyOn(validate, "isDate").and.callThrough();
expect(validate.isEmpty(new Date())).toBe(false);
expect(validate.isDate).toHaveBeenCalled();
});
});

describe("collectFormValues", function() {
Expand Down
10 changes: 10 additions & 0 deletions validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,11 @@
return obj === Object(obj);
},

// Simply checks if the object is an instance of a date
isDate: function(obj) {
return obj instanceof Date;
},

// Returns false if the object is `null` of `undefined`
isDefined: function(obj) {
return obj !== null && obj !== undefined;
Expand Down Expand Up @@ -334,6 +339,11 @@
return value.length === 0;
}

// Dates have no attributes but aren't empty
if (v.isDate(value)) {
return false;
}

// If we find at least one property we consider it non empty
if (v.isObject(value)) {
for (attr in value) {
Expand Down

0 comments on commit e45697d

Please sign in to comment.