• Stars
    star
    13
  • Rank 1,465,120 (Top 30 %)
  • Language
    Crystal
  • License
    MIT License
  • Created over 5 years ago
  • Updated almost 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Validations module for Crystal ✅

Validations

Built with Crystal Build status API Docs Releases Awesome vladfaust.com Patrons count Gitter chat

A validations module for Crystal.

Supporters

Thanks to all my patrons, I can continue working on beautiful Open Source Software! 🙏

Lauri Jutila, Alexander Maslov, Dainel Vera, Anton Yordanov

You can become a patron too in exchange of prioritized support and other perks

Become Patron

Installation

Add this to your application's shard.yml:

dependencies:
  validations:
    github: vladfaust/validations.cr
    version: ~> 0.2.0

This shard follows Semantic Versioning 2.0.0, so see releases and change the version accordingly.

Usage

This shards allows validation composition (i.e. inclusion of modules with custom validations and rules).

require "validations"

module CustomValidations
  include Validations

  rule :email do |attr, value, rule|
    invalidate(attr, "must be an email") unless /@/.match(value)
  end
end

struct User
  include Validations
  include CustomValidations

  property name : String
  property email : String
  @age : UInt8?
  @nilable : String?

  def initialize(@name, @email, @age : UInt8? = nil, @nilable : String? = nil)
  end

  validate name, size: (1..16), presence: true
  validate email, size: (6..64), email: true
  validate @age, gte: 18

  # Will not be run if `@nilable.nil?`
  validate @nilable, size: (5..10)

  # Custom validations are allowed
  def validate
    previous_def
    invalidate("name", "must not be equal to Vadim") if name == "Vadim"
  end
end

user = User.new("Vadim", "e-mail", 17)
user.valid? # false
pp user.invalid_attributes
# {
#   "name" => ["must have size in (1..16)", "must not be equal to Vadim"],
#   "email" => ["must have size in (6..64)", "must be an email"],
#   "@age" => ["must be greater than or equal to 18"]
# }

List of currently implemented rules

  • is: Object - check if attribute == object
  • presence: Bool - check unless attribute.nil?
  • gte: Comparable - check if attribute >= comparable
  • lte: Comparable - check if attribute <= comparable
  • gt: Comparable - check if attribute > comparable
  • lt: Comparable - check if attribute < comparable
  • in: Enumerable - check if enumerable.includes?(attribute)
  • size: Enumerable - check if enumerable.includes?(attribute.size)
  • size: Int - check if attribute.size == int
  • regex: Regex - check if regex.match(attribute)

Rules which need to be explicitly required

Some rules are not required with require "validations", you have to require them explicitly.

  • require "validations/rules/scheme" (related to URI scheme)
    • scheme: String - check if attribute.scheme == string
    • scheme: Enumerable - check if enumerable.includes?(attribute.scheme)

Contributing

  1. Fork it (https://github.com/vladfaust/validations.cr/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

More Repositories

1

unity-wakatime

WakaTime plugin for Unity ⏱
C#
116
star
2

crystalworld

RealWorld back-end API implementation 👍
Crystal
43
star
3

migrate.cr

A database migration solution 🚜
Crystal
31
star
4

i18n.cr

Internationalization shard 🌍
Crystal
24
star
5

jbuilder-json_api

Jbuilder meets jsonapi.org specifications
Ruby
23
star
6

http-multiserver.cr

Mount multiple web applications 🚦
Crystal
23
star
7

http-params-serializable

The HTTP params parsing module for Crystal 🤓
Crystal
20
star
8

tarantool.cr

The Tarantool driver (a.k.a. connector) 🕷
Crystal
19
star
9

mini_redis

A light-weight low-level Redis client for Crystal ♨️
Crystal
18
star
10

tele.cr

A convenient Telegram Bot framework 🤖
Crystal
13
star
11

callbacks.cr

Expressive callbacks module for Crystal 🚉
Crystal
12
star
12

background

Fast background job processing
Crystal
11
star
13

stripe.cr

🚧 WIP 🚧 Stripe API wrapper 💳
Crystal
7
star
14

onyx-http-deprecated

Deprecated Onyx module
Crystal
6
star
15

cake-bake

Bake Cakefile into native Crystal code 🍞
Crystal
5
star
16

time-span-humanize

Time::Span#humanize method
Crystal
5
star
17

crack

Alternative Crystal HTTP server implementation
Crystal
5
star
18

timer.cr

A versatile timer module ⏲
Crystal
4
star
19

realworld-benchmark

RealWorld Benchmark
Crystal
3
star
20

onyx-40-loc-distributed-chat

Distributed websocket chat in 40 lines of code
JavaScript
2
star
21

time_format.cr

Time spans formatting made simple ⌚️
Crystal
2
star
22

Expense-Manager-2

Java
1
star
23

attribute_enum

Rails-like enums with ease
Ruby
1
star
24

tele-broadcast.cr

Broadcasting for Tele 📢
Crystal
1
star