Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multipart input validation support #107

Open
thexa4 opened this issue Jul 24, 2021 · 0 comments
Open

Add multipart input validation support #107

thexa4 opened this issue Jul 24, 2021 · 0 comments

Comments

@thexa4
Copy link
Collaborator

thexa4 commented Jul 24, 2021

Accept: multipart/vnd.delftsolutions
Accept-Multipart: multipart/vnd.delftsolutions; scope=multipart/*, multipart/related, text/html; scope=multipart/*#multipart/*
class BookshelfValidator
  include MediaTypes::Serialization::MultipartValidator

  use_name 'bookshelf'

  validations version: 1 do |view, version|
    has_one name: :key # defaults to text/plain
    has_multiple name: :image do
      optional
      allow raw: 'image/*'
    end
    allow_others do
      allow serialized: BookSerializer, version: 1
    end
  end
end

class BookshelfSerializer < MediaTypes::Serialization::Base
  multipart BookshelfValidator
  
  input do |parts, mapped, context|
    return new Bookshelf(mapped.key, mapped.image, parts.select { |p| p.name.nil? })
  end

  output do |shelf, context|
    part name: :key, disposition: 'form-data' do
      # Defaults to text/plain
      render_media shelf.key
    end
    shelf.images.each do |i|
      part name: :image, disposition: 'attachment', filename: i.filename do
        allow_raw i.content_type
        render_media i
      end
    end
    shelf.books.each do |book|
      part do
        allow_serializer BookSerializer, version: 1
        allow_serializer BookSerializer, view: :html, version: 1
        render_media book
      end
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant