如何验证text_area的输入值是JSON格式?

时间:2015-06-10 16:42:24

标签: ruby-on-rails ruby-on-rails-4

我正在使用带有Ruby 2.1.5的rails 4.2

这是我的描述模型:

class Description < ActiveRecord::Base
  validates :value, presence: true
  belongs_to :api
end

我可以做些什么来验证值的输入是JSON格式?

1 个答案:

答案 0 :(得分:2)

添加一个自定义验证方法,该方法将解析// Step 0: We have a collection of students as an input. Collection<OlccStudent> students = ...; students.stream() // Step 1: Validate each student, keeping a track of any error message generated. .collect(Collectors.toMap(Function.identity(), student -> validate(student))) // Step 2: Keep only those that have an error message associated. .entrySet() .stream() .filter(entry -> entry.getValue() != null); // Step 3: Generate a Map. .collect(Collectors.toMap(entry -> entry.getKey(), entry -> entry.getValue())); 。只是解救异常并在解析失败时添加错误。

value