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
14 changes: 10 additions & 4 deletions packages/flow-runtime/src/TypeContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -1027,10 +1027,16 @@ 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);
const message = makeWarningMessage(validation);
if (typeof message === 'string') {
this.emitWarningMessage(message);
try {
const validation = this.validate(type, input, prefix, path);
const message = makeWarningMessage(validation);
if (typeof message === 'string') {
this.emitWarningMessage(message);
}
}
catch (error) {
const message = error && error.message ? error.message : String(error);
this.emitWarningMessage(`Warning: ${message}`);
}
return input;
}
Expand Down
15 changes: 14 additions & 1 deletion packages/flow-runtime/src/typed.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,19 @@ describe('Typed API', () => {

});

it('should warn instead of throwing when validation throws', () => {
const context = t.createContext();
const type = context.$shape(context.string());
let warning;
const input = {};
context.emitWarningMessage = (message) => {
warning = message;
};

equal(context.warn(type, input), input);
ok(warning.indexOf('Can only $Shape<T> object types.') !== -1);
});

it('should $Keys<A>', () => {
const A = t.object(
t.property('name', t.string()),
Expand Down Expand Up @@ -558,4 +571,4 @@ describe('Typed API', () => {
))
);
});
});
});