https://github.com/ragmaanir/vagt
Crystal validations
https://github.com/ragmaanir/vagt
crystal validation validation-library
Last synced: 12 months ago
JSON representation
Crystal validations
- Host: GitHub
- URL: https://github.com/ragmaanir/vagt
- Owner: Ragmaanir
- License: mit
- Created: 2023-08-30T16:26:59.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-08-30T16:27:38.000Z (over 2 years ago)
- Last Synced: 2025-03-30T18:46:32.045Z (12 months ago)
- Topics: crystal, validation, validation-library
- Language: Crystal
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# vagt
Simple validations for objects.
## Installation
1. Add the dependency to your `shard.yml`:
```yaml
dependencies:
vagt:
github: ragmaanir/vagt
```
2. Run `shards install`
## Usage
```crystal
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
1. Fork it ()
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
- [Ragmaanir](https://github.com/ragmaanir) - creator and maintainer