vagt
Simple validations for objects.
Installation
-
Add the dependency to your
shard.yml
:dependencies: vagt: github: ragmaanir/vagt
-
Run
shards install
Usage
require "vagt"
class User
include Vagt::Validated
schema do
field name : String
field age : Int32
end
def initialize(@name, @age)
end
end
class UserValidator
include Vagt::Validator(User)
BLACKLIST = %w{Admin admin null delete}
validate :name, format: /\A[a-zA-Z]+\z/, size: (2..64), blacklist: BLACKLIST
validate :age, range: (13..150)
end
u = User.new("Admin", 300)
e = UserValidator.call(u).not_nil!
assert e["name"].map(&.name) == ["blacklist"]
assert e["age"].map(&.name) == ["range"]
assert e.to_json == <<-JSON
{
"name": "object_invalid",
"attributes": {},
"errors": {
"name": [
{
"name": "blacklist",
"attributes": {}
}
"age": [
{
"name": "range",
"attributes": {}
}
]
}
}
JSON
Contributing
- Fork it (https://github.com/ragmaanir/vagt/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- Ragmaanir - creator and maintainer