Class: Chronicle::Schema::Validation::ContractFactory
- Inherits:
-
Object
- Object
- Chronicle::Schema::Validation::ContractFactory
- Defined in:
- lib/chronicle/schema/validation/contract_factory.rb
Class Method Summary collapse
- .build_type(range) ⇒ Object
- .coerce_chronicle_edge(value) ⇒ Object
- .create(type_id:, properties: []) ⇒ Object
- .create_schema(type_id:, properties: []) ⇒ Object
- .process_errors(errors) ⇒ Object
Class Method Details
.build_type(range) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/chronicle/schema/validation/contract_factory.rb', line 118 def self.build_type(range) literals = [] literals << :integer if range.include?(:Integer) literals << :float if range.include?(:Float) literals << :string if range.include?(:Text) literals << :string if range.include?(:URL) literals << :time if range.include?(:DateTime) literals << :hash # puts "building type for #{range}. literals: #{literals}" literals.uniq end |
.coerce_chronicle_edge(value) ⇒ Object
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/chronicle/schema/validation/contract_factory.rb', line 107 def self.coerce_chronicle_edge(value) return value unless value.is_a?(Hash) type = (value[:@type] || value['@type']).to_sym contract_klass = Chronicle::Schema::Validation.get_contract(type) return value unless contract_klass result = contract_klass.schema.call(value) result.success? ? result.to_h : value end |
.create(type_id:, properties: []) ⇒ Object
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 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/chronicle/schema/validation/contract_factory.rb', line 22 def self.create(type_id:, properties: []) Class.new(Chronicle::Schema::Validation::BaseContract) do type type_id params(Chronicle::Schema::Validation::ContractFactory.create_schema(type_id:, properties:)) properties.each do |property| edge_name = property.id_snakecase if property.many? rule(edge_name).each do |index:| errors = edge_validator.validate(type_id, edge_name, value) error_path = [edge_name, index] = Chronicle::Schema::Validation::ContractFactory.process_errors(errors) .each do |path, | key(error_path + path).failure() end end else rule(edge_name) do # handle nils. FIXME: this is a hack next unless value errors = edge_validator.validate(type_id, edge_name, value) error_path = [edge_name] = Chronicle::Schema::Validation::ContractFactory.process_errors(errors) .each do |path, | key(error_path + path).failure() end end end end end end |
.create_schema(type_id:, properties: []) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/chronicle/schema/validation/contract_factory.rb', line 58 def self.create_schema(type_id:, properties: []) Dry::Schema.JSON do required(:@type).value(:str?).filled(eql?: type_id.to_s) before(:key_coercer) do |result| result.to_h.transform_keys!(&:to_sym) valid_property_ids = properties.map(&:id_snakecase) valid_property_ids << :@type invalid_properties = result.to_h.keys - valid_property_ids invalid_properties.each do |invalid_property| result.add_error([:unexpected_key, [invalid_property.to_sym, []]]) end end # Attempt to coerce chronicle edges recursively # I tried to use a custom type in the schema and use the constructor # method to do the coercion but it didn't work consistently before(:value_coercer) do |obj| obj.to_h.transform_values do |value| case value when ::Array value.map do |v| Chronicle::Schema::Validation::ContractFactory.coerce_chronicle_edge(v) end when ::Hash Chronicle::Schema::Validation::ContractFactory.coerce_chronicle_edge(value) else value end end end properties.each do |property| property_name = property.id_snakecase type = Chronicle::Schema::Validation::ContractFactory.build_type(property.range_identifiers) outer_macro = property.required? ? :required : :optional if property.many? send(outer_macro, property_name).value(:array).each(type) else send(outer_macro, property_name).value(type) end end end end |
.process_errors(errors) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/chronicle/schema/validation/contract_factory.rb', line 5 def self.process_errors(errors) = [] errors.each do |key, value| base_path = key == :base ? [] : [key] if value.is_a?(Hash) value.each do |k, v| << [base_path + [k], v.first.to_s] end elsif value.is_a?(Array) << [base_path, value.first.to_s] else << [base_path, value.to_s] end end end |