       maximum: schema.type.length-1
     typeIndex = random 'integer', randomOptions
     @generate schema.type[typeIndex], depth + 1
+
+
+  ref: (ref, depth = 0) ->
+    path = ref.split('/')
+    if path[0] != '#'
+      throw "ref does not start with #: " + ref
+
+    schema = @root_schema
+    for name in path.slice(1)
+      schema = schema[name]
+      if not schema
+        throw "$ref traversal failed: " + ref
+
+    return @generate schema, depth

  # type = array
  array: (schema, depth = 0) ->
    o = []
    if type(schema.items) is 'array' and schema.items.length
      for itemSchema in schema.items
        o.push @generate itemSchema, depth + 1

    return schema  unless type(schema) is 'object'

    return @enum schema, depth  if schema.enum

    if schema.$ref
      return @ref schema.$ref, depth

    switch schema.type
      when 'string', 'number', 'integer', 'boolean', 'array', 'object', 'null'
