New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More constraints #72
More constraints #72
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -236,8 +236,8 @@ function BlueprintService($log, $q, $sce, paletteApi, iconGenerator, dslService) | |
|
||
promise.then((data)=> { | ||
deferred.resolve(populateEntityFromApiSuccess(entity, data)); | ||
}).catch(function () { | ||
deferred.resolve(populateEntityFromApiError(entity)); | ||
}).catch(function (error) { | ||
deferred.resolve(populateEntityFromApiError(entity, error)); | ||
}); | ||
} else if (entity.parent) { | ||
entity.clearIssues({group: 'type'}).addIssue(Issue.builder().group('type').message('Entity needs a type').level(ISSUE_LEVEL.WARN).build()); | ||
|
@@ -275,20 +275,54 @@ function BlueprintService($log, $q, $sce, paletteApi, iconGenerator, dslService) | |
return $q((resolve) => { | ||
if (entity.miscData.has('config')) { | ||
entity.miscData.get('config') | ||
.filter(config => config.constraints && Object.keys(config.constraints).length > 0) | ||
.filter(config => config.constraints && config.constraints.length > 0) | ||
.forEach(config => { | ||
for (let [key, constraint] of Object.entries(config.constraints) ) { | ||
for (let constraintO of config.constraints) { | ||
let message = null; | ||
let key = null, args = null; | ||
if (constraintO instanceof String) { | ||
key = constraintO; | ||
} else if (Object.keys(constraintO).length==1) { | ||
key = Object.keys(constraintO)[0]; | ||
args = constraintO[key]; | ||
} else { | ||
$log.warn("Unknown constraint object", constraintO); | ||
key = constraintO; | ||
} | ||
let val = (k) => entity.config.get(k || config.name); | ||
let isSet = (k) => entity.config.has(k || config.name) && angular.isDefined(val(k)); | ||
let hasDefault = () => angular.isDefined(config.defaultValue); | ||
switch (key) { | ||
case 'Predicates.notNull()': | ||
case 'required': | ||
if (!angular.isDefined(config.defaultValue) && (!entity.config.has(config.name) || !angular.isDefined(entity.config.get(config.name)))) { | ||
if (!isSet() && !hasDefault() && val()!='') { | ||
// "required" also means that it must not be the empty string | ||
message = `<samp>${config.name}</samp> is required`; | ||
} | ||
break; | ||
case 'regex': | ||
if (!entity.config.has(config.name) || !angular.isDefined(entity.config.get(config.name)) || !(new RegExp(constraint).test(entity.config.get(config.name)))) { | ||
message = `<samp>${config.name}</samp> does not match the required format: <samp>${config.constraints.regex}</samp>`; | ||
if (isSet() && !(new RegExp(args).test(val))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the regex constraint respect default values? e.g. if a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @aledsage you're asking what if the default value is invalid? i think it would be weird to give a user error in that case. it probably is an error, but on the upstream blueprint author's part, not the consumer's part. so i think we shouldn't -- and we don't currently -- test the default value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. confirmed verbally w aled -- good to merge |
||
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> cannot be set when <samp>${args}</samp> is set`; | ||
} | ||
break; | ||
case 'forbiddenUnless': | ||
if (isSet() && !isSet(args)) { | ||
message = `<samp>${config.name}</samp> cannot be set unless <samp>${args}</samp> is set`; | ||
} | ||
break; | ||
case 'requiredIf': | ||
if (!isSet() && isSet(args)) { | ||
message = `<samp>${config.name}</samp> is required when <samp>${args}</samp> is set`; | ||
} | ||
break; | ||
case 'requiredUnless': | ||
if (!isSet() && !isSet(args)) { | ||
message = `<samp>${config.name}</samp> is required when <samp>${args}</samp> is not set`; | ||
} | ||
break; | ||
} | ||
|
@@ -321,8 +355,8 @@ function BlueprintService($log, $q, $sce, paletteApi, iconGenerator, dslService) | |
|
||
paletteApi.getType(policy.type, policy.version).then((data)=> { | ||
deferred.resolve(populateEntityFromApiSuccess(policy, data)); | ||
}).catch(function () { | ||
deferred.resolve(populateEntityFromApiError(policy)); | ||
}).catch(function (error) { | ||
deferred.resolve(populateEntityFromApiError(policy, error)); | ||
}).finally(()=> { | ||
policy.miscData.set('loading', false); | ||
}); | ||
|
@@ -340,8 +374,8 @@ function BlueprintService($log, $q, $sce, paletteApi, iconGenerator, dslService) | |
|
||
paletteApi.getType(enricher.type, enricher.version).then((data)=> { | ||
deferred.resolve(populateEntityFromApiSuccess(enricher, data)); | ||
}).catch(function () { | ||
deferred.resolve(populateEntityFromApiError(enricher)); | ||
}).catch(function (error) { | ||
deferred.resolve(populateEntityFromApiError(enricher, error)); | ||
}).finally(()=> { | ||
enricher.miscData.set('loading', false); | ||
}); | ||
|
@@ -482,21 +516,7 @@ function BlueprintService($log, $q, $sce, paletteApi, iconGenerator, dslService) | |
version: data.containingBundle.split(':')[1] | ||
}); | ||
entity.miscData.set('typeName', data.displayName || data.symbolicName); | ||
entity.miscData.set('config', (data.config || []).map(config => { | ||
if (config.constraints) { | ||
config.constraints = config.constraints.reduce((map, constraint) => { | ||
if (constraint.startsWith('matchesRegex')) { | ||
map.regex = constraint.replace(/matchesRegex\(([^)]*)\)/, '$1'); | ||
} else { | ||
// The constraint "required" falls back into this case. Good enough for now as we only support | ||
// 2 types of constraints. | ||
map[constraint] = true; | ||
} | ||
return map; | ||
}, {}); | ||
} | ||
return config; | ||
})); | ||
entity.miscData.set('config', data.config || []); | ||
entity.miscData.set('sensors', data.sensors || []); | ||
entity.miscData.set('traits', data.supertypes || []); | ||
entity.miscData.set('tags', data.tags || []); | ||
|
@@ -520,7 +540,8 @@ function BlueprintService($log, $q, $sce, paletteApi, iconGenerator, dslService) | |
return dst; | ||
} | ||
|
||
function populateEntityFromApiError(entity) { | ||
function populateEntityFromApiError(entity, error) { | ||
$log.warn("Error loading/populating type, data will be incomplete.", entity, error); | ||
entity.clearIssues({group: 'type'}); | ||
entity.addIssue(Issue.builder().group('type').message($sce.trustAsHtml(`Type <samp>${entity.type + (entity.hasVersion ? ':' + entity.version : '')}</samp> does not exist`)).build()); | ||
entity.miscData.set('typeName', entity.type || ''); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/contraint0/constraint
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
deliberately
constraintO
to indicateconstraint-object
. don't mind if someone wants to use different var names but don't think it's necessary.