From 9faa113e9b71e3b4efd465d59f6c80a0e0790829 Mon Sep 17 00:00:00 2001 From: Remco Vlierman Date: Wed, 14 Sep 2016 17:33:04 +0200 Subject: [PATCH] fix requiring non-existent custom-formats file (#116) --- .eslintrc | 2 +- custom-formats.js | 30 ++++++------- index.js | 4 ++ templates/outerDescribe.handlebars | 3 +- test/.eslintrc | 2 + .../compare/request/assert/base-path-test.js | 44 ++++++++++++++++++- .../compare/request/expect/base-path-test.js | 44 ++++++++++++++++++- .../compare/request/should/base-path-test.js | 44 ++++++++++++++++++- .../supertest/assert/base-path-test.js | 44 ++++++++++++++++++- .../supertest/expect/base-path-test.js | 44 ++++++++++++++++++- .../supertest/should/base-path-test.js | 44 ++++++++++++++++++- .../compare/request/assert/base-path-test.js | 44 ++++++++++++++++++- .../compare/request/expect/base-path-test.js | 44 ++++++++++++++++++- .../compare/request/should/base-path-test.js | 44 ++++++++++++++++++- .../supertest/assert/base-path-test.js | 44 ++++++++++++++++++- .../supertest/expect/base-path-test.js | 44 ++++++++++++++++++- .../supertest/should/base-path-test.js | 44 ++++++++++++++++++- 17 files changed, 538 insertions(+), 31 deletions(-) diff --git a/.eslintrc b/.eslintrc index 719df03..a2acfad 100644 --- a/.eslintrc +++ b/.eslintrc @@ -33,7 +33,7 @@ wrap-iife: 2 one-var: [2, "never"] quote-props: [2, "as-needed"] - max-len: [2, 80, 2] + max-len: [2, 160, 2] yoda: [2, "never"] dot-notation: 2 new-cap: 2 diff --git a/custom-formats.js b/custom-formats.js index 7edd9c0..b5fa797 100644 --- a/custom-formats.js +++ b/custom-formats.js @@ -1,45 +1,41 @@ -/** - * Placeholder file for all custom-formats in known to swagger.json - * as found on - * https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#dataTypeFormat - */ +module.exports = function(zSchema) { + // Placeholder file for all custom-formats in known to swagger.json + // as found on + // https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#dataTypeFormat -var decimalPattern = /^\d{0,8}.?\d{0,4}[0]+$/; - -exports = module.exports = function(ZSchema) { + var decimalPattern = /^\d{0,8}.?\d{0,4}[0]+$/; /** Validates floating point as decimal / money (i.e: 12345678.123400..) */ - ZSchema.registerFormat("double", function(val) { + zSchema.registerFormat('double', function(val) { return !decimalPattern.test(val.toString()); }); /** Validates value is a 32bit integer */ - ZSchema.registerFormat("int32", function(val) { + zSchema.registerFormat('int32', function(val) { // the 32bit shift (>>) truncates any bits beyond max of 32 return Number.isInteger(val) && ((val >> 0) === val); }); - ZSchema.registerFormat("int64", function(val) { + zSchema.registerFormat('int64', function(val) { return Number.isInteger(val); }); - ZSchema.registerFormat("float", function(val) { + zSchema.registerFormat('float', function(val) { // should parse return Number.isInteger(val); }); - ZSchema.registerFormat("date", function(val) { + zSchema.registerFormat('date', function(val) { // should parse a a date return !isNaN(Date.parse(val)); }); - ZSchema.registerFormat("dateTime", function(val) { + zSchema.registerFormat('dateTime', function(val) { return !isNaN(Date.parse(val)); }); - ZSchema.registerFormat("password", function(val) { + zSchema.registerFormat('password', function(val) { // should parse as a string - return typeof val === 'string' + return typeof val === 'string'; }); - }; diff --git a/index.js b/index.js index 73c8563..6689b4f 100644 --- a/index.js +++ b/index.js @@ -28,6 +28,7 @@ var TYPE_JSON = 'application/json'; var handlebars = require('handlebars'); var sanitize = require('sanitize-filename'); +var fs = require('fs'); var read = require('fs').readFileSync; var _ = require('lodash'); var strObj = require('string'); @@ -399,10 +400,13 @@ function testGenPath(swagger, path, config) { }); var output = ''; + var customFormats = fs.readFileSync(require.resolve('./custom-formats'), 'utf-8'); + var data = { description: path, assertion: config.assertionFormat, testmodule: config.testModule, + customFormats: customFormats, scheme: (swagger.schemes !== undefined ? swagger.schemes[0] : 'http'), host: (swagger.host !== undefined ? swagger.host : 'localhost:10010'), tests: result, diff --git a/templates/outerDescribe.handlebars b/templates/outerDescribe.handlebars index d5d72a1..56341ef 100644 --- a/templates/outerDescribe.handlebars +++ b/templates/outerDescribe.handlebars @@ -1,8 +1,9 @@ 'use strict'; var chai = require('chai');{{#if importValidator}} var ZSchema = require('z-schema'); +var customFormats = {{customFormats}} +customFormats(ZSchema); -require('./custom-formats')(ZSchema); var validator = new ZSchema({});{{/if}} {{#is testmodule 'request'}} var request = require('request'); diff --git a/test/.eslintrc b/test/.eslintrc index af70d65..0a89fcf 100644 --- a/test/.eslintrc +++ b/test/.eslintrc @@ -4,3 +4,5 @@ mocha: true rules: no-unused-expressions: false + valid-jsdoc: 0; + diff --git a/test/loadTest/compare/request/assert/base-path-test.js b/test/loadTest/compare/request/assert/base-path-test.js index c5f6e76..da23291 100644 --- a/test/loadTest/compare/request/assert/base-path-test.js +++ b/test/loadTest/compare/request/assert/base-path-test.js @@ -1,8 +1,50 @@ 'use strict'; var chai = require('chai'); var ZSchema = require('z-schema'); +var customFormats = module.exports = function(zSchema) { + // Placeholder file for all custom-formats in known to swagger.json + // as found on + // https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#dataTypeFormat + + var decimalPattern = /^\d{0,8}.?\d{0,4}[0]+$/; + + /** Validates floating point as decimal / money (i.e: 12345678.123400..) */ + zSchema.registerFormat('double', function(val) { + return !decimalPattern.test(val.toString()); + }); + + /** Validates value is a 32bit integer */ + zSchema.registerFormat('int32', function(val) { + // the 32bit shift (>>) truncates any bits beyond max of 32 + return Number.isInteger(val) && ((val >> 0) === val); + }); + + zSchema.registerFormat('int64', function(val) { + return Number.isInteger(val); + }); + + zSchema.registerFormat('float', function(val) { + // should parse + return Number.isInteger(val); + }); + + zSchema.registerFormat('date', function(val) { + // should parse a a date + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('dateTime', function(val) { + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('password', function(val) { + // should parse as a string + return typeof val === 'string'; + }); +}; + +customFormats(ZSchema); -require('./custom-formats')(ZSchema); var validator = new ZSchema({}); var request = require('request'); var assert = chai.assert; diff --git a/test/loadTest/compare/request/expect/base-path-test.js b/test/loadTest/compare/request/expect/base-path-test.js index 17c6be1..bdc0ac6 100644 --- a/test/loadTest/compare/request/expect/base-path-test.js +++ b/test/loadTest/compare/request/expect/base-path-test.js @@ -1,8 +1,50 @@ 'use strict'; var chai = require('chai'); var ZSchema = require('z-schema'); +var customFormats = module.exports = function(zSchema) { + // Placeholder file for all custom-formats in known to swagger.json + // as found on + // https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#dataTypeFormat + + var decimalPattern = /^\d{0,8}.?\d{0,4}[0]+$/; + + /** Validates floating point as decimal / money (i.e: 12345678.123400..) */ + zSchema.registerFormat('double', function(val) { + return !decimalPattern.test(val.toString()); + }); + + /** Validates value is a 32bit integer */ + zSchema.registerFormat('int32', function(val) { + // the 32bit shift (>>) truncates any bits beyond max of 32 + return Number.isInteger(val) && ((val >> 0) === val); + }); + + zSchema.registerFormat('int64', function(val) { + return Number.isInteger(val); + }); + + zSchema.registerFormat('float', function(val) { + // should parse + return Number.isInteger(val); + }); + + zSchema.registerFormat('date', function(val) { + // should parse a a date + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('dateTime', function(val) { + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('password', function(val) { + // should parse as a string + return typeof val === 'string'; + }); +}; + +customFormats(ZSchema); -require('./custom-formats')(ZSchema); var validator = new ZSchema({}); var request = require('request'); var expect = chai.expect; diff --git a/test/loadTest/compare/request/should/base-path-test.js b/test/loadTest/compare/request/should/base-path-test.js index 1a88560..07d5d7a 100644 --- a/test/loadTest/compare/request/should/base-path-test.js +++ b/test/loadTest/compare/request/should/base-path-test.js @@ -1,8 +1,50 @@ 'use strict'; var chai = require('chai'); var ZSchema = require('z-schema'); +var customFormats = module.exports = function(zSchema) { + // Placeholder file for all custom-formats in known to swagger.json + // as found on + // https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#dataTypeFormat + + var decimalPattern = /^\d{0,8}.?\d{0,4}[0]+$/; + + /** Validates floating point as decimal / money (i.e: 12345678.123400..) */ + zSchema.registerFormat('double', function(val) { + return !decimalPattern.test(val.toString()); + }); + + /** Validates value is a 32bit integer */ + zSchema.registerFormat('int32', function(val) { + // the 32bit shift (>>) truncates any bits beyond max of 32 + return Number.isInteger(val) && ((val >> 0) === val); + }); + + zSchema.registerFormat('int64', function(val) { + return Number.isInteger(val); + }); + + zSchema.registerFormat('float', function(val) { + // should parse + return Number.isInteger(val); + }); + + zSchema.registerFormat('date', function(val) { + // should parse a a date + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('dateTime', function(val) { + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('password', function(val) { + // should parse as a string + return typeof val === 'string'; + }); +}; + +customFormats(ZSchema); -require('./custom-formats')(ZSchema); var validator = new ZSchema({}); var request = require('request'); diff --git a/test/loadTest/compare/supertest/assert/base-path-test.js b/test/loadTest/compare/supertest/assert/base-path-test.js index e9904aa..6a04c96 100644 --- a/test/loadTest/compare/supertest/assert/base-path-test.js +++ b/test/loadTest/compare/supertest/assert/base-path-test.js @@ -1,8 +1,50 @@ 'use strict'; var chai = require('chai'); var ZSchema = require('z-schema'); +var customFormats = module.exports = function(zSchema) { + // Placeholder file for all custom-formats in known to swagger.json + // as found on + // https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#dataTypeFormat + + var decimalPattern = /^\d{0,8}.?\d{0,4}[0]+$/; + + /** Validates floating point as decimal / money (i.e: 12345678.123400..) */ + zSchema.registerFormat('double', function(val) { + return !decimalPattern.test(val.toString()); + }); + + /** Validates value is a 32bit integer */ + zSchema.registerFormat('int32', function(val) { + // the 32bit shift (>>) truncates any bits beyond max of 32 + return Number.isInteger(val) && ((val >> 0) === val); + }); + + zSchema.registerFormat('int64', function(val) { + return Number.isInteger(val); + }); + + zSchema.registerFormat('float', function(val) { + // should parse + return Number.isInteger(val); + }); + + zSchema.registerFormat('date', function(val) { + // should parse a a date + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('dateTime', function(val) { + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('password', function(val) { + // should parse as a string + return typeof val === 'string'; + }); +}; + +customFormats(ZSchema); -require('./custom-formats')(ZSchema); var validator = new ZSchema({}); var supertest = require('supertest'); var api = supertest('https://api.uber.com'); // supertest init; diff --git a/test/loadTest/compare/supertest/expect/base-path-test.js b/test/loadTest/compare/supertest/expect/base-path-test.js index 5d7c594..ae53a4c 100644 --- a/test/loadTest/compare/supertest/expect/base-path-test.js +++ b/test/loadTest/compare/supertest/expect/base-path-test.js @@ -1,8 +1,50 @@ 'use strict'; var chai = require('chai'); var ZSchema = require('z-schema'); +var customFormats = module.exports = function(zSchema) { + // Placeholder file for all custom-formats in known to swagger.json + // as found on + // https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#dataTypeFormat + + var decimalPattern = /^\d{0,8}.?\d{0,4}[0]+$/; + + /** Validates floating point as decimal / money (i.e: 12345678.123400..) */ + zSchema.registerFormat('double', function(val) { + return !decimalPattern.test(val.toString()); + }); + + /** Validates value is a 32bit integer */ + zSchema.registerFormat('int32', function(val) { + // the 32bit shift (>>) truncates any bits beyond max of 32 + return Number.isInteger(val) && ((val >> 0) === val); + }); + + zSchema.registerFormat('int64', function(val) { + return Number.isInteger(val); + }); + + zSchema.registerFormat('float', function(val) { + // should parse + return Number.isInteger(val); + }); + + zSchema.registerFormat('date', function(val) { + // should parse a a date + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('dateTime', function(val) { + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('password', function(val) { + // should parse as a string + return typeof val === 'string'; + }); +}; + +customFormats(ZSchema); -require('./custom-formats')(ZSchema); var validator = new ZSchema({}); var supertest = require('supertest'); var api = supertest('https://api.uber.com'); // supertest init; diff --git a/test/loadTest/compare/supertest/should/base-path-test.js b/test/loadTest/compare/supertest/should/base-path-test.js index 56eee33..a29db8c 100644 --- a/test/loadTest/compare/supertest/should/base-path-test.js +++ b/test/loadTest/compare/supertest/should/base-path-test.js @@ -1,8 +1,50 @@ 'use strict'; var chai = require('chai'); var ZSchema = require('z-schema'); +var customFormats = module.exports = function(zSchema) { + // Placeholder file for all custom-formats in known to swagger.json + // as found on + // https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#dataTypeFormat + + var decimalPattern = /^\d{0,8}.?\d{0,4}[0]+$/; + + /** Validates floating point as decimal / money (i.e: 12345678.123400..) */ + zSchema.registerFormat('double', function(val) { + return !decimalPattern.test(val.toString()); + }); + + /** Validates value is a 32bit integer */ + zSchema.registerFormat('int32', function(val) { + // the 32bit shift (>>) truncates any bits beyond max of 32 + return Number.isInteger(val) && ((val >> 0) === val); + }); + + zSchema.registerFormat('int64', function(val) { + return Number.isInteger(val); + }); + + zSchema.registerFormat('float', function(val) { + // should parse + return Number.isInteger(val); + }); + + zSchema.registerFormat('date', function(val) { + // should parse a a date + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('dateTime', function(val) { + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('password', function(val) { + // should parse as a string + return typeof val === 'string'; + }); +}; + +customFormats(ZSchema); -require('./custom-formats')(ZSchema); var validator = new ZSchema({}); var supertest = require('supertest'); var api = supertest('https://api.uber.com'); // supertest init; diff --git a/test/robust/compare/request/assert/base-path-test.js b/test/robust/compare/request/assert/base-path-test.js index 05b153c..6c0f84d 100644 --- a/test/robust/compare/request/assert/base-path-test.js +++ b/test/robust/compare/request/assert/base-path-test.js @@ -1,8 +1,50 @@ 'use strict'; var chai = require('chai'); var ZSchema = require('z-schema'); +var customFormats = module.exports = function(zSchema) { + // Placeholder file for all custom-formats in known to swagger.json + // as found on + // https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#dataTypeFormat + + var decimalPattern = /^\d{0,8}.?\d{0,4}[0]+$/; + + /** Validates floating point as decimal / money (i.e: 12345678.123400..) */ + zSchema.registerFormat('double', function(val) { + return !decimalPattern.test(val.toString()); + }); + + /** Validates value is a 32bit integer */ + zSchema.registerFormat('int32', function(val) { + // the 32bit shift (>>) truncates any bits beyond max of 32 + return Number.isInteger(val) && ((val >> 0) === val); + }); + + zSchema.registerFormat('int64', function(val) { + return Number.isInteger(val); + }); + + zSchema.registerFormat('float', function(val) { + // should parse + return Number.isInteger(val); + }); + + zSchema.registerFormat('date', function(val) { + // should parse a a date + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('dateTime', function(val) { + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('password', function(val) { + // should parse as a string + return typeof val === 'string'; + }); +}; + +customFormats(ZSchema); -require('./custom-formats')(ZSchema); var validator = new ZSchema({}); var request = require('request'); var assert = chai.assert; diff --git a/test/robust/compare/request/expect/base-path-test.js b/test/robust/compare/request/expect/base-path-test.js index 785d7c1..4aab4bd 100644 --- a/test/robust/compare/request/expect/base-path-test.js +++ b/test/robust/compare/request/expect/base-path-test.js @@ -1,8 +1,50 @@ 'use strict'; var chai = require('chai'); var ZSchema = require('z-schema'); +var customFormats = module.exports = function(zSchema) { + // Placeholder file for all custom-formats in known to swagger.json + // as found on + // https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#dataTypeFormat + + var decimalPattern = /^\d{0,8}.?\d{0,4}[0]+$/; + + /** Validates floating point as decimal / money (i.e: 12345678.123400..) */ + zSchema.registerFormat('double', function(val) { + return !decimalPattern.test(val.toString()); + }); + + /** Validates value is a 32bit integer */ + zSchema.registerFormat('int32', function(val) { + // the 32bit shift (>>) truncates any bits beyond max of 32 + return Number.isInteger(val) && ((val >> 0) === val); + }); + + zSchema.registerFormat('int64', function(val) { + return Number.isInteger(val); + }); + + zSchema.registerFormat('float', function(val) { + // should parse + return Number.isInteger(val); + }); + + zSchema.registerFormat('date', function(val) { + // should parse a a date + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('dateTime', function(val) { + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('password', function(val) { + // should parse as a string + return typeof val === 'string'; + }); +}; + +customFormats(ZSchema); -require('./custom-formats')(ZSchema); var validator = new ZSchema({}); var request = require('request'); var expect = chai.expect; diff --git a/test/robust/compare/request/should/base-path-test.js b/test/robust/compare/request/should/base-path-test.js index 8075e76..20cc49a 100644 --- a/test/robust/compare/request/should/base-path-test.js +++ b/test/robust/compare/request/should/base-path-test.js @@ -1,8 +1,50 @@ 'use strict'; var chai = require('chai'); var ZSchema = require('z-schema'); +var customFormats = module.exports = function(zSchema) { + // Placeholder file for all custom-formats in known to swagger.json + // as found on + // https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#dataTypeFormat + + var decimalPattern = /^\d{0,8}.?\d{0,4}[0]+$/; + + /** Validates floating point as decimal / money (i.e: 12345678.123400..) */ + zSchema.registerFormat('double', function(val) { + return !decimalPattern.test(val.toString()); + }); + + /** Validates value is a 32bit integer */ + zSchema.registerFormat('int32', function(val) { + // the 32bit shift (>>) truncates any bits beyond max of 32 + return Number.isInteger(val) && ((val >> 0) === val); + }); + + zSchema.registerFormat('int64', function(val) { + return Number.isInteger(val); + }); + + zSchema.registerFormat('float', function(val) { + // should parse + return Number.isInteger(val); + }); + + zSchema.registerFormat('date', function(val) { + // should parse a a date + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('dateTime', function(val) { + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('password', function(val) { + // should parse as a string + return typeof val === 'string'; + }); +}; + +customFormats(ZSchema); -require('./custom-formats')(ZSchema); var validator = new ZSchema({}); var request = require('request'); diff --git a/test/robust/compare/supertest/assert/base-path-test.js b/test/robust/compare/supertest/assert/base-path-test.js index 208afbc..59208a5 100644 --- a/test/robust/compare/supertest/assert/base-path-test.js +++ b/test/robust/compare/supertest/assert/base-path-test.js @@ -1,8 +1,50 @@ 'use strict'; var chai = require('chai'); var ZSchema = require('z-schema'); +var customFormats = module.exports = function(zSchema) { + // Placeholder file for all custom-formats in known to swagger.json + // as found on + // https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#dataTypeFormat + + var decimalPattern = /^\d{0,8}.?\d{0,4}[0]+$/; + + /** Validates floating point as decimal / money (i.e: 12345678.123400..) */ + zSchema.registerFormat('double', function(val) { + return !decimalPattern.test(val.toString()); + }); + + /** Validates value is a 32bit integer */ + zSchema.registerFormat('int32', function(val) { + // the 32bit shift (>>) truncates any bits beyond max of 32 + return Number.isInteger(val) && ((val >> 0) === val); + }); + + zSchema.registerFormat('int64', function(val) { + return Number.isInteger(val); + }); + + zSchema.registerFormat('float', function(val) { + // should parse + return Number.isInteger(val); + }); + + zSchema.registerFormat('date', function(val) { + // should parse a a date + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('dateTime', function(val) { + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('password', function(val) { + // should parse as a string + return typeof val === 'string'; + }); +}; + +customFormats(ZSchema); -require('./custom-formats')(ZSchema); var validator = new ZSchema({}); var supertest = require('supertest'); var api = supertest('https://api.uber.com'); // supertest init; diff --git a/test/robust/compare/supertest/expect/base-path-test.js b/test/robust/compare/supertest/expect/base-path-test.js index b7311bb..264228b 100644 --- a/test/robust/compare/supertest/expect/base-path-test.js +++ b/test/robust/compare/supertest/expect/base-path-test.js @@ -1,8 +1,50 @@ 'use strict'; var chai = require('chai'); var ZSchema = require('z-schema'); +var customFormats = module.exports = function(zSchema) { + // Placeholder file for all custom-formats in known to swagger.json + // as found on + // https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#dataTypeFormat + + var decimalPattern = /^\d{0,8}.?\d{0,4}[0]+$/; + + /** Validates floating point as decimal / money (i.e: 12345678.123400..) */ + zSchema.registerFormat('double', function(val) { + return !decimalPattern.test(val.toString()); + }); + + /** Validates value is a 32bit integer */ + zSchema.registerFormat('int32', function(val) { + // the 32bit shift (>>) truncates any bits beyond max of 32 + return Number.isInteger(val) && ((val >> 0) === val); + }); + + zSchema.registerFormat('int64', function(val) { + return Number.isInteger(val); + }); + + zSchema.registerFormat('float', function(val) { + // should parse + return Number.isInteger(val); + }); + + zSchema.registerFormat('date', function(val) { + // should parse a a date + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('dateTime', function(val) { + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('password', function(val) { + // should parse as a string + return typeof val === 'string'; + }); +}; + +customFormats(ZSchema); -require('./custom-formats')(ZSchema); var validator = new ZSchema({}); var supertest = require('supertest'); var api = supertest('https://api.uber.com'); // supertest init; diff --git a/test/robust/compare/supertest/should/base-path-test.js b/test/robust/compare/supertest/should/base-path-test.js index 16d6de7..6f198b4 100644 --- a/test/robust/compare/supertest/should/base-path-test.js +++ b/test/robust/compare/supertest/should/base-path-test.js @@ -1,8 +1,50 @@ 'use strict'; var chai = require('chai'); var ZSchema = require('z-schema'); +var customFormats = module.exports = function(zSchema) { + // Placeholder file for all custom-formats in known to swagger.json + // as found on + // https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#dataTypeFormat + + var decimalPattern = /^\d{0,8}.?\d{0,4}[0]+$/; + + /** Validates floating point as decimal / money (i.e: 12345678.123400..) */ + zSchema.registerFormat('double', function(val) { + return !decimalPattern.test(val.toString()); + }); + + /** Validates value is a 32bit integer */ + zSchema.registerFormat('int32', function(val) { + // the 32bit shift (>>) truncates any bits beyond max of 32 + return Number.isInteger(val) && ((val >> 0) === val); + }); + + zSchema.registerFormat('int64', function(val) { + return Number.isInteger(val); + }); + + zSchema.registerFormat('float', function(val) { + // should parse + return Number.isInteger(val); + }); + + zSchema.registerFormat('date', function(val) { + // should parse a a date + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('dateTime', function(val) { + return !isNaN(Date.parse(val)); + }); + + zSchema.registerFormat('password', function(val) { + // should parse as a string + return typeof val === 'string'; + }); +}; + +customFormats(ZSchema); -require('./custom-formats')(ZSchema); var validator = new ZSchema({}); var supertest = require('supertest'); var api = supertest('https://api.uber.com'); // supertest init;