UNPKG

912 BJavaScriptView Raw
1import _ from 'lodash';
2import { CraftAiDecisionError } from './errors';
3import semver from 'semver';
4
5export default function parse(input) {
6 const json = _.isObject(input) ? input : JSON.parse(input);
7 if (!_.isObject(json) || _.isArray(input)) {
8 throw new CraftAiDecisionError('Invalid decision tree format, the given json is not an object.');
9 }
10 if (_.isUndefined(json) || _.isUndefined(json._version)) {
11 throw new CraftAiDecisionError('Invalid decision tree format, unable to find the version informations.');
12 }
13
14 const version = json._version;
15 if (!semver.valid(version)) {
16 throw new CraftAiDecisionError(`Invalid decision tree format, "${version}" is not a valid version.`);
17 }
18 else if (semver.satisfies(version, '>=1.0.0 <3.0.0')) {
19 return json;
20 }
21 else {
22 throw new CraftAiDecisionError(`Invalid decision tree format, "${version}" is not a supported version.`);
23 }
24}