UNPKG

1.48 kBJavaScriptView Raw
1'use strict';
2const _ = require('lodash');
3const builtInSlotsMap = require('./built-in-slots-map');
4const validator = require('./validator');
5const builtInSlotsValues = _.values(builtInSlotsMap);
6const parseError = require('./error-handler').parseError;
7
8/**
9 * Creates custom slot. Checks if custom slot name is not conflicting with amazon built in slots
10 * @param {Object} customSlots - Map of custom slot names to custom slots
11 * @param {string} name - Name of the custom slot
12 * @param {string[]} samples - Array of custom slot samples
13 */
14module.exports = (customSlots, name, samples) => {
15
16 if (customSlots[name]) {
17 const e = parseError(new Error(`Slot with name ${name} is already defined`));
18 throw e;
19 }
20
21 if (builtInSlotsMap[name] || builtInSlotsValues.indexOf(name) !== -1) {
22 const e = parseError(new Error(`Slot with name ${name} is already defined in built-in slots`));
23 throw e;
24 }
25
26 if (!validator.isCustomSlotNameValid(name)) {
27 const e = parseError(new Error(`Custom slot name ${name} is invalid. Only lowercase, uppercase letters and underscores are allowed`));
28 throw e;
29 }
30
31 samples.forEach(sample => {
32 if (validator.isCustomSlotValueValid(sample)) {
33 const e = parseError(new Error(`Custom slot with name ${name} contains invalid special character(~, ^, *, (, ), [, ], §, !, ?, ;, :, " and |): ${sample}`));
34 throw e;
35 }
36 });
37
38 return {
39 name,
40 samples
41 };
42};