Apipie/apipie-rails — pull request #259 by mourad-ifeelgoods (2014-06-25 16:32:06+00:00)
Title: Revamp array validator

Introduce a new ApiPie Array validator for more flexible validations
The following validation are currently supported
* Validates all items of an array has the specified type
* Validates all items are in a set of values

**exemple 1**
Assert `hits` must be an array of integer value
```ruby
param :hits, Array, of: Integer
```

**exemple 2**
Assert each items of `colors` to be in the enumeration
```ruby
param :colors, Array, enum: [ "red", "green", "blue"]
````

**exemple 3**
The load of enum can be deferred until needed. this useful when the enum may changes
```ruby
param :colors, Array, enum: -> { Color.all.pluck(:name) }
````

**exemple 4**
Assert `things` is an array of any type
```ruby
param :things, Array
````

Notes
=====

This is a first try for a more consistent validator dsl. I want a uniform way to express constraints on parameters. The validators are build around a set of core types `String`, `Number`, `Boolean`, `Array` and `Hash`

```ruby
param :id, :integer
param :title, :string
param :draft :boolean
param :tags: :array
param :author, :hash do
param :name, :string
end
```

it would would simple to extend our validations by adding additional constraints

```ruby
param :id, :integer, min_value: 1
param :title, :string
param :status, :string, enum: %w(draft proposed published)
param :tags: :array, of: :string, max_term: 10, unique: true
param :author, :hash do
param :name, :string
param :email, :string, pattern: /\A\S+@.+\.\S+\z/
end
```

or by defining custom validators.

```ruby
param :address, :email
```

The validator class would be found by naming convention

```ruby
class EmailValidator < Apipie::Validator
def validate(param, value)
value ~= /\A\S+@.+\.\S+\z/
end
end
```