Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not typeconverting values of nested objects #29

Closed
jwaltz opened this issue Oct 18, 2013 · 3 comments
Closed

Not typeconverting values of nested objects #29

jwaltz opened this issue Oct 18, 2013 · 3 comments

Comments

@jwaltz
Copy link

jwaltz commented Oct 18, 2013

Using simple-schema v0.2.10

Simple-schema does not seem to be typeconverting some string values of a nested object into numbers/decimal numbers when I call clean. I was looking through the code of simple-schema.js and it's something in here:

//clean
  newDoc = {};
  _.each(cDoc, function(val, key) {
    var okToAdd = true;

    //filter
    if (options.filter === true) {
      okToAdd = self.allowsKey(key);
    }

    if (okToAdd) {
      //autoconvert
      if (options.autoConvert === true) {
        var def = self._schema[key];
        if (def) {
          var type = def.type;
          if (_.isArray(type)) {
            type = type[0];
          }
          if (looksLikeModifier(val)) {
            //convert modifier values
            _.each(val, function(opVal, op) {
              if (_.isArray(opVal)) {
                for (var i = 0, ln = opVal.length; i < ln; i++) {
                  opVal[i] = typeconvert(opVal[i], type); //typeconvert
                }
              } else if (_.isObject(opVal) && ("$each" in opVal)) {
                for (var i = 0, ln = opVal.$each.length; i < ln; i++) {
                  opVal.$each[i] = typeconvert(opVal.$each[i], type); //typeconvert
                }
              } else {
                opVal = typeconvert(opVal, type); //typeconvert
              }
              val[op] = opVal;
            });
          } else if (_.isArray(val)) {
            for (var i = 0, ln = val.length; i < ln; i++) {
              val[i] = typeconvert(val[i], type); //typeconvert
            }
          } else {
            val = typeconvert(val, type); //typeconvert
          }
        }
      }

      newDoc[key] = val;
    }
  });

If I understand correctly, looksLikeModifier(val) needs to be true in order to get to this line: } else if (_.isObject(opVal) && ("$each" in opVal)) { which I think is what would typeconvert the nested values? I don't really understand what the "$each" is except that opVal is the first argument to the iterator of the _.each call and represents the current element. http://underscorejs.org/#each

Some of the values i'm trying to convert include negative integers and negative decimals.

Let me know what you think and if i'm totally misunderstanding what's going on and doing something wrong.

@jwaltz
Copy link
Author

jwaltz commented Oct 18, 2013

I've been reading over the code in simple-schema.js and looksLikeModifier(val) seems to be for mongo modifiers, which doesn't apply to the situation i'm in. It just seems like in the else portion of the looksLikeModifier(val) if-block there isn't any code that deals with objects.

@aldeed
Copy link
Collaborator

aldeed commented Oct 24, 2013

Sorry, @jwaltz, I didn't see this issue until now. There shouldn't be any need for the clean code to recurse into objects because it calls collapseObj first, which reduces nesting to one level. There might be a problem with the collapsing code, though. Can you post an example of an object that is not being typeconverted properly?

@aldeed
Copy link
Collaborator

aldeed commented Oct 26, 2013

There's a new release that I think will fix this. If not, you can re-open with an example for reproducing the issue. Thanks.

@aldeed aldeed closed this as completed Oct 26, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants