This repository has been archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from zapier/pe-184--require-samples-for-non-hi…
…dden-ops PE-184 Require samples for non hidden ops
- Loading branch information
Showing
13 changed files
with
483 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
const jsonschema = require('jsonschema'); | ||
|
||
// todo: deal with circular dep. | ||
const RESOURCE_ID = '/ResourceSchema'; | ||
const RESOURCE_METHODS = ['get', 'hook', 'list', 'search', 'create']; | ||
|
||
const check = definition => { | ||
if (!definition.operation || _.get(definition, 'display.hidden')) { | ||
return null; | ||
} | ||
|
||
const samples = _.get(definition, 'operation.sample', {}); | ||
return !_.isEmpty(samples) | ||
? null | ||
: new jsonschema.ValidationError( | ||
'requires "sample", because it\'s not hidden', | ||
definition, | ||
definition.id | ||
); | ||
}; | ||
|
||
module.exports = (definition, mainSchema) => { | ||
let definitions = []; | ||
|
||
if (mainSchema.id === RESOURCE_ID) { | ||
definitions = RESOURCE_METHODS.map(method => definition[method]).filter( | ||
Boolean | ||
); | ||
|
||
// allow method definitions to inherit the sample | ||
if (definition.sample) { | ||
definitions.forEach(methodDefinition => { | ||
if (methodDefinition.operation && !methodDefinition.operation.sample) { | ||
methodDefinition.operation.sample = definition.sample; | ||
} | ||
}); | ||
} | ||
|
||
if (!definitions.length) { | ||
return [ | ||
new jsonschema.ValidationError( | ||
'expected at least one resource operation', | ||
definition, | ||
definition.id | ||
) | ||
]; | ||
} | ||
} else { | ||
definitions = [definition]; | ||
} | ||
|
||
return definitions.map(check).filter(Boolean); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.