nesaulov/surrealist

conditional tags for json_schema

briu opened this issue · 3 comments

briu commented

Currently, if I want to serialize only some fields from a serializer(for example for authorization purposes) I do it like this:

module SerializerMixin
  def first_field
    []
  end
end

class CompositeSerializer < Surrealist::Serializer
  include SerializerMixin

  json_schema do
    { first_field: Array, second_field: Hash, third_field: String }
  end

  def second_field
    {}
  end

  def third_field
    "awesome issue"
  end
end

class GuestSerializer < Surrealist::Serializer
  include SerializerMixin

  json_schema do
    { first_field: Array }
  end
end

GuestSerializer.new(object).build_schema

I think it would be great, if we can use one serializer with different json schemas

class CompositeSerializer < Surrealist::Serializer
  json_schema do
    { first_field: Array, second_field: Hash, third_field: String }
  end
 
  json_schema(:guest_user) do
    { first_field: Array }
  end

  def first_field
    []
  end

  def second_field
    {}
  end

  def third_field
    "awesome issue"
  end
end

CompositeSerializer.new(object).build_schema(:guest_user)

Looks good to me. @AlessandroMinali @nulldef what do you think?

I think it is useless feature, 'cause we already have multiple serializers and this case can be solved via inheritance or composition.

Yeah, I guess @nulldef is right about this. Placing different schemas in one class can lead to creating a god-object and introduce difficulties in supporting and changing it later on. Closing this for now, but thanks for proposal, @briu