Skip to content

Commit

Permalink
format functions should return true if value type does not match spec…
Browse files Browse the repository at this point in the history
…ified types
  • Loading branch information
Dan Rasmussen committed Feb 19, 2015
1 parent d9d48eb commit 3f67a05
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
8 changes: 4 additions & 4 deletions schemaFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ var max = MAX_SAFE_INTEGER / 100;
var min = -MAX_SAFE_INTEGER / 100;
function currencyFormatCheck(value) {
if (typeof value !== 'number') {
return false;
return true;
} else if ((value.toString().split('.')[1] || '').length > 2) {
return false;
} else if (value > max || value < min) {
Expand All @@ -169,7 +169,7 @@ function currencyFormatCheck(value) {

function rateFormat(value) {
if (typeof value !== 'number') {
return false;
return true;
} else if ((value.toString().split('.')[1] || '').length > 2) {
return false;
} else if (value > 100 || value < 0) {
Expand All @@ -180,7 +180,7 @@ function rateFormat(value) {

function rateNegativeFormat(value) {
if (typeof value !== 'number') {
return false;
return true;
} else if ((value.toString().split('.')[1] || '').length > 2) {
return false;
} else if (value > 0 || value < -100) {
Expand All @@ -191,7 +191,7 @@ function rateNegativeFormat(value) {

function currencyRateFormat(value) {
if (typeof value !== 'number') {
return false;
return true;
} else if ((value.toString().split('.')[1] || '').length > 6) {
return false;
} else if (value > 999999999 || value < 0.000001) {
Expand Down
4 changes: 2 additions & 2 deletions test/exampleSchemas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,11 @@ module.exports = {

currencySchema:{
"description":'Simple object',
"required":true,
"required":false,
"type":'object',
"properties": {
"a": {
"type": 'number',
"type": ['null', 'number'],
"required": false,
"format": 'currency'
}
Expand Down
32 changes: 31 additions & 1 deletion test/integration/schemaFactory.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ describe('/source/util/schemaFactory run on simpleSchema, the returned object',
});
});

describe('not required field', function () {
describe('invalid format', function () {
var result;

var document = {
Expand All @@ -325,6 +325,36 @@ describe('/source/util/schemaFactory run on simpleSchema, the returned object',
expect(result).to.have.property('valid', false);
});
});

describe('not required field', function () {
var result;

var document = {};

before(function () {
result = schema.validate(document);
});

it('should validate with errors', function () {
expect(result).to.have.property('valid', true);
});
});

describe('allow null', function () {
var result;

var document = {
a: null
};

before(function () {
result = schema.validate(document);
});

it('should validate with errors', function () {
expect(result).to.have.property('valid', true);
});
});
});

describe('validate with filter', function () {
Expand Down

0 comments on commit 3f67a05

Please sign in to comment.