Class: ForestAdminDatasourceToolkit::Validations::FieldValidator
- Inherits:
-
Object
- Object
- ForestAdminDatasourceToolkit::Validations::FieldValidator
- Includes:
- Schema
- Defined in:
- lib/forest_admin_datasource_toolkit/validations/field_validator.rb
Class Method Summary collapse
- .check_enum_value(column_schema, enum_value) ⇒ Object
- .validate(collection, field, values = nil) ⇒ Object
- .validate_name(collection_name, name) ⇒ Object
- .validate_value(field, schema, value, allowed_types = nil) ⇒ Object
- .validate_value_for_id(field, schema, value) ⇒ Object
Class Method Details
.check_enum_value(column_schema, enum_value) ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/forest_admin_datasource_toolkit/validations/field_validator.rb', line 79 def self.check_enum_value(column_schema, enum_value) is_enum_allowed = column_schema.enum_values.include?(enum_value) return if is_enum_allowed raise Exceptions::ValidationError, "The given enum value(s) #{enum_value} is not listed in #{column_schema.enum_values}" end |
.validate(collection, field, values = nil) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/forest_admin_datasource_toolkit/validations/field_validator.rb', line 6 def self.validate(collection, field, values = nil) dot_index = field.index(':') if dot_index.nil? schema = collection.schema[:fields][field] raise Exceptions::ValidationError, "Column not found: '#{collection.name}.#{field}'" if schema.nil? if schema.type != 'Column' raise Exceptions::ValidationError, "Unexpected field type: '#{collection.name}.#{field}' (found '#{schema.type}' expected 'Column')" end if values.is_a?(Array) values.each do |value| validate_value(field, schema, value) end end else prefix, suffix = field.split(':') schema = collection.schema[:fields][prefix] raise Exceptions::ValidationError, "Relation not found: '#{collection.name}.#{prefix}'" if schema.nil? if schema.type == 'PolymorphicManyToOne' && suffix != '*' raise Exceptions::ValidationError, "Unexpected nested field #{suffix} under generic relation: #{collection.name}.#{prefix}" end if schema.type != 'ManyToOne' && schema.type != 'OneToOne' && schema.type != 'PolymorphicManyToOne' && schema.type != 'PolymorphicOneToOne' raise Exceptions::ValidationError, "Unexpected field type: '#{collection.name}.#{prefix}' (found '#{schema.type}')" end if schema.type != 'PolymorphicManyToOne' suffix = field[dot_index + 1, field.length - dot_index - 1] association = collection.datasource.get_collection(schema.foreign_collection) validate(association, suffix, values) end end end |
.validate_name(collection_name, name) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/forest_admin_datasource_toolkit/validations/field_validator.rb', line 71 def self.validate_name(collection_name, name) return unless name.include?(' ') sanitized_name = name.gsub(/ (.)/, &:upcase) raise Exceptions::ValidationError, "The name of field '#{name}' you configured on '#{collection_name}' must not contain space. Something like '#{sanitized_name}' should work has expected." end |
.validate_value(field, schema, value, allowed_types = nil) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/forest_admin_datasource_toolkit/validations/field_validator.rb', line 52 def self.validate_value(field, schema, value, allowed_types = nil) allowed_types ||= Rules.get_allowed_types_for_column_type(schema.column_type) # TODO: FIXME: handle complex type from ColumnType # if schema.column_type != PrimitiveType::STRING # end type = TypeGetter.get(value, schema.column_type) unless allowed_types.include?(type) raise Exceptions::ValidationError, "The given value has a wrong type for '#{field}': #{value}.\n Expects #{allowed_types}" end return unless value && schema.column_type == PrimitiveType::ENUM check_enum_value(schema, value) end |
.validate_value_for_id(field, schema, value) ⇒ Object
48 49 50 |
# File 'lib/forest_admin_datasource_toolkit/validations/field_validator.rb', line 48 def self.validate_value_for_id(field, schema, value) validate_value(field, schema, value, [schema.column_type]) end |