Tóth Péter

Joined

10 Experience
0 Lessons Completed
0 Questions Solved

Activity

Posted in API params validation in Rails app

How about ActiveModel::Validations?

class MyValidator
  include ActiveModel::Validations

  attr_reader :data

  validates_presence_of :name

  def initialize(data)
    @data = data || {}
  end

  def read_attribute_for_validation(key)
    data[key]
  end
end

MyValidator.new(name: '').valid? # => false

I couldn't find the etiquette guide about external links, but you can find more info about a nested case with Rails integration and tests here

Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

© 2024 GoRails, LLC. All rights reserved.