README.md in va-0.0.5 vs README.md in va-0.1.0

- old
+ new

@@ -1,13 +1,107 @@ # Va -Va is a minimalistic, framework agnostic, validation library. +Va is a minimalistic validation library. It is meant to be used as a first line of defense from external imputs. +Va doesnn't care what framework you're using, it just provides a good way to set and validate attributes in a model. + ## Usage -TODO: Write usage instructions here +### Attributes + +Say you need a signup form validator that only allows an email, a password and the password confirmation. +The first thing to do is to create the corresponding model + +```ruby + class Signup < Va::Model + attribute :email + attribute :password + attribute :password_confirmation + end +``` + +And then you can instantiate it like this: + +```ruby + s = Signup.new(email: "fede@example.com", password: "123456", password_confirmation: "123456") + # => #<Signup:0x00000001cc90d8 + # @attributes= + # {:email=>"fede@example.com", + # :password=>"123456", + # :password_confirmation=>"123456"}> +``` + +As you can see, you can use either Strings or Symbols as keys (to be fair, it allows any object that responds to the `#to_sym` method) + +You can check the values of the attributes using the `#attributes` method. + +```ruby + s.attributes + # => {:email=>"fede@example.com", + # :password=>"123456", + # :password_confirmation=>"123456"} +``` + +If you miss an argument, it won't appear on the attributes list: + +```ruby + s = Signup.new(email: "fede@example.com", "password_confirmation" => "123456") + s.attributes + # => {:email=>"fede@example.com", + # :password_confirmation=>"123456"} +``` + +And if you pass a non-declared attribute, it will be ignored: + +```ruby + s = Signup.new(email: "fede@example.com", phone_number: "987654321") + # => #<Signup:0x000000015f8dd0 @attributes={:email=>"fede@example.com"}> +``` + +### Custom Validations + + +Up until here, we haven't talked about validations. + +Va allows you to write generic validations. + +For example, if we need the email to be present and the password and password validation to match, we can do it as it follows: + +```ruby + s = Signup.new(email: "fede@example.com", password: "a", password_confirmation: "a") + s.valid? + # => true + + t = Signup.new(password: "a", password_confirmation: "a") + t.valid? + # => false + + u = Signup.new(email: "fede@example.com", password: "a", password_confirmation: "b") + u.valid? + # => false +``` + +### Predefined Validations + +#### Blank + +If you want to validate that a field is not blank, you can just say: + +```ruby + class Person < Va::Model + attribute :name + attribute :age + validate_present(:name, :age) + end +``` + +And if any of those attributes is either nil or an empty string, the validation will fail. + +#### More Validations + +TODO: Add more validations ## Installation Add this line to your application's Gemfile: