Skip to content

Commit

Permalink
Add output check dynamicFieldsHaveKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
eliangcs committed Dec 17, 2024
1 parent 37b2b61 commit c5efcd8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
30 changes: 30 additions & 0 deletions packages/core/src/checks/dynamic-fields-have-keys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const isInputOrOutputFields = (method) =>
method.endsWith('.operation.inputFields') ||
method.endsWith('.operation.outputFields');

const dynamicFieldsHaveKeys = {
name: 'dynamicFieldsHaveKeys',
shouldRun: isInputOrOutputFields,
run: (method, results) => {
const lastMethodPart = method.split('.').pop();

if (!Array.isArray(results)) {
const type = typeof results;
return [`${lastMethodPart} must be an array, got ${type}`];
}

const errors = [];
for (let i = 0; i < results.length; i++) {
const field = results[i];
if (!field || !field.key) {
errors.push(`${lastMethodPart}[${i}] is missing a key`);
}
}

return errors;
},
};

module.exports = dynamicFieldsHaveKeys;
4 changes: 4 additions & 0 deletions packages/core/src/checks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ module.exports = {
triggerIsObject: require('./trigger-is-object'),
triggerHasUniquePrimary: require('./trigger-has-unique-primary'),
triggerHasId: require('./trigger-has-id'),

firehoseSubscriptionIsArray: require('./firehose_is_array'),
firehoseSubscriptionKeyIsString: require('./firehose_is_string'),

performBufferReturnType: require('./perform-buffer-return-type'),

dynamicFieldsHaveKeys: require('./dynamic-fields-have-keys'),
};
22 changes: 22 additions & 0 deletions packages/core/test/checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,28 @@ describe('checks', () => {
);
errors.should.be.empty();
});

it('should error if input field is missing key', () => {
const results = [{ key: 'foo' }, { title: 'bar' }, { status: 400 }];
const errors = checks.dynamicFieldsHaveKeys.run(
'creates.blah.operation.inputFields',
results,
);
errors.length.should.eql(2);
errors[0].should.eql('inputFields[1] is missing a key');
errors[1].should.eql('inputFields[2] is missing a key');
});

it('should error if output field is missing key', () => {
const results = [{ title: 'bar' }, { key: 'foo' }, { status: 400 }];
const errors = checks.dynamicFieldsHaveKeys.run(
'creates.blah.operation.outputFields',
results,
);
errors.length.should.eql(2);
errors[0].should.eql('outputFields[0] is missing a key');
errors[1].should.eql('outputFields[2] is missing a key');
});
});

describe('checkOutput', () => {
Expand Down

0 comments on commit c5efcd8

Please sign in to comment.