From ac70f1e7dd2cb0cc06aaa0a18faf006fb1fdfaec Mon Sep 17 00:00:00 2001 From: Muhammad Nawzad Date: Sun, 12 Nov 2023 11:45:13 +0300 Subject: [PATCH] Adds RequestSchemaGenerator class --- lib/schemable.rb | 1 + lib/schemable/request_schema_generator.rb | 60 ++++++++++++++++++++++ sig/schemable/request_schema_generator.rbs | 10 ++++ 3 files changed, 71 insertions(+) create mode 100644 lib/schemable/request_schema_generator.rb create mode 100644 sig/schemable/request_schema_generator.rbs diff --git a/lib/schemable.rb b/lib/schemable.rb index 1fa7fbd..e2c1285 100644 --- a/lib/schemable.rb +++ b/lib/schemable.rb @@ -8,6 +8,7 @@ require_relative 'schemable/attribute_schema_generator' require_relative 'schemable/response_schema_generator' require_relative 'schemable/relationship_schema_generator' require_relative 'schemable/included_schema_generator' +require_relative 'schemable/request_schema_generator' module Schemable class Error < StandardError; end diff --git a/lib/schemable/request_schema_generator.rb b/lib/schemable/request_schema_generator.rb new file mode 100644 index 0000000..5847530 --- /dev/null +++ b/lib/schemable/request_schema_generator.rb @@ -0,0 +1,60 @@ +module Schemable + class RequestSchemaGenerator + attr_accessor :model_definition, :schema_modifier + + def initialize(model_definition) + @model_definition = model_definition + @schema_modifier = SchemaModifier.new + end + + def generate_for_create + schema = { + type: :object, + properties: { + data: AttributeSchemaGenerator.new(@model_definition).generate + } + } + + @schema_modifier.add_properties(schema, @model_definition.additional_create_request_attributes, 'properties.data.properties') + + @model_definition.excluded_create_request_attributes.each do |key| + @schema_modifier.delete_properties(schema, "properties.data.properties.#{key}") + end + + required_attributes = { + required: ( + schema.as_json['properties']['data']['properties'].keys - + @model_definition.optional_create_request_attributes.map(&:to_s) - + @model_definition.nullable_attributes.map(&:to_s) + ).map { |key| key.to_s.camelize(:lower).to_sym } + } + + @schema_modifier.add_properties(schema, required_attributes, 'properties.data') + end + + def generate_for_update + schema = { + type: :object, + properties: { + data: AttributeSchemaGenerator.new(@model_definition).generate + } + } + + @schema_modifier.add_properties(schema, @model_definition.additional_update_request_attributes, 'properties.data.properties') + + @model_definition.excluded_update_request_attributes.each do |key| + @schema_modifier.delete_properties(schema, "properties.data.properties.#{key}") + end + + required_attributes = { + required: ( + schema.as_json['properties']['data']['properties'].keys - + @model_definition.optional_update_request_attributes.map(&:to_s) - + @model_definition.nullable_attributes.map(&:to_s) + ).map { |key| key.to_s.camelize(:lower).to_sym } + } + + @schema_modifier.add_properties(schema, required_attributes, 'properties.data') + end + end +end diff --git a/sig/schemable/request_schema_generator.rbs b/sig/schemable/request_schema_generator.rbs new file mode 100644 index 0000000..29f3996 --- /dev/null +++ b/sig/schemable/request_schema_generator.rbs @@ -0,0 +1,10 @@ +module Schemable + class RequestSchemaGenerator + attr_accessor model_definition: Definition + attr_accessor schema_modifier: SchemaModifier + + def initialize: (Definition) -> void + def generate_for_create: () -> (Hash[Symbol, any] | Array[Hash[Symbol, any]]) + def generate_for_update: () -> (Hash[Symbol, any] | Array[Hash[Symbol, any]]) + end +end