class

Marten::Schema

Inherits Marten::Template::Object / Marten::Template::CanDefineTemplateAttributes / Marten::Core::Validation::Callbacks / Marten::Core::Validation / Reference / Object

Abstract base schema class.

Schemas allow to define how a data set should be validated and what fields are expected. Schemas can be used to validate query parameters, forms data, files, or JSON objects.

Constructors

new(data : AnyDataHash, initial : AnyDataHash = DataHash.new)
Source

Class methods

fields

Returns all the fields instances associated with the current schema.

Source
get_field(id : String | Symbol)

Allows to retrieve a specific field instance associated with the current schema.

The returned object will be an instance of a subclass of Marten::Schema::Field::Base.

Source

Instance methods

[](field_name : String | Symbol)

Returns the bound field associated with the passed field name or raises if the field is not present.

Source
[]?(field_name : String | Symbol)

Returns the bound field associated with the passed field name or nil if the field is not present.

Source
data
Source
get_field_value(field_name : String | Symbol)

Allows to read the value of a specific field.

This methods returns the value of the field corresponding to field_name. If the passed field_name doesn't match any existing field, a Marten::Schema::Errors::UnknownField exception is raised.

Source
initial
Source
validated_data
Source

Macros

field(*args, **kwargs)

Allows to define a schema field.

At least two positional arguments are required when defining schema fields: a field identifier and a field type. Depending on the considered field types, additional keyword arguments can be used in order to customize on the field behaves and how it handles validations:

class MySchema  < Marten::Schema
  field :my_field, :string, required: true, max_size: 128
end
Source

Nested types