fix: validate template_params for WhatsApp (#6881)
- Add JsonSchemaValidator, which takes a declarative schema and validates it for a given property. - Add specs for JsonSchemaValidator - Enable the validator for template_params
This commit is contained in:
@@ -37,12 +37,33 @@ class Message < ApplicationRecord
|
||||
include Liquidable
|
||||
NUMBER_OF_PERMITTED_ATTACHMENTS = 15
|
||||
|
||||
TEMPLATE_PARAMS_SCHEMA = {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'template_params': {
|
||||
'type': 'object',
|
||||
'properties': {
|
||||
'name': { 'type': 'string' },
|
||||
'category': { 'type': 'string' },
|
||||
'language': { 'type': 'string' },
|
||||
'namespace': { 'type': 'string' },
|
||||
'processed_params': { 'type': 'object' }
|
||||
},
|
||||
'required': %w[name category language namespace processed_params]
|
||||
}
|
||||
}
|
||||
}.to_json.freeze
|
||||
|
||||
before_validation :ensure_content_type
|
||||
|
||||
validates :account_id, presence: true
|
||||
validates :inbox_id, presence: true
|
||||
validates :conversation_id, presence: true
|
||||
validates_with ContentAttributeValidator
|
||||
validates_with JsonSchemaValidator,
|
||||
schema: TEMPLATE_PARAMS_SCHEMA,
|
||||
attribute_resolver: ->(record) { record.additional_attributes }
|
||||
|
||||
validates :content_type, presence: true
|
||||
validates :content, length: { maximum: 150_000 }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user