docs/validation.md in flexirest-1.6.7 vs docs/validation.md in flexirest-1.6.8

- old
+ new

@@ -1,8 +1,8 @@ # *Flexirest:* Validation -You can create validations on your objects just like Rails' built in ActiveModel validations. For example: +Flexirest comes with its own validation. It is very similar to the Rails' built in ActiveModel validations. For example: ```ruby class Person < Flexirest::Base validates :first_name, presence: true #ensures that the value is present and not blank validates :last_name, existence: true #ensures that the value is non-nil only @@ -18,10 +18,11 @@ get :index, '/' end ``` + Note: the block based validation is responsible for adding errors to `object._errors[name]` (and this will automatically be ready for `<<` inserting into). Validations are run when calling `valid?` or when calling any API on an instance (and then only if it is `valid?` will the API go on to be called). `full_error_messages` returns an array of attributes with their associated error messages, i.e. `["age must be at least 18"]`. Custom messages can be specified by passing a `:message` option to `validates`. This differs slightly from ActiveRecord in that it's an option to `validates` itself, not a part of a final hash of other options. This is because the author doesn't like the ActiveRecord format (but will accept pull requests that make both syntaxes valid). To make this clearer, an example may help: @@ -80,9 +81,37 @@ - `:middle_name` - `:golf_score` - `:retirement_age` - `:favorite_authors` + +## ActiveModel::Validations + +This built-in validations have a bit different syntax than the ActiveModel validations and use a different codebase. + +You can opt-out from the built-in validations and use the `ActiveModel` validations instead if you inherit from the `Flexirest::BaseWithoutValidation` class instead of the `Flexirest::Base`. + +Here is the same example what you could see at the top but with `ActiveModel` validations: + + +```ruby +class Person < Flexirest::BaseWithoutValidation + include ActiveModel::Validations + + validates :first_name, :last_name, presence: true # ensures that the value is present and not blank + validates :password, length: { within: 6..12, message: "Invalid password length, must be 6-12 characters" } + validates :post_code, length: { minimum: 6, maximum: 8 } + validates :salary, numericality: { greater_than_or_equal_to: 20_000, less_than_or_equal_to: 50_000 } + validates :age, numericality: { greater_than_or_equal_to: 18, less_than_or_equal_to: 65 } + validates :suffix, inclusion: { in: %w{Dr. Mr. Mrs. Ms.} } + + validate do + errors.add(:name, "must be over 4 chars long") if first_name.length <= 4 + end + + get :index, '/' +end +``` ----- [< HTTP/parse error handling](httpparse-error-handling.md) | [Filtering result lists >](filtering-result-lists.md)