Skip to content

Commit

Permalink
Add test cases to replicate bug report #1224
Browse files Browse the repository at this point in the history
  • Loading branch information
nvie committed Feb 18, 2025
1 parent 1b6b05f commit 7487b25
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions test/bug-report-1224.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { expect, test } from 'vitest';
import {
define,
formatShort,
nonEmptyString,
object,
oneOf,
positiveInteger,
type Decoder,
} from '~';

export function combineObjects<
A extends Record<string, unknown>,
B extends Record<string, unknown>,
>(decoderA: Decoder<A>, decoderB: Decoder<B>): Decoder<A & B> {
return define((blob, ok, error) => {
const a = decoderA.decode(blob);
if (!a.ok) {
// console.log(util.inspect(a, { depth: null }));
return error(a.error);
}

const b = decoderB.decode(blob);
if (!b.ok) {
return error(b.error);
}

return ok({ ...a.value, ...b.value });
});
}

test('test', () => {
const profile = {
name: 'Bob',
surname: 'Burgers',
age: 40,
occupation: 'Restaurateur',
};

const restaurantOwner = combineObjects(
object({
name: nonEmptyString,
surname: nonEmptyString,
age: nonEmptyString,
}),
object({
age: positiveInteger,
occupation: oneOf(['Restaurateur']),
}),
);

const result = restaurantOwner.decode(profile);

expect(result.ok).toBe(false);
expect(result.value).toBeUndefined();

expect((result.error as any)?.text).toBe(undefined);
expect((result.error as any)?.fields.get('age')).toStrictEqual({
text: 'Must be string',
type: 'scalar',
value: 40,
});

expect(formatShort(result.error!)).toEqual("Value at key 'age': Must be string");
});

0 comments on commit 7487b25

Please sign in to comment.