From d3e7406f469bf6af081ef463f031c43c2ad18d71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kevin=20Juli=C3=A1n=20Mart=C3=ADnez=20Escobar?= Date: Wed, 12 May 2021 00:59:35 +0200 Subject: [PATCH] Feat/ajv formats example (#45) * feat: update ajv formats version * feat: add complex validation tests --- package-lock.json | 19 +++---------------- package.json | 2 +- .../complexValidation.test.js | 14 +++++++++++--- test/complexValidation/fake-server.js | 2 +- test/complexValidation/mock.json | 4 ++-- 5 files changed, 18 insertions(+), 23 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5ffae48..c22b113 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1374,24 +1374,11 @@ } }, "ajv-formats": { - "version": "2.0.2", - "resolved": "https://registry.npmjs.org/ajv-formats/-/ajv-formats-2.0.2.tgz", - "integrity": "sha512-Brah4Uo5/U8v76c6euTwtjVFFaVishwnJrQBYpev1JRh4vjA1F4HY3UzQez41YUCszUCXKagG8v6eVRBHV1gkw==", + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/ajv-formats/-/ajv-formats-2.1.0.tgz", + "integrity": "sha512-USH2jBb+C/hIpwD2iRjp0pe0k+MvzG0mlSn/FIdCgQhUb9ALPRjt2KIQdfZDS9r0ZIeUAg7gOu9KL0PFqGqr5Q==", "requires": { "ajv": "^8.0.0" - }, - "dependencies": { - "ajv": { - "version": "8.0.5", - "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.0.5.tgz", - "integrity": "sha512-RkiLa/AeJx7+9OvniQ/qeWu0w74A8DiPPBclQ6ji3ZQkv5KamO+QGpqmi7O4JIw3rHGUXZ6CoP9tsAkn3gyazg==", - "requires": { - "fast-deep-equal": "^3.1.1", - "json-schema-traverse": "^1.0.0", - "require-from-string": "^2.0.2", - "uri-js": "^4.2.2" - } - } } }, "ansi-colors": { diff --git a/package.json b/package.json index b077e5a..3955c87 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "dependencies": { "ajv": "^8.3.0", - "ajv-formats": "^2.0.2", + "ajv-formats": "^2.1.0", "lodash": "^4.17.21" }, "devDependencies": { diff --git a/test/complexValidation/complexValidation.test.js b/test/complexValidation/complexValidation.test.js index 2a89c9d..853d695 100644 --- a/test/complexValidation/complexValidation.test.js +++ b/test/complexValidation/complexValidation.test.js @@ -1,6 +1,10 @@ +const fs = require('fs'); +const path = require('path'); const validator = require('../..'); const mock = require('./mock.json'); +const binary = fs.readFileSync(path.join(__dirname, 'mock.json'), 'binary').toString('binary'); + /** * All the endpoints we are using, you can find them in the fake-server.js file * And the OpenAPI JSON in the mock.json @@ -13,11 +17,15 @@ describe('ValidateRequest method', () => { }).toThrow('Error in request: must have required property \'title\'. You provide "{"id":"id"}"'); }); - // We have to skip this test until this issue is solved https://github.com/ajv-validator/ajv-formats/issues/25 - it.skip('validate form-data params', () => { + it('validate form-data required param', () => { expect(() => { validateRequest({ id: 'id' }, '/api/v1/album', 'post', 'multipart/form-data'); - }).toThrow('Error in request: should have required property \'title\'. You provide "{"id":"id"}"'); + }).toThrow('Error in request: Schema Song must have required property \'title\'. You provide "{"id":"id"}"'); + }); + + it('validate form-data params', () => { + const result = validateRequest({ title: 'title', year: 1.2, cover: binary }, '/api/v1/album', 'post', 'multipart/form-data'); + expect(result).toBeTruthy(); }); it('validate date type with $ref object', () => { diff --git a/test/complexValidation/fake-server.js b/test/complexValidation/fake-server.js index c869c76..e254be1 100644 --- a/test/complexValidation/fake-server.js +++ b/test/complexValidation/fake-server.js @@ -43,7 +43,7 @@ app.post('/api/v1/songs', (req, res) => res.json({})); * @property {string} title.required - The title * @property {string} artist - The artist * @property {string} cover - image cover - binary - * @property {integer} year - The year - int64 + * @property {number} year - The year - double */ /** diff --git a/test/complexValidation/mock.json b/test/complexValidation/mock.json index 7057e4e..89ed3d6 100644 --- a/test/complexValidation/mock.json +++ b/test/complexValidation/mock.json @@ -35,8 +35,8 @@ }, "year": { "description": "The year", - "type": "integer", - "format": "int64" + "type": "number", + "format": "double" } } },