-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
validate a cookbook version against chef's required format
A version for a cookbook must be either X.Y.Z or X.Y.[1] Up until this change, the validation for a CookbookVersion's version was against SemVer rules. SemVer allows for release labels or build info to appear after the version number itself. Chef does not! Here, we add a new ChefVersionValidator that reuses the existing version validation logic of the Chef gem in ::Chef::Version. Supermarket is already relying on the Chef gem to validate version constraints set for cookbook dependencies and supported platform. Fixes #1714 to return an error to any client if a cookbook is uploaded with an invalid version. [1] https://docs.chef.io/cookbook_versions.html#constraints Signed-off-by: Robb Kidd <[email protected]>
- Loading branch information
Showing
4 changed files
with
97 additions
and
42 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/supermarket/app/lib/active_model/validations/chef_version_validator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
require 'active_model' | ||
require 'chef/version_class' | ||
require 'chef/exceptions' | ||
|
||
module ActiveModel | ||
module Validations | ||
# | ||
# Validates that strings are formatted as Chef version constraints. | ||
# | ||
class ChefVersionValidator < ActiveModel::EachValidator | ||
# | ||
# Create a new validator. Called implicitly when | ||
# a +chef_version+ validation is added to an attribute. | ||
# | ||
# @param options [Hash] validation options | ||
# @option options [String] :message | ||
# ('is not a valid Chef version') a custom validation message | ||
# | ||
def initialize(options) | ||
options.fetch(:message) do | ||
options.store(:message, 'is not a valid Chef version') | ||
end | ||
|
||
super(options) | ||
end | ||
|
||
# | ||
# Add an error to +attribute+ of +record+ if the given +value+ is not | ||
# a valid Chef version constraint | ||
# | ||
# @param record [ActiveModel::Model] | ||
# @param attribute [Symbol] | ||
# @param value | ||
# | ||
def validate_each(record, attribute, value) | ||
Chef::Version.new(value) | ||
rescue Chef::Exceptions::InvalidCookbookVersion => e | ||
msg = "#{options.fetch(:message)}. #{e.class}: #{e.message}" | ||
record.errors.add(attribute, msg, value: value) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters