-
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.
Merge pull request #1715 from chef/robb/validate-cookbook-version-aga…
…inst-chef Fix version validation against chef's required format
- Loading branch information
Showing
9 changed files
with
126 additions
and
99 deletions.
There are no files selected for viewing
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
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
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