abstract class Marten::Schema

Overview

Abstract base schema class.

Schemas allows 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.

Included Modules

Defined in:

marten/schema.cr
marten/schema/bound_field.cr
marten/schema/errors.cr
marten/schema/field.cr
marten/schema/field/base.cr
marten/schema/field/bool.cr
marten/schema/field/date.cr
marten/schema/field/date_time.cr
marten/schema/field/duration.cr
marten/schema/field/email.cr
marten/schema/field/file.cr
marten/schema/field/float.cr
marten/schema/field/int.cr
marten/schema/field/json.cr
marten/schema/field/slug.cr
marten/schema/field/string.cr
marten/schema/field/url.cr
marten/schema/field/uuid.cr
marten/template/ext/schema.cr

Constructors

Class Method Summary

Instance Method Summary

Macro Summary

Instance methods inherited from module Marten::Core::Validation

errors : ErrorSet errors, invalid?(context : Nil | String | Symbol = nil) invalid?, valid?(context : Nil | String | Symbol = nil) valid?, validate validate

Constructor Detail

def self.new(data : AnyDataHash, initial : AnyDataHash = DataHash.new) #

[View source]

Class Method Detail

def self.fields #

Returns all the fields instances associated with the current schema.


[View source]
def self.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.


[View source]

Instance Method Detail

def [](field_name : String | Symbol) #

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


[View source]
def []?(field_name : String | Symbol) #

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


[View source]
def 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.


[View source]
def validated_data : Hash(String, Bool | Float64 | Int64 | JSON::Any | JSON::Serializable | Marten::HTTP::UploadedFile | String | Time | Time::Span | UUID | Nil) #

[View source]

Macro Detail

macro 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

[View source]