Loading ...
Sorry, an error occurred while loading the content.
 

Re: Valid json gets rejected from Datasource because of dash '-' in field key.

Expand Messages
  • xabbaxaco
    ... all very well and good indeed, and now you should add one more info to that: Not ME but your/YUI s/YAHOO s very own datasource impementation is walking the
    Message 1 of 11 , Jul 30, 2008
      --- In ydn-javascript@yahoogroups.com, "y_lsmith" <lsmith@...> wrote:
      >
      > .... The path builder dissects the string then
      > uses the bits to walk down the object structure to the value much as
      > JavaScript does internally. In your case, you cannot access the data
      > in column-2 via
      >
      > recordObject.column-2 (represented as {key:'.column-2'} or
      > {key:'column-2'})
      >
      > as that would evaluate to the expression recordObject.column - 2. You
      > must access it via square bracket notation

      all very well and good indeed, and now you should add one more info
      to that:

      Not ME but your/YUI's/YAHOO's very own datasource impementation is
      walking the recordObject.

      And as the fix(just disabling the regex) proves, your current
      datasource implementation does NOT take the keys to eval them

      into: recordObject.column - 2
      but to: recordObject['column-2']

      and this is good. Assuming and actually enforcing valid javascript
      identifier syntax on arbitray hash keys is a very very bad idea.

      mayby by the wish for an overly flexible implementation the datasource
      became complex and buggy.


      one last thing, i you where right, than

      { key: "['column-2']', ...

      would work. but it does not. the datasource implementation fails
      before the lines where you spotted a bug.

      the datasource is NOT evaling the keys. it is 'walking' them with the
      [] operator and not de-referenceing them with the '.' operator.

      what can i say more? i got my code working, datasource impl. is robust
      against arbitrary keys and works find with my disables regexp
      fix, but i don't need this fix anymore and can restart using the YUI
      CDN.

      dirk


      >
      > recordObject["column-2"] (represented as {key:'["column-2"]'})
      >
      > DataSource field keys are a string containing everything after
      >
      > recordObject
      >
      > with the leading dot left optional. So
      >
      > MyDataSource.responseSchema = {
      > resultsList : 'Results',
      > fields : [ { key: 'foo' }, // and
      > { key: '.foo' }, // and (optional leading dot)
      > { key: '["foo"]'} // are equivalent
      > ]
      > };
      >
      > Since your key is not a legal JavaScript identifier in the context of
      > dot notation, your field definition needs to use square bracket
      notation:
      > ...
      > fields : [ { key: '["column-2"]' } ... ]
      >
      >
      > [snip]
      > >
      > > sorry to say so, but that's not quite right, the code fails well
      > > before that.
      > >
      > > please re-check/read my original posting:
      > >
      > > just 6 lines above 'if (path) {' you find 'path = buildPath(key);' and
      > > as i tried to make clear in my original post, in line 1521 of
      > > datasource-beta-debug 2.5.2, inside the implementation of 'buildPath',
      > > you'll find:
      > >
      > > if (!/[^\w\.\$@]/.test(needle)) {
      > >
      > > -> which results in false for any string with a dash
      > > -> which results in buildPath returning a null path
      > > -> which results in if (path) beeing false
      > > -> which result in YAHOO.log("Invalid key syntax: "... being called
      > >
      >
      > This test verifies that the key string is a valid JavaScript
      > identifier. If it fails on your identifier, you are likely supplying
      > a key string that should use square bracket notation.
      >
      >
      > > again, a ' '(blank) is not a problem, as well as depth 1 or 2 are not
      > > even considered at that point.
      > >
      > > and sorry, after all this misunderstanding of my post, i didn't bother
      > > no more to check the second fix.
      > >
      > > by now i added a key rewriting to my javascript lib.
      > >
      > > and, please,
      > >
      > > { "bar-foo": "foobar" } is PERFECTLY VALID JSON!!!
      > >
      > > changing this to { "[bar-foo]": "foobar" } or similar crueltoes is
      > > just plain wrong. i definitly don't start crippling my code with keys
      > > like "[foo-bar]" and similar cruelties. Thats cargocult.
      >
      > hehe. Agreed. It's awful. My apologies if it appeared I was
      > recommending you change your JSON to such an atrocity. I was trying
      > to illustrate how the included code from DataSource was buggy by
      > showing what it considered "valid" in the case of single depth square
      > bracket notation keys.
      >
      > >
      > > and now that i made i complete asshole of myself i still want to thank
      > > you all for spending your time into looking for the problem,
      honestly,
      > >
      > > thx
      > > dirk
      > >
      >
      > Our pleasure. After all, you (inadvertently, perhaps) helped discover
      > a bug. Good things all 'round.
      >
      > Luke
      >
    Your message has been successfully submitted and would be delivered to recipients shortly.