-
Notifications
You must be signed in to change notification settings - Fork 0
Validators
You can define 3 kind of validators:
These validators will run against your xls/csv file.
The built in file validators are:
-
File format *
class SuperheroesParser < Parxer::XlsParser validate_file(:file_format, allowed_extensions: [:xls, :xlsx]) # [:csv] with CsvParser end
If you are working with an xls parser and try to upload for example a .doc file, you will get an error.
-
File presence *
class SuperheroesParser < Parxer::XlsParser validate_file(:file_presence) end
If you do not upload the file, you will get an error.
-
Rows count
class SuperheroesParser < Parxer::XlsParser validate_file(:rows_count, max: 50) end
If you exceed the number of rows to update (50), you will get an error.
-
Header order
class SuperheroesParser < Parxer::XlsParser validate_file(:header_order) end
The order is defined by the execution order of the
column
method. If you have for example:class SuperheroesParser < Parxer::XlsParser validate_file(:header_order) column(:name, name: "Name") column(:super_power, name: "Super Power") column(:publisher, name: "Publisher") end
You will need to upload a xls file with "Name" as first column, "Super Power" as second and "Publisher" as third or you will get an error.
Validators marked with * are included by default in
XlsParser
andCsvParser
parsers.
These validators will run against each cell of your xls/csv file and need to be defined in column
context.
The built in file validators are:
-
Presence
class SuperheroesParser < Parxer::XlsParser column(:name, name: "Name") do validate(:presence) end end
Following the previous example, if you have an empty cell under the "Name" column you will get an error.
-
Boolean
class SuperheroesParser < Parxer::XlsParser column(:alive, name: "Alive") do validate(:boolean) end end
According to the previous example, if you have a cell under the "Alive" column with a value different from any of the following:
true, "true", "t", "1", false, "false", "f", "0"
you will get an error. -
Email
class SuperheroesParser < Parxer::XlsParser column(:email, name: "E-Mail") do validate(:email) end end
Following the previous example, if you have an invalid email under the "E-mail" column you will get an error.
-
URL
class SuperheroesParser < Parxer::XlsParser column(:site_url, name: "Site URL") do validate(:url) end end
Following the previous example, if you have an invalid URL under the "Site URL" column you will get an error.
-
Chilean RUT
class SuperheroesParser < Parxer::XlsParser column(:rut, name: "RUT") do validate(:rut) end end
Following the previous example, if you have an invalid RUT under the "RUT" column you will get an error.
-
Inclusion
class SuperheroesParser < Parxer::XlsParser column(:publisher, name: "Publisher") do validate(:inclusion, in: ["Marvel", "DC"]) end end
According to the previous example, if you have a cell under the "Publisher" column with a value different from any of the following:
"Marvel", "DC"
you will get an error. -
Datetime
class SuperheroesParser < Parxer::XlsParser column(:created_at, name: "Created At") do validate(:datetime, options) end end
Options:
-
format
: to ensure specific date/time format. If you have for example:validate(:datetime, format: "%Y%M")
, you will need to provide "Created At" values like this:"201812"
. -
gt
: to ensure date/times greater than the given value. For example:validate(:datetime, gt: DateTime.current)
-
gteq
: to ensure date/times greater than or equal to the given value. -
lt
: to ensure date/times lower than the given value. -
lteq
: to ensure date/times lower than or equal to the given value.
-
-
Number
class SuperheroesParser < Parxer::XlsParser column(:age, name: "Age") do validate(:number, options) end end
Options:
-
only_integer
: to ensure Integer values. If you have for example:validate(:number, only_integer: true)
-
allow_negative
: set to false if you want to work with positive values only. If you have for example:validate(:number, allow_negative: false)
-
gt
,gteq
,lt
andlteq
: the same as with the dates.
-
These validators will run after Column Validators if all the cells in the row are valid.
class SuperheroesParser < Parxer::XlsParser
column(:name, name: "Name")
validate(:presence)
end
column(:real_name, name: "Real Name") do
validate(:presence)
end
validate_row(:odd_chars, options) do
(row.name + row.real_name).delete(" ").size.odd?
end
end
According to the previous example, the custom odd_chars
row validator will run after running presence column validators defined for name and real_name columns. Inside the validate_row
's block you need to define a conditional.
You can access row attributes using the
row
method if you need.
Options:
-
if_valid
: by default row validators will run if all the cells in the row are valid. But, you can set theif_valid
option execute the validator if one or more cells are valid. For example:validate_row(:odd_chars, if_valid: [:name, :real_name])
. So, on this case, the validator will run if "Name" and "Real Name" are valid.
You can define 2 kind of custom validators:
-
Inline Validators:
This kind of validators take a name (:odd_string) and a block. The block needs to hold a conditional. For example:
class SuperheroesParser < Parxer::XlsParser column(:name, name: "Name") validate(:odd_string) do value.to_s.odd? end end end
-
Reusable Validators:
If you want to reuse a validator, you can create one like this:
module Parxer module Validator class OddString < Base def validate value.to_s.odd? end end end end
And then you can use it like this:
class SuperheroesParser < Parxer::XlsParser column(:name, name: "Name") validate(:odd_string) end column(:publisher, name: "Publisher") do validate(:odd_string) end end
You can use Custom validators with row and file validators too