Skip to content
Open
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
9 changes: 8 additions & 1 deletion packages/flow-runtime/src/TypeContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,14 @@ export default class TypeContext {
}

warn <T, V: T | any> (type: Type<T>, input: V, prefix: string = '', path?: string[]): V {
const validation = this.validate(type, input, prefix, path);
let validation;
try {
validation = this.validate(type, input, prefix, path);
}
catch (error) {
this.emitWarningMessage(error && error.message ? error.message : String(error));
return input;
}
const message = makeWarningMessage(validation);
if (typeof message === 'string') {
this.emitWarningMessage(message);
Expand Down
17 changes: 16 additions & 1 deletion packages/flow-runtime/src/typed.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ import t from './globalContext';
const no = (input: any): any => equal(Boolean(input), false);

describe('Typed API', () => {
it('should warn instead of throwing validation-time invariant errors', () => {
const context = t.createContext();
const type = context.$shape(context.string());
const input = {foo: 'bar'};
const messages = [];

context.emitWarningMessage = message => {
messages.push(message);
};

equal(context.warn(type, input), input);
equal(messages.length, 1);
ok(/Can only \$Shape<T> object types\./.test(messages[0]));
});

it('should check a string', () => {
const type = t.string();
ok(type.accepts('helo world'));
Expand Down Expand Up @@ -558,4 +573,4 @@ describe('Typed API', () => {
))
);
});
});
});