Created
March 12, 2013 14:19
-
-
Save mikermcneil/5143241 to your computer and use it in GitHub Desktop.
Proposed specification for defining simple, RESTful, all-js schemas Use cases:
+ generating JSON API documentation
+ generating unit tests for JSON APIs
+ validating requests and marshaling responses
This file contains hidden or 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
| /** | |
| * Proposed specification for defining simple, RESTful, all-js schemas | |
| * (see anchor: https://github.com/balderdashy/anchor) | |
| * | |
| * Use cases: | |
| * + generating JSON API documentation | |
| * + generating unit tests for JSON APIs | |
| * + validating requests and marshaling responses | |
| * | |
| */ | |
| // Example: | |
| // GET /user/createAll | |
| module.exports = { | |
| // RESTful method to get to this API endpoint | |
| method: 'get', | |
| // Path which leads to this API endpoint | |
| path: '/user', | |
| // Transport-agnostic parameter map | |
| params: { | |
| limit: 'integer', | |
| skip: 'integer', | |
| // Regexes are fine | |
| sort: /(.+) (asc|desc)/i | |
| name: 'string', | |
| // You can be as specific as you like | |
| age: {or: ['integer',{'=': 'integer'},{'equals': 'integer'}], | |
| // Obviously you can take this pretty far if you want to | |
| // Notice that the data types of the request params are less restrictive than the response's | |
| email: {or: ['string',{'=': 'string'},{'equals': 'string'}, {'<=':'string'}, {'<':'string'}, {'>=':'string'}, {'>':'string'}, {'like':'string'}, {'startsWith':'string'}, {'endsWith':'string'}] | |
| }, | |
| // A list of the params which are not optional | |
| required: [], | |
| // A specification of what the response will be | |
| response: [{ | |
| name: 'string', | |
| age: 'number', | |
| email: 'email', | |
| createdAt: 'date', | |
| updatedAt: 'date' | |
| }] | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment