require "strong_json" describe "StrongJSON.new" do it "tests the structure of a JSON object" do s = StrongJSON.new do let :item, object(name: string, count: numeric, price: numeric).ignore(Set.new([:comment])) let :items, array(item) let :checkout, object(items: items, change: optional(number), type: enum(literal(1), symbol), customer: object?( name: string, id: string, birthday: string, gender: enum(literal("man"), literal("woman"), literal("other")), phone: string ) ) end expect( s.checkout.coerce(items: [{ name: "test", count: 1, price: "2.33", comment: "dummy" }], type: 1) ).to eq(items: [ { name: "test", count: 1, price: "2.33" }], type: 1, change: nil, customer: nil) expect { s.checkout.coerce(items: [{ name: "test", count: 1, price: [], comment: "dummy" }], type: 1) }.to raise_error(StrongJSON::Type::TypeError) {|e| expect(e.path.to_s).to eq("$.items[0].price") expect(e.type).to be_a(StrongJSON::Type::Base) expect(e.message).to eq("TypeError at $.items[0].price: expected=numeric, value=[]") reporter = StrongJSON::ErrorReporter.new(path: e.path) expect(reporter.to_s).to eq(<3}") reporter = StrongJSON::ErrorReporter.new(path: e.path) expect(reporter.to_s).to eq(<