Add webhook templating (#23289)
Co-authored-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
parent
a80efb449e
commit
4eda233e09
10 changed files with 134 additions and 11 deletions
|
@ -11,6 +11,7 @@
|
|||
# enabled :boolean default(TRUE), not null
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# template :text
|
||||
#
|
||||
|
||||
class Webhook < ApplicationRecord
|
||||
|
@ -30,6 +31,7 @@ class Webhook < ApplicationRecord
|
|||
validates :events, presence: true
|
||||
|
||||
validate :validate_events
|
||||
validate :validate_template
|
||||
|
||||
before_validation :strip_events
|
||||
before_validation :generate_secret
|
||||
|
@ -49,7 +51,18 @@ class Webhook < ApplicationRecord
|
|||
private
|
||||
|
||||
def validate_events
|
||||
errors.add(:events, :invalid) if events.any? { |e| !EVENTS.include?(e) }
|
||||
errors.add(:events, :invalid) if events.any? { |e| EVENTS.exclude?(e) }
|
||||
end
|
||||
|
||||
def validate_template
|
||||
return if template.blank?
|
||||
|
||||
begin
|
||||
parser = Webhooks::PayloadRenderer::TemplateParser.new
|
||||
parser.parse(template)
|
||||
rescue Parslet::ParseFailed
|
||||
errors.add(:template, :invalid)
|
||||
end
|
||||
end
|
||||
|
||||
def strip_events
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue