class Csvlint::Field
Constants
- TYPE_VALIDATIONS
Attributes
constraints[R]
description[R]
name[R]
title[R]
Public Class Methods
new(name, constraints={}, title=nil, description=nil)
click to toggle source
# File lib/csvlint/field.rb, line 8 def initialize(name, constraints={}, title=nil, description=nil) @name = name @constraints = constraints || {} @uniques = Set.new @title = title @description = description reset end
Public Instance Methods
validate_column(value, row=nil, column=nil, all_errors=[])
click to toggle source
# File lib/csvlint/field.rb, line 17 def validate_column(value, row=nil, column=nil, all_errors=[]) reset unless all_errors.any?{|error| ((error.type == :invalid_regex) && (error.column == column))} validate_regex(value, row, column, all_errors) end validate_length(value, row, column) validate_values(value, row, column) parsed = validate_type(value, row, column) validate_range(parsed, row, column) if parsed != nil return valid? end
Private Instance Methods
build_regex_error(value, row, column, pattern, all_errors)
click to toggle source
# File lib/csvlint/field.rb, line 58 def build_regex_error(value, row, column, pattern, all_errors) return if @regex_error_exists build_errors(:invalid_regex, :schema, nil, column, ("#{name}: Constraints: Pattern: #{pattern}"), { "pattern" => constraints["pattern"] }) @regex_error_exists = true end
convert_to_type(value)
click to toggle source
# File lib/csvlint/field.rb, line 112 def convert_to_type(value) parsed = nil tv = TYPE_VALIDATIONS[constraints["type"]] if tv begin parsed = tv.call value, constraints rescue ArgumentError end end return parsed end
validate_length(value, row, column)
click to toggle source
# File lib/csvlint/field.rb, line 30 def validate_length(value, row, column) if constraints["required"] == true build_errors(:missing_value, :schema, row, column, value, { "required" => true }) if value.nil? || value.length == 0 end if constraints["minLength"] build_errors(:min_length, :schema, row, column, value, { "minLength" => constraints["minLength"] }) if value.nil? || value.length < constraints["minLength"] end if constraints["maxLength"] build_errors(:max_length, :schema, row, column, value, { "maxLength" => constraints["maxLength"] } ) if !value.nil? && value.length > constraints["maxLength"] end end
validate_range(value, row, column)
click to toggle source
# File lib/csvlint/field.rb, line 93 def validate_range(value, row, column) #TODO: we're ignoring issues with converting ranges to actual types, maybe we #should generate a warning? The schema is invalid if constraints["minimum"] minimumValue = convert_to_type( constraints["minimum"] ) if minimumValue build_errors(:below_minimum, :schema, row, column, value, { "minimum" => constraints["minimum"] }) unless value >= minimumValue end end if constraints["maximum"] maximumValue = convert_to_type( constraints["maximum"] ) if maximumValue build_errors(:above_maximum, :schema, row, column, value, { "maximum" => constraints["maximum"] }) unless value <= maximumValue end end end
validate_regex(value, row, column, all_errors)
click to toggle source
# File lib/csvlint/field.rb, line 45 def validate_regex(value, row, column, all_errors) pattern = constraints["pattern"] if pattern begin Regexp.new(pattern) build_errors(:pattern, :schema, row, column, value, { "pattern" => constraints["pattern"] } ) if !value.nil? && !value.match( constraints["pattern"] ) rescue RegexpError build_regex_error(value, row, column, pattern, all_errors) end end end
validate_type(value, row, column)
click to toggle source
# File lib/csvlint/field.rb, line 79 def validate_type(value, row, column) if constraints["type"] && value != "" parsed = convert_to_type(value) if parsed == nil failed = { "type" => constraints["type"] } failed["datePattern"] = constraints["datePattern"] if constraints["datePattern"] build_errors(:invalid_type, :schema, row, column, value, failed) return nil end return parsed end return nil end
validate_values(value, row, column)
click to toggle source
# File lib/csvlint/field.rb, line 65 def validate_values(value, row, column) # If a pattern exists, raise an invalid regex error if it is not in # valid regex form, else, if the value of the relevant field in the csv # does not match the given regex pattern in the schema, raise a # pattern error. if constraints["unique"] == true if @uniques.include? value build_errors(:unique, :schema, row, column, value, { "unique" => true }) else @uniques << value end end end