Skip to content

Releases: azutoolkit/schema

Release v0.2.7

10 Jun 14:22
bdab209
Compare
Choose a tag to compare
Update local_converter.cr

Release v0.2.6

10 Jun 13:11
0fa53a9
Compare
Choose a tag to compare
Create iso8601_converter.cr

Release v0.2.5

01 Jun 19:32
cdd64f8
Compare
Choose a tag to compare
Create time_converter.cr

Adds time converter for parsing time strings

Release v0.2.4

01 Jun 15:09
bc3ad2d
Compare
Choose a tag to compare
Update ext.cr (#13)

Adds missing require for epochtime converter module

Release v0.2.3

23 Mar 22:33
Compare
Choose a tag to compare
Set shard version 0.2.3

Release v0.2.2

18 Nov 22:32
Compare
Choose a tag to compare
includes required predicate

v0.2.1

17 Apr 22:34
Compare
Choose a tag to compare
  • Support Crystal 1.0.0

Better Schema API

26 Dec 16:12
1cffd0c
Compare
Choose a tag to compare

Changes

  • No longer uses custom param keyword to define schemas, simply create your objects as plain old crystal objects POCOs
  • Use either nested objects or objects defined elsewhere in the codebase.
  • No longer uses validation do ... end macro to define validations, simply use validate :field, ..{predicates}, message: {error message}
  • POCOs that include the Schema::Validation module will have a nested validator class. For example, a class User will have a User::Validator class this makes the validator reusable.
  • POCOs that include the Schema::Validation module will have:
    • errors runs the Validator.validate method with an instance of the class returns an Array(Schema::Error)
    • valid? checks if the errors are empty if empty then is valid
    • validate! checks if the errors are empty if empty then is valid or raises Schema::ValidationError

Examples

class Address
    include Schema::Definition

    getter street : String
    getter zip : String
    getter city : String
    getter location : Location
  end

  class Location
    include Schema::Definition

    getter longitude : Float64
    getter latitude : Float64
    getter useful : Bool
  end

struct Example
  include Schema::Definition
  include Schema::Validation

  getter email : String
  getter name : String
  getter age : Int32
  getter alive : Bool
  getter childrens : Array(String)
  getter childrens_ages : Array(Int32)
  getter address : Address
  
  # Custom Validators
  use EmailValidator, UniqueRecordValidator

  validate :email, match: /\w+@\w+\.\w{2,3}/, message: "Email must be valid!"
  validate :name, size: (1..20)
  validate :age, gte: 18, lte: 25, message: "Age must be 18 and 25 years old"
  validate :alive, eq: true
  validate :last_name, presence: true, message: "Last name is invalid"
  
  # Custom Predicates
  predicates do
    def some?(value : String, some) : Bool
      (!value.nil? && value != "") && !some.nil?
    end

    def if?(value : Array(Int32), bool : Bool) : Bool
      !bool
    end
  end
end

Example Custom Validators

class EmailValidator < Schema::Validator
  include Schema::Validators
  getter :record, :field, :message

  def initialize(@record : UserModel)
    @field = :email
    @message = "Email must be valid!"
  end

  def valid? : Array(Schema::Error)
    [] of Schema::Error
  end
end

class UniqueRecordValidator < Schema::Validator
  getter :record, :field, :message

  def initialize(@record : UserModel)
    @field = :email
    @message = "Record must be unique!"
  end

  def valid? : Array(Schema::Error)
    [] of Schema::Error
  end
end

Pre

26 Dec 01:40
Compare
Choose a tag to compare
Pre

Implements Self Validating Schemas for Crystal Objects.