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)
nesaulov commented
Looks good to me. @AlessandroMinali @nulldef what do you think?
nulldef commented
I think it is useless feature, 'cause we already have multiple serializers and this case can be solved via inheritance or composition.