Skip to content

Instantly share code, notes, and snippets.

@nijikokun
Created February 16, 2015 23:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nijikokun/9af989950fa282619c8e to your computer and use it in GitHub Desktop.
Save nijikokun/9af989950fa282619c8e to your computer and use it in GitHub Desktop.
Generate (BASIC) JSON Schema from JSON Object
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