SpicyValidation
Generate validation methods automatically from database schema.
[important notice] Your model file will be overwritten!
Installation
Add this line to your application's Gemfile:
gem 'spicy_validation'
And then execute:
$ bundle install
Or install it yourself as:
$ gem install spicy_validation
Usage
- Run
validation:generate
task - Type a number that you would like to generate validation
% rails validation:generate
[warning] If you generate validation, model files will be overwritten.
{:"0"=>"samples", :"1"=>"users"}
Type a number you wanna generate validation > ex) 0
Example
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| message | varchar(255) | YES | | NULL | |
| age | int(11) | NO | | NULL | |
| score | int(11) | YES | | NULL | |
| premium | tinyint(1) | YES | | NULL | |
| created_at | datetime(6) | NO | | NULL | |
| updated_at | datetime(6) | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
# app/models/user.rb
class User < ApplicationRecord
validates :name, presence: true
validates :age, presence: true, numericality: true
validates :score, numericality: true, allow_nil: true
end
License
The gem is available as open source under the terms of the MIT License.
Acknowledgement
This repository based on https://github.com/sinsoku/pretty_validation. See the file headers for detail informations.