JSONSchema::Fluent
Contains methods for creating JSONSchema::Validator instances without interacting with the low-level domain objects. This class implements a
DSL through block receivers rather than the standard method-chaining fluent API seen in json-schema libraries in other languages.
An instance of this class is available through JSONSchema.fluent.
require "jsonschema"
js = JSONSchema.fluent
validator = js.object do
prop "first_name", (js.string do
min_length 2
max_length 64
end)
prop "last_name", (js.string do
min_length 2
max_length 64
end)
prop "email", js.string { format "email" }
prop "address", (js.object do
prop "street", js.string
prop "city", js.string
prop "state", js.string
prop "zipcode", js.string
end)
prop "nicknames", (js.array do
min_items 1
items js.string
end)
end
As in the example above, it's recommended to create a shorter variable that references JSONSchema.fluent.
This will improve readability and can be customized to the needs of your code (rather than jsonschema exposing a global method
with a short name).
Instance methods
Create a JSONSchema::ArrayValidator using the fluent API. See JSONSchema::FluentArrayValidator for receiver methods.
js = JSONSchema.fluent
validator = js.array do
unique_items
validator js.string
end
Create a JSONSchema::GenericValidator using the fluent API. See JSONSchema::FluentGenericValidator for receiver methods.
Accepts a block to receive options for the validator.
This method does not have a corresponding non-block-accepting method because a generic validator must have options to be a valid schema.
js = JSONSchema.fluent
validator = js.generic do
enum_list "one", "two", "three"
end
Create a JSONSchema::NumberValidator with the integer constraint set using the fluent API. See JSONSchema::FluentNumberValidator for receiver methods.
js = JSONSchema.fluent
validator = js.integer do
minimum 0
multiple_of 5
end
Convenience method for creating a new JSONSchema::NumberValidator with the integer constraint set (and not other options).
Create a JSONSchema::NumberValidator using the fluent API. See JSONSchema::FluentNumberValidator for receiver methods.
js = JSONSchema.fluent
validator = js.number do
minimum 0
maximum 100
end
Create a JSONSchema::ObjectValidator using the fluent API. See JSONSchema::FluentObjectValidator for receiver methods.
js = JSONSchema.fluent
validator = js.object do
prop "name", js.string
prop "age", (js.integer do
minimum 0
end)
required "name", "age"
end
Create a JSONSchema::StringValidator using the fluent API. See JSONSchema::FluentStringValidator for receiver methods.
js = JSONSchema.fluent
validator = js.string do
min_length 10
pattern /^[a-z0-9-_]$/
end