Skip to content

Instantly share code, notes, and snippets.

@mikermcneil
Created March 12, 2013 14:19
Show Gist options
  • Select an option

  • Save mikermcneil/5143241 to your computer and use it in GitHub Desktop.

Select an option

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
/**
* 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