<commit_before>require 'spec_helper'

describe Parametric::Schema do
  subject(:schema) do
    described_class.new do
      prepare do |payload, context|
        payload[:slug] = payload[:name].to_s.downcase.gsub(/\s+/, '-') unless payload[:slug]
        payload
      end

      field(:name).policy(:string).present
      field(:slug).policy(:string).present
      field(:variants).policy(:array).schema do
        prepare do |payload, context|
          payload[:slug] = "v: #{payload[:name].to_s.downcase}"
          payload
        end
        field(:name).policy(:string).present
        field(:slug).type(:string).present
      end
    end
  end

  it 'passes payload through prepare block, if defined' do
    result = schema.resolve({ name: 'A name', variants: [{ name: 'A variant' }] })
    expect(result.valid?).to be true
    expect(result.output[:slug]).to eq 'a-name'
    expect(result.output[:variants].first[:slug]).to eq 'v: a variant'
  end
end
<commit_msg>Test that pre-resolvers can add errors<commit_after>require 'spec_helper'

describe Parametric::Schema do
  it 'passes payload through prepare block, if defined' do
    schema = described_class.new do
      prepare do |payload, context|
        payload[:slug] = payload[:name].to_s.downcase.gsub(/\s+/, '-') unless payload[:slug]
        payload
      end

      field(:name).policy(:string).present
      field(:slug).policy(:string).present
      field(:variants).policy(:array).schema do
        prepare do |payload, context|
          payload[:slug] = "v: #{payload[:name].to_s.downcase}"
          payload
        end
        field(:name).policy(:string).present
        field(:slug).type(:string).present
      end
    end

    result = schema.resolve({ name: 'A name', variants: [{ name: 'A variant' }] })
    expect(result.valid?).to be true
    expect(result.output[:slug]).to eq 'a-name'
    expect(result.output[:variants].first[:slug]).to eq 'v: a variant'
  end

  it 'collects errors added in pre-resolvers' do
    schema = described_class.new do
      field(:variants).type(:array).schema do
        prepare do |payload, context|
          context.add_error 'nope!' if payload[:name] == 'with errors'
          payload
        end
        field(:name).type(:string)
      end
    end

    results = schema.resolve({ variants: [ {name: 'no errors'}, {name: 'with errors'}]})
    expect(results.valid?).to be false
    expect(results.errors['$.variants[1]']).to eq ['nope!']
  end
end

{'fraction_non_alphanumeric': 0.14771347632537435, 'fraction_numerical': 0.0004046944556859571, 'mean_word_length': 2.930047694753577, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1976756', 'n_tokens_mistral': 741, 'n_tokens_neox': 741, 'n_words': 182}