class JsonschemaSerializer::Builder

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

Attributes

schema[R]

An hash representation of the schema

Public Class Methods

build() { |builder| ... } click to toggle source

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
new() click to toggle source

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

_boolean(**opts) click to toggle source

A base representation of the boolean type.

   # File lib/jsonschema_serializer/builder.rb
78 def _boolean(**opts)
79   { type: :boolean }.merge(opts)
80 end
_integer(**opts) click to toggle source

A base representation of the integer type.

   # File lib/jsonschema_serializer/builder.rb
97 def _integer(**opts)
98   { type: :integer }.merge(opts)
99 end
_number(**opts) click to toggle source

A base representation of the number type.

    # File lib/jsonschema_serializer/builder.rb
120 def _number(**opts)
121   { type: :number }.merge(opts)
122 end
_object(**opts) { |h| ... } click to toggle source

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
_string(**opts) click to toggle source

A base representation of the string type.

    # File lib/jsonschema_serializer/builder.rb
143 def _string(**opts)
144   { type: :string }.merge(opts)
145 end
array(name, items:, **opts) click to toggle source

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
    # File lib/jsonschema_serializer/builder.rb
204 def array(name, items:, **opts)
205   {
206     name => { type: :array, items: items }.merge(opts)
207   }
208 end
boolean(name, **opts) click to toggle source

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

   # File lib/jsonschema_serializer/builder.rb
92 def boolean(name, **opts)
93   { name => _boolean(opts) }
94 end
description(description) click to toggle source

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
integer(name, **opts) click to toggle source

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

    # File lib/jsonschema_serializer/builder.rb
115 def integer(name, **opts)
116   { name => _integer(opts) }
117 end
number(name, **opts) click to toggle source

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

    # File lib/jsonschema_serializer/builder.rb
138 def number(name, **opts)
139   { name => _number(opts) }
140 end
properties() click to toggle source

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
required(*required) click to toggle source

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
string(name, **opts) click to toggle source

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

    # File lib/jsonschema_serializer/builder.rb
159 def string(name, **opts)
160   { name => _string(opts) }
161 end
title(title) click to toggle source

Assigns the title to the root schema object

Params:

title

String or Symbol title field of the schema object

   # File lib/jsonschema_serializer/builder.rb
42 def title(title)
43   @schema[:title] = title
44 end
to_json(pretty: true) click to toggle source

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