abstract class Marten::Schema
- Marten::Schema
- Reference
- Object
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.crmarten/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
-
.fields
Returns all the fields instances associated with the current schema.
-
.get_field(id : String | Symbol)
Allows to retrieve a specific field instance associated with the current schema.
Macro Summary
-
field(*args, **kwargs)
Allows to define a schema field.
Instance Method Summary
-
#[](field_name : String | Symbol)
Returns the bound field associated with the passed field name or raises if the field is not present.
-
#[]?(field_name : String | Symbol)
Returns the bound field associated with the passed field name or
nil
if the field is not present. -
#get_field_value(field_name : String | Symbol)
Allows to read the value of a specific field.
- #validated_data : Hash(String, Bool | Float64 | Int64 | JSON::Any | JSON::Serializable | Marten::HTTP::UploadedFile | String | Time | Time::Span | UUID | Nil)
Macros inherited from module Marten::Template::Object
template_attributes(*names)
template_attributes
Macros inherited from module Marten::Core::Validation::Callbacks
after_validation(*names)
after_validation,
before_validation(*names)
before_validation
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
Macros inherited from module Marten::Core::Validation
validate(*names)
validate
Constructor Detail
Class Method Detail
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
.
Macro Detail
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
Instance Method Detail
Returns the bound field associated with the passed field name or raises if the field is not present.
Returns the bound field associated with the passed field name or nil
if the field is not present.
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.