Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/src/json/decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ const objectMap = <A>(decoder: Decoder<A>): Decoder<ObjectMap<A>> =>
return failure('expected object but found ' + typeof input);
}

const result = {} as ObjectMap<A>;
// object without a prototype or built-in functions.
const result = Object.create(null) as ObjectMap<A>;
for (const field in input) {
// @ts-ignore
const decoded = decoder.run(input[field]);
Expand Down
19 changes: 15 additions & 4 deletions core/tests/suites/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const tests = group('json', [
)),
test('pair', () =>
roundtrip(s.pair(s.string, s.number), ['wat', 2] as [string, number])),
test('tripple', () =>
test('triple', () =>
roundtrip(s.triple(s.string, s.number, s.boolean), ['wat', 2, false] as [
string,
number,
Expand All @@ -72,7 +72,11 @@ const tests = group('json', [
.dictionary(genString(), fc.boolean())
.map((v) => new Map(Object.entries(v)))
)),
test('json', () => roundtripTest(s.json, genJson())),
group('json', [
test('plain', () => roundtripTest(s.json, genJson())),
test('with "__proto__" key', () =>
roundtrip(s.json, [{ ['__proto__']: [] }])),
]),
group('recursive', [
test('base case', () => {
type List =
Expand Down Expand Up @@ -553,10 +557,17 @@ function genJson(lvl = 0): fc.Arbitrary<Json> {
);
}

function roundtripTest<T>(schema: Schema<T>, gen: fc.Arbitrary<T>): void {
function roundtripTest<T>(
schema: Schema<T>,
gen: fc.Arbitrary<T>,
assertOptions?: fc.Parameters<[v: T]>
): void {
fc.assert(
fc.property(gen, (v) => roundtrip(schema, v)),
{ includeErrorInReport: true }
{
includeErrorInReport: true,
...assertOptions,
}
);
}

Expand Down