Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support 'function' data type #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions lib/suites/draft-04/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,17 @@ exports.jsonEqual = jsonEqual;
// ******************************************************************
function apparentType(val) {
switch (typeof val) {
case 'boolean':
case 'string':
return typeof val;
case 'object':
if (val === null) { return 'null'; }
if (Array.isArray(val)) { return 'array'; }
return 'object';

case 'number':
if (val % 1 === 0) { return 'integer'; }
return 'number';

default:
if (val === null) { return 'null'; }
if (Array.isArray(val)) { return 'array'; }
return 'object';
return typeof val;
}
}
exports.apparentType = apparentType;
Expand Down
26 changes: 26 additions & 0 deletions tests/apparentType.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe('Core § 3.5 JSON Schema primitive types:', function() {
core.apparentType(null).should.not.equal('array');
core.apparentType({foo: [1, 2, 3]}).should.not.equal('array');
core.apparentType('hello world').should.not.equal('array');
core.apparentType(function(){}).should.not.equal('array');
});
});

Expand All @@ -43,6 +44,7 @@ describe('Core § 3.5 JSON Schema primitive types:', function() {
core.apparentType(null).should.not.equal('boolean');
core.apparentType({foo: [1, 2]}).should.not.equal('boolean');
core.apparentType('hello world').should.not.equal('boolean');
core.apparentType(function(){}).should.not.equal('boolean');
});
});

Expand All @@ -61,6 +63,7 @@ describe('Core § 3.5 JSON Schema primitive types:', function() {
core.apparentType(null).should.not.equal('integer');
core.apparentType({foo: [1, 2]}).should.not.equal('integer');
core.apparentType('hello world').should.not.equal('integer');
core.apparentType(function(){}).should.not.equal('integer');
});
});

Expand All @@ -78,6 +81,7 @@ describe('Core § 3.5 JSON Schema primitive types:', function() {
core.apparentType(null).should.not.equal('number');
core.apparentType({foo: [1, 2, 3]}).should.not.equal('number');
core.apparentType('hello world').should.not.equal('number');
core.apparentType(function(){}).should.not.equal('number');
});
});

Expand All @@ -95,6 +99,7 @@ describe('Core § 3.5 JSON Schema primitive types:', function() {
core.apparentType(42.1).should.not.equal('null');
core.apparentType({foo: [1, 2, 3]}).should.not.equal('null');
core.apparentType('hello world').should.not.equal('null');
core.apparentType(function(){}).should.not.equal('null');
});
});

Expand All @@ -115,6 +120,7 @@ describe('Core § 3.5 JSON Schema primitive types:', function() {
core.apparentType(42.1).should.not.equal('object');
core.apparentType(null).should.not.equal('object');
core.apparentType('hello world').should.not.equal('object');
core.apparentType(function(){}).should.not.equal('object');
});

it('should not mistake null or Array for "object"', function() {
Expand All @@ -137,6 +143,26 @@ describe('Core § 3.5 JSON Schema primitive types:', function() {
core.apparentType(42.1).should.not.equal('string');
core.apparentType(null).should.not.equal('string');
core.apparentType({foo: 'hello'}).should.not.equal('string');
core.apparentType(function(){}).should.not.equal('string');
});
});

describe('function:', function() {
it('should return "function"', function() {
core.apparentType(function(){}).should.equal('function');
core.apparentType([].map).should.equal('function');
});

it ('should not return "function" for non-function types', function() {
core.apparentType([true, false]).should.not.equal('function');
core.apparentType(true).should.not.equal('function');
core.apparentType(false).should.not.equal('function');
core.apparentType(0).should.not.equal('function');
core.apparentType(42).should.not.equal('function');
core.apparentType(42.1).should.not.equal('function');
core.apparentType(null).should.not.equal('function');
core.apparentType({foo: [1, 2]}).should.not.equal('function');
core.apparentType('hello world').should.not.equal('function');
});
});
});