Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Commit

Permalink
handle "Converting circular structure to JSON" error
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Jun 2, 2015
1 parent da34b29 commit d666909
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
**Note**: Gaps between patch versions are faulty/broken releases.

## 0.7.0
## v0.8.0

- **Breaking Change**
+ upgrade to tcomb-validation v2.0
Expand Down
21 changes: 14 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
'use strict';

var t = require('tcomb-validation');
var format = t.format;
var noop = function () {};

function stringify(x) {
try { // handle "Converting circular structure to JSON" error
return JSON.stringify(x);
} catch (e) {
return String(x);
}
}

function propTypes(type) {
if (process.env.NODE_ENV !== 'production') {

Expand All @@ -20,7 +27,7 @@ function propTypes(type) {
function checkPropType(values, prop, displayName) {
var value = values[prop];
if (!t.validate(value, props[prop]).isValid()) {
var message = format('Invalid prop `%s` = `%s` supplied to `%s`, should be `%s`', prop, value, displayName, name);
var message = 'Invalid prop ' + prop + ' = ' + value + ' supplied to ' + displayName + ', should be a ' + name + '.';
// add a readable entry in the call stack
checkPropType.displayName = message;
t.fail(message);
Expand All @@ -38,17 +45,15 @@ function propTypes(type) {
ret.__strict__ = function (values, prop, displayName) {
for (var k in values) {
if (values.hasOwnProperty(k) && !props.hasOwnProperty(k)) {
var message = t.format('Invalid additional prop `%s` supplied to `%s`', k, displayName);
t.fail(message);
t.fail('Invalid additional prop ' + k + ' supplied to ' + displayName + '.');
}
}
};

if (isSubtype) {
ret.__subtype__ = function (values, prop, displayName) {
if (!type.meta.predicate(values)) {
var message = format('Invalid props `%j` supplied to `%s`, should be `%s`', values, displayName, t.getTypeName(type));
t.fail(message);
t.fail('Invalid props ' + stringify(values) + ' supplied to ' + displayName + ', should be a ' + t.getTypeName(type) + '.');
}
};
}
Expand All @@ -62,7 +67,9 @@ function propTypes(type) {
// in production will be a noop
function props(Type) {
if (process.env.NODE_ENV !== 'production') {
Type = t.isType(Type) ? Type : t.struct(Type);
if (!t.isType(Type)) {
Type = t.struct(Type);
}
return function (Component) {
Component.propTypes = propTypes(Type);
};
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"name": "tcomb-react",
"version": "0.7.0",
"version": "0.8.0",
"description": "Type checking for React components",
"main": "index.js",
"scripts": {
"docs": "watchify -t reactify docs/demo/alert/src.js -o docs/demo/alert/bundle.js -v -x react"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit d666909

Please sign in to comment.