The CFInputValidator - Cloud Factory Input Validator is a simple gem to validate the spreadsheet data inputs as per the given rules.
This gem is being developed for Project Cloud Factory Copyright© Sprout Technology International Limited
In Gemfile source 'http://rubygems.org' gem 'unit_validator'
bundle install
OR gem install unit_validator
require 'rubygems'
require 'unit_validator'
The CFInputValidator checks if the headers of the provided spreadsheet matches with the corresponding rules (checked by label) and gives error message if it doesn't match.
The individual row of the spreadsheet undergoes the validation process and gives the final output as an array of valid and invalid units. Later the valid and invalid units can be processed separately as required.
You can even check if a file format is supported by the gem.
The gem currently supports validation_format
of email
, number
, url
, date
and time
only.
It also checks if value is present if required is true.
-
Specify the rules, i.e
:label
:required
:validation_format
rules = [{:label => "company name",:required => true, :validation_format => "general"}, {:label => "email", :required => false, :validation_format => "email"}] -
Provide data for validation: inputs = "company name,email\nSprout,[email protected]\nApple,saroj@apple"
You can also give the file location as input inputs = File.read("fixtures/gdoc.csv")
-
Check the file format with
check_extension(path)
method which gives you a 'boolean' value:check
file_location = "fixtures/gdoc.xlsxsfdsf"
CFInputValidator.check_extension(file_location)[:check]
-
This gem currently supports the following formats and 'must be converted into csv' before validation.
inputs = File.read("fixtures/gdoc.csv")
inputs = Excel.new("fixtures/gdoc.xls").to_csv
inputs = Excelx.new("fixtures/gdoc.xlsx").to_csv
inputs = Openoffice.new("fixtures/gdoc.ods").to_csv
-
-
Create a new constructor of CF::InputValidator class passing the above rules as argument.
cloud_validator = CF::InputValidator.new(rules)
-
Call
parse_and_validate(data)
method passing the above inputs as arguments. val = cloud_validator.parse_and_validate(inputs) -
Now you'll get both valid and invalid data in
:valid_units
and:invalid_units
arrays respectively.val[:valid_units]
val[:invalid_units]
-
It will give a
.errors
instance variable as error if the headers of spreadsheet data doesn't match with the rules or vice-versa saying "Headers doesnot match the column counts". rules = [{:field_id => "field_1", :label => "company name", :field_type => "text_data", :value => "Sprout", :required => true, :validation_format => "general" }] inputs = "company name,email\nSprout,[email protected]"cloud_validator = CF::InputValidator.new(rules) val = cloud_validator.parse_and_validate(inputs) err = val.errors
- Fork the project.
- Make your feature addition or bug fix.
- Add tests for it. This is important so I don't break it in a future version unintentionally.
- Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
- Send me a pull request. Bonus points for topic branches.
Copyright© Sprout Technology International Limited. See LICENSE for details.