Skip to content

Commit

Permalink
Feat/ajv formats example (#45)
Browse files Browse the repository at this point in the history
* feat: update ajv formats version

* feat: add complex validation tests
  • Loading branch information
kevinccbsg authored May 11, 2021
1 parent c032f4d commit d3e7406
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 23 deletions.
19 changes: 3 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
14 changes: 11 additions & 3 deletions test/complexValidation/complexValidation.test.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/complexValidation/fake-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/

/**
Expand Down
4 changes: 2 additions & 2 deletions test/complexValidation/mock.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
},
"year": {
"description": "The year",
"type": "integer",
"format": "int64"
"type": "number",
"format": "double"
}
}
},
Expand Down

0 comments on commit d3e7406

Please sign in to comment.