forked from zuriby/Faker.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[dist] First pass at schema generator #170
* Generates a mschema from faker api * Useful for auto-documentation * Starts to provide API contract to all methods
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
var faker = require('../'); | ||
|
||
var items = Object.keys(faker); | ||
|
||
items = items.filter(function(i){ | ||
if(['locales', 'definitions', 'locale', 'localeFallback'].indexOf(i) === -1) { | ||
return i; | ||
} | ||
}); | ||
|
||
var schema = { | ||
"methods": { | ||
"type": "string", | ||
"enum": [] | ||
} | ||
}; | ||
|
||
schema.modules = { | ||
"type": "string", | ||
"enum": [] | ||
}; | ||
|
||
schema.methodSchemas = { | ||
}; | ||
|
||
items.forEach(function(item){ | ||
// schema[item] = {}; | ||
|
||
schema.modules.enum.push(item); | ||
for (var q in faker[item]) { | ||
|
||
//console.log(item + '.' + q); | ||
var fnLine = faker[item][q].toString().split('\n').slice(0,1)[0]; | ||
var prop; | ||
|
||
// find first ( | ||
var start = fnLine.search(/\(/); | ||
|
||
// find first ) | ||
var end = fnLine.search(/\)/); | ||
|
||
// substr on those positions | ||
fnLine = fnLine.substr(start + 1, end - start - 1) | ||
|
||
if (fnLine === "") { | ||
//console.log(item + '.' + q, 'no arguments') | ||
prop = { | ||
}; | ||
} else { | ||
// split on , | ||
fnLine = fnLine.split(','); | ||
//console.log(item + '.' + q, fnLine); | ||
prop = {}; | ||
fnLine.forEach(function(arg){ | ||
prop[arg] = { | ||
type: "any" | ||
}; | ||
}); | ||
} | ||
schema.methods.enum.push(item + '.' + q); | ||
schema.methodSchemas[item + '.' + q] = prop; | ||
} | ||
|
||
}); | ||
|
||
var util = require('util'); | ||
console.log(util.inspect(schema, false, null)); |