Module: VCAP::JsonSchema

Defined in:
lib/vcap/json_schema.rb

Defined Under Namespace

Classes: ArraySchema, BaseSchema, BoolSchema, HashSchema, MissingKeyError, OptionalKeyMarker, SyntaxError, TypeError, TypeSchema, ValidationError, ValueError

Class Method Summary collapse

Class Method Details

.build(&blk) ⇒ Object



173
174
175
176
# File 'lib/vcap/json_schema.rb', line 173

def build(&blk)
  schema_def = instance_eval(&blk)
  parse(schema_def)
end

.optional(key) ⇒ Object



169
170
171
# File 'lib/vcap/json_schema.rb', line 169

def optional(key)
  OptionalKeyMarker.new(key)
end

.parse(schema_def) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/vcap/json_schema.rb', line 178

def parse(schema_def)
  case schema_def
  when VCAP::JsonSchema::BaseSchema
    schema_def
  when Hash
    schema = VCAP::JsonSchema::HashSchema.new
    for k, v in schema_def
      sym = k.kind_of?(OptionalKeyMarker) ? :optional : :required
      schema.send(sym, k, parse(v))
    end
    schema
  when Array
    raise SyntaxError, "Schema definition for an array must have exactly 1 element" unless schema_def.size == 1
    item_schema = parse(schema_def[0])
    VCAP::JsonSchema::ArraySchema.new(item_schema)
  when Class
    VCAP::JsonSchema::TypeSchema.new(schema_def)
  else
    raise SyntaxError, "Don't know what to do with class #{schema_def.class}"
  end
end