Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncate long types #32

Merged
merged 4 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,18 @@ const getValidationContext = (validation: t.ValidationError) =>
// https://github.com/gcanti/fp-ts/pull/544/files
validation.context as t.ContextEntry[];

export const TYPE_MAX_LEN = 160; // Two lines of 80-col text
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an arbitrary number, perhaps it can be an overridable default

gillchristian marked this conversation as resolved.
Show resolved Hide resolved
const truncateType = (type: string): string =>
type.length > TYPE_MAX_LEN ? `${type.slice(0, TYPE_MAX_LEN - 3)}...` : type;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative is to do the ... in the middle and show both the beginning and end, to make it feel more "complete"

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it would look better.


const errorMessageSimple = (
expectedType: string,
path: string,
error: t.ValidationError
) =>
// https://github.com/elm-lang/core/blob/18c9e84e975ed22649888bfad15d1efdb0128ab2/src/Native/Json.js#L199
[
`Expecting ${expectedType}`,
`Expecting ${truncateType(expectedType)}`,
path === '' ? '' : `at ${path}`,
`but instead got: ${jsToString(error.value)}`,
error.message ? `(${error.message})` : ''
Expand All @@ -53,7 +57,7 @@ const errorMessageUnion = (
// https://github.com/elm-lang/core/blob/18c9e84e975ed22649888bfad15d1efdb0128ab2/src/Native/Json.js#L199
[
'Expecting one of:\n',
expectedTypes.map(type => ` ${type}`).join('\n'),
expectedTypes.map(type => ` ${truncateType(type)}`).join('\n'),
path === '' ? '\n' : `\nat ${path} `,
`but instead got: ${jsToString(value)}`
]
Expand Down
18 changes: 17 additions & 1 deletion tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import test from 'ava';
import * as iots from 'io-ts';
import { withMessage } from 'io-ts-types/lib/withMessage';

import { reporter } from '../src';
import { reporter, TYPE_MAX_LEN } from '../src';

test('reports an empty array when the result doesn’t contain errors', t => {
const PrimitiveType = iots.string;
Expand Down Expand Up @@ -60,3 +60,19 @@ test('formats branded types correctly', t => {
"Expecting Positive but instead got: -1 (Don't be so negative!)"
]);
});

test('truncates really long types', t => {
const longType = iots.type({
'1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890':
iots.number
});
const messages = reporter(longType.decode(null));
t.is(messages.length, 1);
t.regex(
messages[0],
new RegExp(
`^Expecting .{${TYPE_MAX_LEN - 3}}\\.{3} but instead got: null$`
),
'Should be truncated'
);
});
26 changes: 25 additions & 1 deletion tests/unions.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava';
import * as iots from 'io-ts';

import { reporter } from '../src';
import { reporter, TYPE_MAX_LEN } from '../src';

test('formats keyof unions as "regular" types', t => {
const WithKeyOf = iots.interface({
Expand Down Expand Up @@ -144,3 +144,27 @@ test('string union deeply nested', t => {
]
);
});

test('truncates really long unions', t => {
const longUnion = iots.union([
iots.type({
'1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890':
iots.string
}),
iots.type({
'1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890':
iots.number
})
]);
const messages = reporter(longUnion.decode(null));
t.is(messages.length, 1);
t.regex(
messages[0],
new RegExp(
`^Expecting one of:\n( *.{${
TYPE_MAX_LEN - 3
}}\\.{3}\n){2} *but instead got: null$`
),
'Should be truncated'
);
});