UNPKG

1.63 kBJavaScriptView Raw
1const Ajv = require('ajv');
2const logger = require('../../lib/logger').gateway;
3
4const ajv = new Ajv({
5 useDefaults: true,
6 coerceTypes: true,
7 verbose: true,
8 logger
9});
10
11require('ajv-keywords')(ajv, 'instanceof');
12
13const registeredKeys = [];
14
15function register(type, name, schema) {
16 /*
17 This piece of code is checking that the schema isn't already registered.
18 This is not optimal and it's happening because the testing helpers aren't
19 removing the schemas when we're shutting down the gateway. Hopefully we'll
20 get back to this once we'll have a better story for programmatic startup/shutdown
21 */
22
23 if (!schema) {
24 logger.warn(`${name} ${type} hasn't provided a schema. Validation for this ${type} will be skipped.`);
25 return () => ({ isValid: true });
26 } else {
27 if (!schema.$id) {
28 throw new Error('The schema must have the $id property.');
29 }
30
31 if (registeredKeys.findIndex(keys => keys.$id === schema.$id) === -1) {
32 ajv.addSchema(schema);
33 registeredKeys.push({ type, $id: schema.$id });
34 } else {
35 ajv.removeSchema(schema.$id).addSchema(schema);
36 }
37 }
38
39 return (data) => validate(schema.$id, data);
40}
41
42function find(param = null) {
43 if (param) {
44 const item = ajv.getSchema(param);
45 if (item) {
46 return { schema: item.schema };
47 }
48 }
49
50 return registeredKeys
51 .filter(item => !param || item.type === param)
52 .map(key => ({ type: key.type, schema: ajv.getSchema(key.$id).schema }));
53}
54
55const validate = (id, data) =>
56 ({ isValid: ajv.validate(id, data), error: ajv.errorsText() });
57
58module.exports = {
59 register,
60 find,
61 validate
62};