Class: JsonschemaSerializer::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonschema_serializer/builder.rb

Overview

The JsonschemaSerializer::Builder class provides an effective DSL to generate a valid json schema.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBuilder

The new method creates assigns an empty object to a schema instance variable



23
24
25
# File 'lib/jsonschema_serializer/builder.rb', line 23

def initialize
  @schema ||= _object
end

Instance Attribute Details

#schemaObject (readonly)

An hash representation of the schema



19
20
21
# File 'lib/jsonschema_serializer/builder.rb', line 19

def schema
  @schema
end

Class Method Details

.buildObject

The build class method create a new instance and applies the block



11
12
13
14
15
# File 'lib/jsonschema_serializer/builder.rb', line 11

def build
  new.tap do |builder|
    yield(builder) if block_given?
  end
end

Instance Method Details

#_boolean(**opts) ⇒ Object

A base representation of the boolean type.



78
79
80
# File 'lib/jsonschema_serializer/builder.rb', line 78

def _boolean(**opts)
  { type: :boolean }.merge(opts)
end

#_integer(**opts) ⇒ Object

A base representation of the integer type.



97
98
99
# File 'lib/jsonschema_serializer/builder.rb', line 97

def _integer(**opts)
  { type: :integer }.merge(opts)
end

#_number(**opts) ⇒ Object

A base representation of the number type.



120
121
122
# File 'lib/jsonschema_serializer/builder.rb', line 120

def _number(**opts)
  { type: :number }.merge(opts)
end

#_object(**opts) ⇒ Object

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


173
174
175
176
177
# File 'lib/jsonschema_serializer/builder.rb', line 173

def _object(**opts)
  { type: :object, properties: {} }.merge(opts).tap do |h|
    yield(h[:properties]) if block_given?
  end
end

#_string(**opts) ⇒ Object

A base representation of the string type.



143
144
145
# File 'lib/jsonschema_serializer/builder.rb', line 143

def _string(**opts)
  { type: :string }.merge(opts)
end

#array(name, items:, **opts) ⇒ Object

A property representation of the array type.

Params:

name

String or Symbol

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


204
205
206
207
208
# File 'lib/jsonschema_serializer/builder.rb', line 204

def array(name, items:, **opts)
  {
    name => { type: :array, items: items }.merge(opts)
  }
end

#boolean(name, **opts) ⇒ Object

A property representation of the boolean type.

Params:

name

String or Symbol

Optional Params:

default

Boolean default value

description

String property description

title

String property title



92
93
94
# File 'lib/jsonschema_serializer/builder.rb', line 92

def boolean(name, **opts)
  { name => _boolean(opts) }
end

#description(description) ⇒ Object

Assigns the description to the root schema object

params:

description

String description field of the schema object



51
52
53
# File 'lib/jsonschema_serializer/builder.rb', line 51

def description(description)
  @schema[:description] = description
end

#integer(name, **opts) ⇒ Object

A property representation of the integer type.

Params:

name

String or Symbol

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



115
116
117
# File 'lib/jsonschema_serializer/builder.rb', line 115

def integer(name, **opts)
  { name => _integer(opts) }
end

#number(name, **opts) ⇒ Object

A property representation of the number type.

Params:

name

String or Symbol

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



138
139
140
# File 'lib/jsonschema_serializer/builder.rb', line 138

def number(name, **opts)
  { name => _number(opts) }
end

#propertiesObject

The properties method allows to access object properties

e.g.:

JsonschemaSerializer::Builder.build do |b|
  b.properties.tap do |p|
    p.merge! {}
  end
end


73
74
75
# File 'lib/jsonschema_serializer/builder.rb', line 73

def properties
  @schema[:properties] ||= {}
end

#required(*required) ⇒ Object

The required method allows to provide a list of required properties

params: required [Array[String, Symbol]]



60
61
62
# File 'lib/jsonschema_serializer/builder.rb', line 60

def required(*required)
  @schema[:required] = required
end

#string(name, **opts) ⇒ Object

A property representation of the string type.

Params:

name

String or Symbol

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



159
160
161
# File 'lib/jsonschema_serializer/builder.rb', line 159

def string(name, **opts)
  { name => _string(opts) }
end

#title(title) ⇒ Object

Assigns the title to the root schema object

Params:

title

String or Symbol title field of the schema object



42
43
44
# File 'lib/jsonschema_serializer/builder.rb', line 42

def title(title)
  @schema[:title] = title
end

#to_json(pretty: true) ⇒ Object

The to_json method exports the schema as a json string By default it would exported with a pretty print

Params:

pretty

Boolean



33
34
35
# File 'lib/jsonschema_serializer/builder.rb', line 33

def to_json(pretty: true)
  pretty ? JSON.pretty_generate(@schema) : @schema.to_json
end