insomnia/app/validators/request.js

95 lines
2.1 KiB
JavaScript
Raw Normal View History

2016-03-20 20:42:27 +00:00
import {Validator} from 'jsonschema';
import {METHODS} from "../constants/global";
const validator = new Validator();
const paramSchema = {
id: '/Param',
type: 'object',
properties: {
name: {type: 'string'},
value: {type: 'string'}
},
required: ['name'],
additionalProperties: false
};
const headerSchema = {
id: '/Header',
type: 'object',
properties: {
name: {type: 'string', minLength: 1},
value: {type: 'string', minLength: 0}
},
required: ['name', 'value'],
additionalProperties: false
};
const noAuthenticationSchema = {
id: '/NoAuthentication',
type: 'object',
properties: {},
required: [],
additionalProperties: false
};
const basicAuthenticationSchema = {
id: '/BasicAuthentication',
type: 'object',
properties: {
username: {type: 'string', minLength: 1},
password: {type: 'string', minLength: 0}
},
required: ['username', 'password'],
additionalProperties: false
};
const requestSchema = {
id: '/Request',
type: 'object',
properties: {
id: {type: 'string', pattern: '^rq_[\\w]{13}$'},
created: {type: 'number', minimum: 1000000000000, maximum: 10000000000000},
modified: {type: 'number', minimum: 1000000000000, maximum: 10000000000000},
name: {type: 'string', minLength: 1},
method: {enum: METHODS},
2016-03-20 23:20:00 +00:00
url: {type: 'string'},
body: {type: 'string'},
2016-04-10 02:58:48 +00:00
contentType: {type: 'string'},
2016-03-20 20:42:27 +00:00
authentication: {
oneOf: [
{$ref: '/BasicAuthentication'},
{$ref: '/NoAuthentication'}
]
},
params: {type: 'array', items: {$ref: '/Param'}},
headers: {type: 'array', items: {$ref: '/Header'}}
},
required: [
'id',
2016-03-20 23:20:00 +00:00
'url',
2016-03-20 20:42:27 +00:00
'created',
'modified',
'name',
'method',
'body',
'authentication',
'params',
'headers'
],
additionalProperties: false
};
validator.addSchema(paramSchema);
validator.addSchema(headerSchema);
validator.addSchema(noAuthenticationSchema);
validator.addSchema(basicAuthenticationSchema);
export default function (requestObject) {
const result = validator.validate(requestObject, requestSchema);
if (result.errors.length) {
// console.warn(result.errors);
}
return result;
}