Sha256: 18418d4536f753068c0ff348d1acdecd8d46343bb184b2ccaa47e14b1ae8ff9d

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

require 'spec_helper'

describe JsonValidator do
  before do
    run_migration do
      create_table(:users, force: true) do |t|
        t.string :name
        t.text :profile # Change this to `t.json` when Travis supports PostgreSQL 9.2+
      end
    end

    spawn_model :User do
      schema = {
        type: 'object',
        :'$schema' => 'http://json-schema.org/draft-03/schema',
        properties: {
          city: { type: 'string', required: false },
          country: { type: 'string', required: true }
        }
      }

      serialize :profile, JSON
      validates :name, presence: true
      validates :profile, presence: true, json: { schema: schema }
    end
  end

  context 'with blank JSON value' do
    let(:attributes) { { name: 'Samuel Garneau', profile: {} } }
    it { expect(User.new(attributes)).to_not be_valid }
  end

  context 'with invalid JSON value' do
    let(:attributes) { { name: 'Samuel Garneau', profile: { city: 'Quebec City' } } }
    it { expect(User.new(attributes)).to_not be_valid }
  end

  context 'with valid JSON value' do
    let(:attributes) { { name: 'Samuel Garneau', profile: { country: 'CA' } } }
    it { expect(User.new(attributes)).to be_valid }
  end

  context 'with malformed JSON value' do
    let(:attributes) { { name: 'Samuel Garneau', profile: 'foo:}bar' } }
    it { expect(User.new(attributes)).to_not be_valid }
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activerecord_json_validator-0.1.1 spec/json_validator_spec.rb