-
Notifications
You must be signed in to change notification settings - Fork 60
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
Add limited 'oneOf' json schema dependencies support for plan schemas. #677
Add limited 'oneOf' json schema dependencies support for plan schemas. #677
Conversation
ping @spadgett @philipgough @pb82 @maleck13 Related to UI & Broker changes discussed in openshift/ansible-service-broker#859 & #675 |
In the example I see a field always shows. Is it possible to not show any fields also? excuse my ignorance just not sure |
@maleck13 do you mean "don't show this field if another field has a specific value" ? If yes, I don't think that's part of json schema dependencies. i.e. you define fields that are dependencies of others, but not fields that are negated dependencies.
In this case, field 3 & 4 won't be shown if option 1 is selected on field 1. Is there a use case you're thinking of? |
@philipgough I've added support for below if you want to try it again:
|
After discussion on openshift/ansible-service-broker#859 (comment) I'll look to round out this PR by allowing |
@spadgett This is ready for review now. |
_.each(requiredFields, (requiredField: any) => { | ||
let fieldIndex = _.findIndex(this.ctrl.parameterForm, (field: any) => { | ||
if (_.isObject(field)) { | ||
return requiredFields.indexOf(field.key) > -1; |
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.
return _.includes(requiredFields, field.key);
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.
This code is actually wrong.
It should be:
if (_.isObject(field)) {
return requiredField === field.key;
} else {
return requiredField === field;
}
|
||
// Check if the required field is already in the schema properties. | ||
// Add it if not. | ||
if (_.isUndefined(this.ctrl.parameterSchema.properties[field.key])) { |
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.
I'd probably write this as
!_.has(this.ctrl.parameterSchema, ['properties', field.key]) {
|
||
private mapDependencyCondition(schema, key) { | ||
// Get the fields to show if the dependency is valid | ||
let requiredFields = _.get(schema, 'required'); |
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.
We're not good about this in the catalog repo, but better to use const
} | ||
|
||
// Set the angular 'condition' | ||
let enumMatches = _.get(schema, 'properties.' + key + '.enum'); |
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.
const enumMatches = _.get(schema, ['properties', key, 'enum']);
|
||
// Set the angular 'condition' | ||
let enumMatches = _.get(schema, 'properties.' + key + '.enum'); | ||
if (_.isUndefined(enumMatches)) { |
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.
Should this just be...?
if (enumMatches) {
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.
This is an undefined check, so do you mean if (!enumMatches) {
?
|
||
private escapeIfString(value) { | ||
if (_.isString(value)) { | ||
return '"' + value + '"'; |
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.
OK, so if I understand correctly this is escaping the string to use in an Angular expression. If the string contains an "
character, could I break out an execute arbitrary expressions?
Should we just use JSON.stringify
on all values?
What types can value
be?
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.
OK, so if I understand correctly this is escaping the string to use in an Angular expression. If the string contains an " character, could I break out an execute arbitrary expressions?
I have tested a value with quotes in it (in the serviceplan definition), and they are escaped before they get to this point.
However, ...
Should we just use JSON.stringify on all values?
... this makes a lot of sense and will definitely prevent arbitrary expressions.
I'll change it to use this.
What types can value be?
String, number, boolean.
test: this.escapeIfString(enumMatches[0]) | ||
}); | ||
// Allow matching with any other strings in the array (if there are multiple) | ||
for (var oi=1, ol=enumMatches.length; oi<ol; oi++) { |
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.
let
@spadgett Thanks for the feedback. My typescript skills are obviously lacking :) I've pushed up changes for re-review |
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.
LGTM, but hold for 3.11
/hold
This change examinse the json schema object in the service plan, looking for a `dependencies` object. If this exists, any `oneOf` property dependencies defined will be examined and a corresponding angular-schema-form `condition` will be added to the form definition so specified fields will only be shown when the corresponding option is selected/entered in another field. Example service plan that has a `oneOf` dependency for changing the follow up fields depending on the selected value in the first selection field. https://gist.github.com/david-martin/594464b8193fb3f81c9bd878709731b5
cb5a06c
to
271a12e
Compare
@spadgett ok to merge this now as it is generically useful |
Issues go stale after 90d of inactivity. Mark the issue as fresh by commenting If this issue is safe to close now please do so with /lifecycle stale |
Out of date, no longer required |
This change examines the json schema object in the service plan,
looking for a
dependencies
object.If this exists, any
oneOf
property dependencies defined will beexamined and a corresponding angular-schema-form
condition
willbe added to the form definition so specified fields will only be
shown when the corresponding option is selected/entered in another
field.
Short clip of how it looks
https://youtu.be/MX-G0TjaP7E
Example plan
Example parameters in an apb.yml (pending related broker changes in openshift/ansible-service-broker#928)
Changes based on initial integration with broker (with @philipgough)
dependencies
and in theproperties