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

Revamp array validator #259

Merged
merged 3 commits into from
Jul 26, 2014
Merged

Conversation

mourad-ifeelgoods
Copy link
Contributor

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

    example 1
    Assert hits must be an array of integer value

      param :hits, Array, of: Integer

    example 2
    Assert each items of colors to be in the enumeration

      param :colors, Array, in: [ "red", "green", "blue"] 

    example 3
    The load of enum can be deferred until needed. this useful when the enum may changes

      param :colors, Array, in: ->  { Color.all.pluck(:name) }

    example 4
    Assert things is an array of any type

       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

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

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.

param :address, :email

The validator class would be found by naming convention

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

The name ArrayValidator is a bit confusing as the type of the argument
value is not an array
@mtparet
Copy link
Contributor

mtparet commented Jun 26, 2014

Nice PR!

end

def self.build(param_description, argument, options={}, block)
if argument == Array && !block.is_a?(Proc)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be better to only allow lambda for code safety ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

block is eventually a block of nested param. it is not a lambda.
currently this validator does not support nested object we just skip this param. it will be handle by NestedValidator

@mtparet
Copy link
Contributor

mtparet commented Jul 2, 2014

@iNecas any thoughts on this PR ?

@iNecas
Copy link
Member

iNecas commented Jul 7, 2014

Nice one! Could you add some notes to the readme for the documentation purposes?

Validates that argument value is an array
It allow to specify additional validation on array elements via param
description options
@dali-ifg
Copy link

@iNecas do the readme updates match your expectation?

@iNecas
Copy link
Member

iNecas commented Jul 26, 2014

They do, thanks for the notification: would be nice if github notified about new commits in the PR,merging now.

iNecas added a commit that referenced this pull request Jul 26, 2014
@iNecas iNecas merged commit beb1dc3 into Apipie:master Jul 26, 2014
@mtparet mtparet deleted the revamp_array_validator branch July 26, 2014 20:03
@iNecas
Copy link
Member

iNecas commented Aug 8, 2014

The changes were released as part of the 0.2.3 release. Thanks for your contribution! Let the code be with you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants