class JsonschemaSerializer::Builder
The JsonschemaSerializer::Builder
class provides an effective DSL to generate a valid json schema.
Attributes
An hash representation of the schema
Public Class Methods
The build
class method create a new instance and applies the block
# File lib/jsonschema_serializer/builder.rb 11 def build 12 new.tap do |builder| 13 yield(builder) if block_given? 14 end 15 end
The new
method creates assigns an empty object to a schema
instance variable
# File lib/jsonschema_serializer/builder.rb 23 def initialize 24 @schema ||= _object 25 end
Public Instance Methods
A base representation of the boolean
type.
# File lib/jsonschema_serializer/builder.rb 78 def _boolean(**opts) 79 { type: :boolean }.merge(opts) 80 end
A base representation of the integer
type.
# File lib/jsonschema_serializer/builder.rb 97 def _integer(**opts) 98 { type: :integer }.merge(opts) 99 end
A base representation of the number
type.
# File lib/jsonschema_serializer/builder.rb 120 def _number(**opts) 121 { type: :number }.merge(opts) 122 end
A base representation of the object
type.
JsonschemaSerializer::Builder.build do |b| subscriber = b._object title: :subscriber, required: [:age] do |prop| prop.merge! b.string :first_name, title: 'First Name' prop.merge! b.string :last_name, title: 'Last Name' prop.merge! b.integer :age, title: 'Age' end end
# File lib/jsonschema_serializer/builder.rb 173 def _object(**opts) 174 { type: :object, properties: {} }.merge(opts).tap do |h| 175 yield(h[:properties]) if block_given? 176 end 177 end
A base representation of the string
type.
# File lib/jsonschema_serializer/builder.rb 143 def _string(**opts) 144 { type: :string }.merge(opts) 145 end
A property representation of the array
type.
Params:
name
-
String
orSymbol
items
-
an object representation or an array of objects
Optional Params:
default
-
Array
default value description
-
String
property description title
-
String
property title minItems
-
Int
property minimum length maxItems
-
Int
property maximum length
JsonschemaSerializer::Builder.build do |b| b.array :integers, items: {type: :integer}, minItems:5 b.array :strings, items: b._string, default: [] subscriber = b._object title: :subscriber, required: [:age] do |prop| prop.merge! b.string :first_name, title: 'First Name' prop.merge! b.string :last_name, title: 'Last Name' prop.merge! b.integer :age, title: 'Age' end b.array :subscribers, items: subscriber end
# File lib/jsonschema_serializer/builder.rb 204 def array(name, items:, **opts) 205 { 206 name => { type: :array, items: items }.merge(opts) 207 } 208 end
A property representation of the boolean
type.
Params:
name
-
String
orSymbol
Optional Params:
default
-
Boolean
default value description
-
String
property description title
-
String
property title
# File lib/jsonschema_serializer/builder.rb 92 def boolean(name, **opts) 93 { name => _boolean(opts) } 94 end
Assigns the description
to the root schema object
params:
description
-
String
description field of the schema object
# File lib/jsonschema_serializer/builder.rb 51 def description(description) 52 @schema[:description] = description 53 end
A property representation of the integer
type.
Params:
name
-
String
orSymbol
Optional Params:
default
-
Integer
default value description
-
String
property description title
-
String
property title enum
-
Array
property allowed values minimum
-
Integer
property minimum value maximum
-
Integer
property maximum value multipleOf
-
Integer
property conditional constraint
# File lib/jsonschema_serializer/builder.rb 115 def integer(name, **opts) 116 { name => _integer(opts) } 117 end
A property representation of the number
type.
Params:
name
-
String
orSymbol
Optional Params:
default
-
Numeric
default value description
-
String
property description title
-
String
property title enum
-
Array
property allowed values minimum
-
Numeric
property minimum value maximum
-
Numeric
property maximum value multipleOf
-
Numeric
property conditional constraint
# File lib/jsonschema_serializer/builder.rb 138 def number(name, **opts) 139 { name => _number(opts) } 140 end
The properties
method allows to access object properties
e.g.:
JsonschemaSerializer::Builder.build do |b| b.properties.tap do |p| p.merge! {} end end
# File lib/jsonschema_serializer/builder.rb 73 def properties 74 @schema[:properties] ||= {} 75 end
The required
method allows to provide a list of required properties
params: required
[Array[String, Symbol]]
# File lib/jsonschema_serializer/builder.rb 60 def required(*required) 61 @schema[:required] = required 62 end
A property representation of the string
type.
Params:
name
-
String
orSymbol
Optional Params:
default
-
String
default value description
-
String
property description title
-
String
property title format
-
String
property format for validation minLength
-
Int
property minimum length
# File lib/jsonschema_serializer/builder.rb 159 def string(name, **opts) 160 { name => _string(opts) } 161 end
Assigns the title
to the root schema object
Params:
title
-
String
orSymbol
title field of the schema object
# File lib/jsonschema_serializer/builder.rb 42 def title(title) 43 @schema[:title] = title 44 end
The to_json
method exports the schema as a json string By default it would exported with a pretty print
Params:
pretty
-
Boolean
# File lib/jsonschema_serializer/builder.rb 33 def to_json(pretty: true) 34 pretty ? JSON.pretty_generate(@schema) : @schema.to_json 35 end