Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand validation docs to include new validators and add examples #80

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions en/docs/validations.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Validations
## Introduction to Validations

Within a model class, you can setup validations. Validations let you restrict the types of data that can be stored in a model. Validations are mostly useful for the ```store``` collection, though they can be used elsewhere.

Expand All @@ -9,12 +10,14 @@ At the moment, we have the following validations (with more on the way):
- email
- format
- numericality
- inclusion
- phone number
- unique
- type

See [this folder](https://github.com/voltrb/volt/tree/master/lib/volt/models/validators) for more info on the validators.

Validators should be called at the top of your model classes, and generally have this format:
```ruby
class Info < Volt::Model
validate :name, length: 5
Expand All @@ -30,6 +33,84 @@ When ```save!``` on a buffer with validations is called, the following occurs:
3. If all validations pass, the data is saved to the database and the promise is resolved on the client.
4. The data is synced to all other clients.

## Validation Examples
- length
```ruby
class Info < Volt::Model
# Pass a number for the minimum length
validate :first_name, length: 5
# or
# pass an array with min and max
validate :last_name, length: {minimum: 5, maximum: 10}
end
```

- present
```ruby
class Info < Volt::Model
validate :name, present: true
end
```

- email
```ruby
class Info < Volt::Model
validate :email, email: true
end
```

- format
```ruby
class Info < Volt::Model
# takes an hash with keys with: and message:.
validate :name, format: { with: /.+@.+/, message: 'must include an @ symbol' }
end
```

- numericality
```ruby
class Info < Volt::Model
# Pass true to only validate it's a number
validate :name, numericality: true
# Pass a hash with :gt, :lt, :lte, or :gte to specify a range
validate :name, numericality: {gt: 5, lte: 10}
end
```

- inclusion
```ruby
class Info < Volt::Model
# pass an array to validate inclusion in the array
validate :name, inclusion: ['red', 'blue', 'green', 'purple']
# or specify a custom message by passing a hash with in: and message: keys
validate :name, inclusion: {in: ['red', 'blue', 'green', 'purple'], message: 'Must be in the list of colors' }
end
```

- phone number
```ruby
class Info < Volt::Model
# pass true to validate U.S.-style phone numbers
validate :name, phone_number: true
# or pass a hash with with: and message: keys
validate :phone, phone_numbe: {with: /\d-\d+/, message: 'Must be a phone number in the format...'}
end
```

- unique
```ruby
class Info < Volt::Model
validate :name, unique: true
end
```

- type
```ruby
class Info < Volt::Model
validate :name, String #Valid options are currently String and Numeric. Volt::Boolean is available but unsupported.
end
```

## Custom Validations

You can create a one off custom validation by passing a block to validate:
Expand Down