function checkConstraints()

in ui-modules/blueprint-composer/app/components/providers/blueprint-service.provider.js [464:541]


        function checkConstraints(config) {
            for (let constraintO of config.constraints) {
                let message = null;
                let key = null, args = null;
                if (constraintO instanceof String || typeof constraintO=='string') {
                    key = constraintO;
                } else if (Object.keys(constraintO).length==1) {
                    key = Object.keys(constraintO)[0];
                    args = constraintO[key];
                } else {
                    $log.warn("Unknown constraint object", typeof constraintO, constraintO, config);
                    key = constraintO;
                }
                let valExplicit = (k) => entity.config.get(k || config.name);
                let isSet = (k) => entity.config.has(k || config.name) && angular.isDefined(valExplicit(k));
                let val = (k) => isSet(k) ? entity.config.get(k || config.name) : config.defaultValue;
                let isAnySet = (k) => {
                    if (!k || !Array.isArray(k)) return false;
                    return k.some(isSet);
                }
                const hasDefault = (typeof config.defaultValue) !== 'undefined';

                switch (key) {
                    case 'Predicates.notNull()':
                    case 'Predicates.notNull':
                        if ((!isSet() && !hasDefault) || val()===null) {
                            message = `<samp>${config.name}</samp> is required`;
                        }
                        break;
                    case 'required':
                        if ((!isSet() && !hasDefault) || val()===null || val()==='') {
                            message = `<samp>${config.name}</samp> is required`;
                        }
                        break;
                    case 'regex':
                        let matchFullLine = '^' + args + '$';
                        if (isSet() && !(val() instanceof Dsl) && !(new RegExp(matchFullLine).test(val()))) {
                            message = `<samp>${config.name}</samp> does not match the required format: <samp>${args}</samp>`;
                        }
                        break;
                    case 'forbiddenIf':
                        if (isSet() && isSet(args)) {
                            message = `<samp>${config.name}</samp> and <samp>${args}</samp> cannot both be set`;
                        }
                        break;
                    case 'forbiddenUnless':
                        if (isSet() && !isSet(args)) {
                            message = `<samp>${config.name}</samp> can only be set when <samp>${args}</samp> is set`;
                        }
                        break;
                    case 'requiredIf':
                        if (!isSet() && isSet(args)) {
                            message = `<samp>${config.name}</samp> must be set if <samp>${args}</samp> is set`;
                        }
                        break;
                    case 'requiredUnless':
                        if (!isSet() && !isSet(args)) {
                            message = `<samp>${config.name}</samp> or <samp>${args}</samp> is required`;
                        }
                        break;
                    case 'requiredUnlessAnyOf':
                        if (!isSet() && !isAnySet(args)) {
                            message = `<samp>${config.name}</samp> or one of <samp>${args}</samp> is required`;
                        }
                        break;
                    case 'forbiddenUnlessAnyOf':
                        if (isSet() && !isAnySet(args)) {
                            message = `<samp>${config.name}</samp> cannot be set if any of <samp>${args}</samp> are set`;
                        }
                        break;
                    default:
                        $log.warn("Unknown constraint predicate", constraintO, config);
                }
                if (message !== null) {
                    entity.addIssue(Issue.builder().group('config').ref(config.name).message($sce.trustAsHtml(message)).build());
                }
            }
        }