Skip to content

Commit

Permalink
Helper for whether a parameter is required (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
itowlson authored Aug 7, 2019
1 parent 37931e5 commit a1f3895
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## 0.0.4

* Added method to check if a parameter is required

## 0.0.3

* Added which parameters apply to a given action
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cnabjs",
"version": "0.0.3",
"version": "0.0.4",
"description": "A library for loading and working with CNAB (Cloud Native Application Bundle) manifests",
"main": "js/ts/index.js",
"types": "js/ts/index.d.ts",
Expand Down
47 changes: 47 additions & 0 deletions test/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,19 @@ const TEST_BUNDLE: cnab.Bundle = {
noApplyTo: { definition: 'sample', destination: {} },
emptyApplyTo: { applyTo: [], definition: 'sample', destination: { } },
applyTo2: { applyTo: ['install', 'update'], definition: 'sample', destination: { } },
required: { required: true, definition: 'sample', destination: {} },
notRequired: { definition: 'sample', destination: {} },
explicitlyNotRequired: { definition: 'sample', destination: {} },
},
};

const NO_PARAMS_BUNDLE: cnab.Bundle = {
name: 'test',
schemaVersion: 'v1',
version: '1.0.0',
invocationImages: [],
};

describe('a parameter with no applyTo', () => {

it('should apply to everything', () => {
Expand Down Expand Up @@ -53,3 +63,40 @@ describe('a parameter with populated applyTo', () => {
assert(!parameters.includes('applyTo2'), 'expected applyTo2 NOT to be an uninstall parameter but it was');
});
});

describe('an action', () => {

it('should have no parameters if there is no parameters section', () => {
const parameters = Parameters.forAction(NO_PARAMS_BUNDLE, 'install');
assert.equal(parameters.length, 0);
});

});

describe('a parameter', () => {

it('should not be required if there is no required atttribute', () => {
const required = Parameters.isRequired(TEST_BUNDLE, 'notRequired');
assert.equal(required, false);
});

it('should not be required if required is false', () => {
const required = Parameters.isRequired(TEST_BUNDLE, 'explicitlyNotRequired');
assert.equal(required, false);
});

it('should be required if required is true', () => {
const required = Parameters.isRequired(TEST_BUNDLE, 'required');
assert.equal(required, true);
});

it('should not be required if it doesn\'t exist', () => {
const required = Parameters.isRequired(TEST_BUNDLE, 'does not exist');
assert.equal(required, false);
});

it('should not be required if there is no parametes section', () => {
const required = Parameters.isRequired(NO_PARAMS_BUNDLE, 'foofaraw');
assert.equal(required, false);
});
});
18 changes: 18 additions & 0 deletions ts/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,22 @@ export namespace Parameters {
return parameter.applyTo.includes(actionName);
}

/**
* Gets whether a particular parameter is required to be specified.
* @param bundle The bundle containg the parameters.
* @param parameterName The name of the parameter to check.
* @returns Whether the parameter is required.
*/
export function isRequired(bundle: Bundle, parameterName: string): boolean {
if (!bundle.parameters) {
return false;
}

const parameter = bundle.parameters[parameterName];
if (!parameter) {
return false;
}

return parameter.required || false;
}
}

0 comments on commit a1f3895

Please sign in to comment.