class JSONSchema::Fluent

Overview

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).

Defined in:

fluent.cr

Instance Method Summary

Instance Method Detail

def array(&) : ArrayValidator #

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

[View source]
def array : ArrayValidator #

Convenience method for creating a new JSONSchema::ArrayValidator without options.


[View source]
def boolean #

Convenience method for creating a new JSONSchema::BooleanValidator.


[View source]
def generic(&) : GenericValidator #

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

[View source]
def integer(&) : NumberValidator #

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

[View source]
def integer : NumberValidator #

Convenience method for creating a new JSONSchema::NumberValidator with the integer constraint set (and not other options).


[View source]
def null #

Convenience method for creating a new JSONSchema::NullValidator.


[View source]
def number(&) : NumberValidator #

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

[View source]
def number : NumberValidator #

Convenience method for creating a new JSONScheam::NumberValidator without options.


[View source]
def object(&) : ObjectValidator #

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

[View source]
def object : ObjectValidator #

Convenience method for creating a new JSONSchema::ObjectValidator without options.


[View source]
def string(&) : StringValidator #

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

[View source]
def string : StringValidator #

Convenience method for creating a new JSONSchema::StringValidator without options.


[View source]