Sha256: 03a2123459f5f0be9255376a21baaeaf32e04a0c84fec566886f822057a5eb22

Contents?: true

Size: 1.44 KB

Versions: 1

Compression:

Stored size: 1.44 KB

Contents

RSpec.describe Formalist::OutputCompiler do
  subject(:compiler) { Formalist::OutputCompiler.new }

  let(:schema) {
    Class.new(Dry::Validation::Schema) do
      key(:title, &:str?)
      key(:rating, &:int?)

      key(:reviews) do |reviews|
        reviews.each do |review|
          review.key(:description, &:str?)
          review.key(:rating, &:int?)
        end
      end

      key(:meta) do |meta|
        meta.key(:pages, &:int?)
        meta.key(:publisher, &:str?)
      end
    end.new
  }

  let(:form) {
    Class.new(Formalist::Form) do
      field :title, type: "string"
      field :rating, type: "int"

      many :reviews do |review|
        review.field :description, type: "string"
        review.field :rating, type: "int"
      end

      attr :meta do |meta|
        meta.section "Metadata" do |section|
          section.group do |group|
            group.field :pages, type: "int"
            group.field :publisher, type: "string"
          end
        end
      end
    end.new(schema)
  }

  let(:input) {
    {
      title: "Aurora",
      rating: "10",
      reviews: [
        {
          description: "Wonderful",
          rating: "10",
        },
        {
          description: "Enchanting",
          rating: "9",
        }
      ],
      meta: {
        pages: "321",
        publisher: "Orbit",
      },
    }
  }

  let(:ast) { form.build(input).to_ast }

  it "works" do
    expect(compiler.call(ast)).to eq input
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
formalist-0.2.2 spec/unit/output_compiler_spec.rb