question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Weird behaviour on toJSON method

See original GitHub issue

When you fill a child element with an invalid value toJSON method acts in a weird way. The parent toJSON returns the old child element and the toJSON of the child element returns the child element in the invalid state.

For example, given the schema:

class MyEntity extends Speck {
  static SCHEMA = {
    field: PropTypes.string,
    anotherField: PropTypes.string
  }
}

class FatherEntity extends Speck {
  static SCHEMA = {
    child: PropTypes.instanceOf(MyEntity)
  }
}

And the variables setup:

const a = new FatherEntity({ child: { field: 'value' } })
a.child = { field: 'invalid entity', anotherField: 'yet a invalid entity' }

The toJSON method have this behaviour:

//prints { child: { field: 'value' } }
console.log(a.toJSON()) 
//prints { field: 'invalid entity', anotherField: 'yet a invalid entity' }
console.log(a.child.toJSON()) 

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
guisouzacommented, May 28, 2017

try with :

class FatherEntity extends Speck {
  static SCHEMA = {
    child: {
      validator: PropTypes.instanceOf(MyEntity),
      type: MyEntity
    }
  }
}
0reactions
danielbdiascommented, Sep 22, 2017

I can’t simulate this error anymore. For now I think that is better to close this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Date object to JSON weird behaviour - Stack Overflow
So I suppose every 'toJSON' method uses toISOString method within.. :) So problem is solved - probably its default behaviour that 'jsonizing' ...
Read more >
JSON and the stringification oddities in JavaScript - Zhenghao
Surprising and inconsistent behaviour of JSON.stringify# ... In JavaScript, the way to convert a value to a JSON string is via JSON.stringify ....
Read more >
Weird behavior during parsing Json - Microsoft Q&A
Weird behavior during parsing Json. I'm parsing this JSON ... Public Shared Function FromJson(ByVal json As String) As Openorders ...
Read more >
Strange & annoying JSON behavior in ExtendScript.
The problem is, in the following JSX code I get a result where all empty objects and empty arrays are split into having...
Read more >
Weird JSON object behavior? - ShapeDiver - McNeel Forum
Hey Devs,. Just a quick one to point out that I personally think that the ShapediverJSONConstruct object does not act the way i...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found