Initial commit
This commit is contained in:
21
app/models/concerns/data_is_json_validator.rb
Normal file
21
app/models/concerns/data_is_json_validator.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Data json validation
|
||||
class DataIsJsonValidator < ActiveModel::EachValidator
|
||||
def validate_each(record, attribute, data)
|
||||
return if data.nil?
|
||||
|
||||
unless validate_data_is_json!(data)
|
||||
record.errors.add(attribute, :invalid_format, message: 'data is not json compatible string')
|
||||
end
|
||||
end
|
||||
|
||||
def validate_data_is_json!(data)
|
||||
begin
|
||||
JSON.parse(data)
|
||||
true
|
||||
rescue JSON::ParserError => e
|
||||
false
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user