SlackValidBlockKit
A tool for building Slack Block Kit with validation.
Installation
Add this line to your application's Gemfile:
gem 'slack_valid_block_kit'And then execute:
$ bundle
Or install it yourself as:
$ gem install slack_valid_block_kit
Usage
- surfaces.rb contains the View objects
- layouts.rb contains the Layout blocks
- elements.rb contains the Block elements
- compositions.rb contains the Message composition objects
You can build views by SlackValidBlockKit.build or SlackValidBlockKit.blocks.
Both methods run validation your views automatically.
# Build modal with validation
modal = SlackValidBlockKit.build do |b|
b.modal(...)
end
# Build blocks with validation
blocks = SlackValidBlockKit.blocks do |b|
[...]
endIf you want to skip validations, you write as follows.
# Build modal without validation
modal = SlackValidBlockKit.build(validate: false) do |b|
b.modal(...)
end
# Build blocks without validation
blocks = SlackValidBlockKit.blocks(validate: false) do |b|
[...]
endor you can set to skip validations as config
SlackValidBlockKit::Config.skip_validation = trueFor example, when you ride on Rails, you can set in config/initializers/slack_valid_block_kit.rb.
SlackValidBlockKit::Config.skip_validation = Rails.env.production?You can just use validations.
modal = ... # create a modal hash without SlackValidBlockKit
SlackValidBlockKit.validate!(modal) # raise an exception with the invalid input
blocks = ... # create a blocks array without SlackValidBlockKit
SlackValidBlockKit.validate_blocks!(blocks) # raise an exception with the invalid inputExamples
Modal example with validation
require 'slack_valid_block_kit'
require 'json'
modal = SlackValidBlockKit.build do |b|
b.modal(
title: b.plain_text(text: "Modal title"),
blocks: [
b.section(
text: b.mrkdwn(text: "It's Block Kit...but _in a modal_"),
block_id: "section1",
accessory: b.button(
text: b.plain_text(text: "Click me"),
action_id: "button_abc",
value: "Button value",
style: "danger"
)
)
],
close: b.plain_text(text: "Cancel"),
submit: b.plain_text(text: "Save"),
private_metadata: "Shhhhhhhh",
callback_id: "view_identifier_12"
)
end
puts modal.to_jsonYou can get a just blocks array (with validation).
require 'slack_valid_block_kit'
require 'json'
blocks = SlackValidBlockKit.blocks do |b|
[
b.divider,
b.section(
text: b.mrkdwn(text: "*<fakeLink.toHotelPage.com|Windsor Court Hotel>*\n★★★★★\n$340 per night\nRated: 9.4 - Excellent"),
accessory: b.image(
image_url: "https://api.slack.com/img/blocks/bkb_template_images/tripAgent_1.png",
alt_text: "Windsor Court Hotel thumbnail"
)
),
b.context(
elements: [
b.image(
image_url: "https://api.slack.com/img/blocks/bkb_template_images/tripAgentLocationMarker.png",
alt_text: "Location Pin Icon"
),
b.plain_text(text: "Location: Central Business District", emoji: true)
]
)
]
end
puts blocks.to_jsonDevelopment
After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/slack_valid_block_kit. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the SlackValidBlockKit project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

