Created
February 16, 2015 23:19
-
-
Save nijikokun/9af989950fa282619c8e to your computer and use it in GitHub Desktop.
Generate (BASIC) JSON Schema from JSON Object
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
var Type = require('type-of-is') | |
module.exports = function generateJsonSchema (object) { | |
for (var key in object) { | |
var value = object[key] | |
var type = Type.string(value).toLowerCase() | |
if (type === 'undefined') { | |
type = 'null' | |
} | |
if (type !== 'object') { | |
object[key] = { | |
type: type | |
} | |
} else { | |
object[key] = generateJsonSchema(object[key]) | |
object[key].type = type | |
} | |
} | |
return object | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment