Skip to content

Commit 4572b20

Browse files
committed
Openapi Schema improvements
- Return proper booleans in api responses - Update jsonschemavalidation to latest draft
1 parent dfe2588 commit 4572b20

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+863
-2083
lines changed

backend/doc/api.swagger.json

Lines changed: 0 additions & 1456 deletions
This file was deleted.

backend/internal/proxy-host.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const internalAuditLog = require('./audit-log');
88
const internalCertificate = require('./certificate');
99

1010
function omissions () {
11-
return ['is_deleted'];
11+
return ['is_deleted', 'owner.is_deleted'];
1212
}
1313

1414
const internalProxyHost = {

backend/lib/access.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
const _ = require('lodash');
1212
const logger = require('../logger').access;
13-
const validator = require('ajv');
13+
const Ajv = require('ajv/dist/2020');
1414
const error = require('./error');
1515
const userModel = require('../models/user');
1616
const proxyHostModel = require('../models/proxy_host');
@@ -174,7 +174,6 @@ module.exports = function (token_string) {
174174

175175
let schema = {
176176
$id: 'objects',
177-
$schema: 'http://json-schema.org/draft-07/schema#',
178177
description: 'Actor Properties',
179178
type: 'object',
180179
additionalProperties: false,
@@ -251,7 +250,7 @@ module.exports = function (token_string) {
251250
// Initialised, token decoded ok
252251
return this.getObjectSchema(permission)
253252
.then((objectSchema) => {
254-
let data_schema = {
253+
const data_schema = {
255254
[permission]: {
256255
data: data,
257256
scope: Token.get('scope'),
@@ -267,7 +266,6 @@ module.exports = function (token_string) {
267266
};
268267

269268
let permissionSchema = {
270-
$schema: 'http://json-schema.org/draft-07/schema#',
271269
$async: true,
272270
$id: 'permissions',
273271
additionalProperties: false,
@@ -276,14 +274,9 @@ module.exports = function (token_string) {
276274

277275
permissionSchema.properties[permission] = require('./access/' + permission.replace(/:/gim, '-') + '.json');
278276

279-
// logger.info('objectSchema', JSON.stringify(objectSchema, null, 2));
280-
// logger.info('permissionSchema', JSON.stringify(permissionSchema, null, 2));
281-
// logger.info('data_schema', JSON.stringify(data_schema, null, 2));
282-
283-
let ajv = validator({
277+
const ajv = new Ajv({
284278
verbose: true,
285279
allErrors: true,
286-
format: 'full',
287280
missingRefs: 'fail',
288281
breakOnError: true,
289282
coerceTypes: true,

backend/lib/access/permissions.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
"$schema": "http://json-schema.org/draft-07/schema#",
32
"$id": "perms",
43
"definitions": {
54
"view": {

backend/lib/access/roles.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
"$schema": "http://json-schema.org/draft-07/schema#",
32
"$id": "roles",
43
"definitions": {
54
"admin": {

backend/lib/helpers.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,24 @@ module.exports = {
2727
}
2828

2929
return null;
30+
},
31+
32+
convertIntFieldsToBool: function (obj, fields) {
33+
fields.forEach(function (field) {
34+
if (typeof obj[field] !== 'undefined') {
35+
obj[field] = obj[field] === 1;
36+
}
37+
});
38+
return obj;
39+
},
40+
41+
convertBoolFieldsToInt: function (obj, fields) {
42+
fields.forEach(function (field) {
43+
if (typeof obj[field] !== 'undefined') {
44+
obj[field] = obj[field] ? 1 : 0;
45+
}
46+
});
47+
return obj;
3048
}
3149

3250
};

backend/lib/validator/api.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
const Ajv = require('ajv/dist/2020');
12
const error = require('../error');
23

3-
const ajv = require('ajv')({
4-
verbose: true,
5-
validateSchema: true,
6-
allErrors: false,
7-
format: 'full',
8-
coerceTypes: true
4+
const ajv = new Ajv({
5+
verbose: true,
6+
allErrors: true,
7+
allowUnionTypes: true,
8+
strict: false,
9+
coerceTypes: true,
910
});
1011

1112
/**
@@ -25,8 +26,8 @@ function apiValidator (schema, payload/*, description*/) {
2526
return;
2627
}
2728

28-
let validate = ajv.compile(schema);
29-
let valid = validate(payload);
29+
const validate = ajv.compile(schema);
30+
const valid = validate(payload);
3031

3132
if (valid && !validate.errors) {
3233
resolve(payload);

backend/lib/validator/index.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
const _ = require('lodash');
2+
const Ajv = require('ajv/dist/2020');
23
const error = require('../error');
34
const commonDefinitions = require('../../schema/common.json');
45

56
RegExp.prototype.toJSON = RegExp.prototype.toString;
67

7-
const ajv = require('ajv')({
8-
verbose: true,
9-
allErrors: true,
10-
format: 'full', // strict regexes for format checks
11-
coerceTypes: true,
12-
schemas: [commonDefinitions]
8+
const ajv = new Ajv({
9+
verbose: true,
10+
allErrors: true,
11+
allowUnionTypes: true,
12+
coerceTypes: true,
13+
strict: false,
14+
schemas: [commonDefinitions]
1315
});
1416

1517
/**
@@ -38,7 +40,6 @@ function validator (schema, payload) {
3840
}
3941
}
4042
});
41-
4243
}
4344

4445
module.exports = validator;

backend/models/access_list.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// http://vincit.github.io/objection.js/
33

44
const db = require('../db');
5+
const helpers = require('../lib/helpers');
56
const Model = require('objection').Model;
67
const User = require('./user');
78
const AccessListAuth = require('./access_list_auth');
@@ -10,6 +11,12 @@ const now = require('./now_helper');
1011

1112
Model.knex(db);
1213

14+
const boolFields = [
15+
'is_deleted',
16+
'satisfy_any',
17+
'pass_auth',
18+
];
19+
1320
class AccessList extends Model {
1421
$beforeInsert () {
1522
this.created_on = now();
@@ -25,6 +32,16 @@ class AccessList extends Model {
2532
this.modified_on = now();
2633
}
2734

35+
$parseDatabaseJson(json) {
36+
json = super.$parseDatabaseJson(json);
37+
return helpers.convertIntFieldsToBool(json, boolFields);
38+
}
39+
40+
$formatDatabaseJson(json) {
41+
json = helpers.convertBoolFieldsToInt(json, boolFields);
42+
return super.$formatDatabaseJson(json);
43+
}
44+
2845
static get name () {
2946
return 'AccessList';
3047
}

backend/models/auth.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
// Objection Docs:
22
// http://vincit.github.io/objection.js/
33

4-
const bcrypt = require('bcrypt');
5-
const db = require('../db');
6-
const Model = require('objection').Model;
7-
const User = require('./user');
8-
const now = require('./now_helper');
4+
const bcrypt = require('bcrypt');
5+
const db = require('../db');
6+
const helpers = require('../lib/helpers');
7+
const Model = require('objection').Model;
8+
const User = require('./user');
9+
const now = require('./now_helper');
910

1011
Model.knex(db);
1112

13+
const boolFields = [
14+
'is_deleted',
15+
];
16+
1217
function encryptPassword () {
1318
/* jshint -W040 */
1419
let _this = this;
@@ -41,6 +46,16 @@ class Auth extends Model {
4146
return encryptPassword.apply(this, queryContext);
4247
}
4348

49+
$parseDatabaseJson(json) {
50+
json = super.$parseDatabaseJson(json);
51+
return helpers.convertIntFieldsToBool(json, boolFields);
52+
}
53+
54+
$formatDatabaseJson(json) {
55+
json = helpers.convertBoolFieldsToInt(json, boolFields);
56+
return super.$formatDatabaseJson(json);
57+
}
58+
4459
/**
4560
* Verify a plain password against the encrypted password
4661
*

0 commit comments

Comments
 (0)