Why not create a presenter instead? You could handle any errors with syntax if you needed to, but you could be more flexible with your data.
Let’s say your data was products in a store.
class Product
attr_accessor :name, :description
# Defaults to zero if no price is given
def price
@price || 0
end
# Only accepts numbers
def price=(n)
raise "#{n} is not a number" unless n.is_a?(Fixnum)
@price = n
end
# Returns a list of all products (you may want to memoize this)
def self.all
data.products.map do |data|
self.from_data(data)
end
end
# Construct a new product object from YAML data
def self.from_data(data)
self.new.tap do |product|
product.name = data[:name]
product.description = data[:description]
product.price = data[:price]
end
end
end
There are a number of advantages; easier to test, easier to set defaults, choice over how you want to parse each attribute.
<h1>Products</h1>
<% Product.all.each do |product| %>
<h2><%= product.name %></h2>
<h3>$<%= product.price %></h3>
<p><%= product.description %></p>
<% end %>
If you really wanted to just check the YAML, there are two approaches you could take. You could have a Middleman extension that checks when the data file changes, or you could have a separate tool that can check on demand, like a lint-checker. The latter is probably easier. Have it run just before building.