Skip to content

Instantly share code, notes, and snippets.

@netmilk
Created December 21, 2015 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netmilk/ca5f39e607336e81edea to your computer and use it in GitHub Desktop.
Save netmilk/ca5f39e607336e81edea to your computer and use it in GitHub Desktop.
Workaround Dredd hook for nullable type attributes in API Blueprint
var hooks = require('hooks');
// Recursively add null as acceptable type if there is string
// "#nullable" present in the property description
var patchPropertiesWithNullable = function(schema) {
if (typeof(schema['properties']) == 'object' && ! Array.isArray(schema['properties'])){
for (property in schema['properties']){
var partialSchemaToPatch = schema['properties'][property];
schema['properties'][property] = patchPropertiesWithNullable(partialSchemaToPatch);
};
};
if (schema['description'] !== undefined) {
if (schema['description'].indexOf("#nullable") > -1) {
if (schema['type'] === undefined) {
schema['type'] = 'null';
} else if (typeof(schema['type']) == 'string') {
schema['type'] = [schema['type'], 'null'];
} else if (Array.isArray(schema['type'])) {
schema['type'].push('null');
};
};
};
return(schema);
};
hooks.beforeAll(function(transactions, callback) {
for(index in transactions) {
var transaction = transactions[index];
if(transaction['expected']['bodySchema'] !== undefined){
var schema = JSON.parse(transaction['expected']['bodySchema']);
schema = patchPropertiesWithNullable(schema);
transactions[index]['expected']['bodySchema'] = JSON.stringify(schema, null, 2);
};
};
callback();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment