Skip to content

Commit

Permalink
fix: revert changes to cast.js
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jan 13, 2019
1 parent 563da27 commit d483040
Showing 1 changed file with 39 additions and 37 deletions.
76 changes: 39 additions & 37 deletions lib/cast.js
Expand Up @@ -29,7 +29,10 @@ module.exports = function cast(schema, obj, options, context) {

const paths = Object.keys(obj);
let i = paths.length;
let _keys;
let any$conditionals;
let schematype;
let nested;
let path;
let type;
let val;
Expand Down Expand Up @@ -241,56 +244,55 @@ module.exports = function cast(schema, obj, options, context) {
} else if (val == null) {
continue;
} else if (val.constructor.name === 'Object') {
const any$conditionals = Object.keys(val).some(function(k) {
any$conditionals = Object.keys(val).some(function(k) {
return k.charAt(0) === '$' && k !== '$id' && k !== '$ref';
});

if (!any$conditionals) {
return schematype.castForQueryWrapper({
obj[path] = schematype.castForQueryWrapper({
val: val,
context: context
});
}

const ks = Object.keys(val);

let k = ks.length;

while (k--) {
const $cond = ks[k];
const nested = val[$cond];

if ($cond === '$not') {
if (nested && schematype && !schematype.caster) {
const _keys = Object.keys(nested);
if (_keys.length && _keys[0].charAt(0) === '$') {
for (const key in nested) {
nested[key] = schematype.castForQueryWrapper({
$conditional: key,
val: nested[key],
} else {
const ks = Object.keys(val);
let $cond;

let k = ks.length;

while (k--) {
$cond = ks[k];
nested = val[$cond];

if ($cond === '$not') {
if (nested && schematype && !schematype.caster) {
_keys = Object.keys(nested);
if (_keys.length && _keys[0].charAt(0) === '$') {
for (const key in nested) {
nested[key] = schematype.castForQueryWrapper({
$conditional: key,
val: nested[key],
context: context
});
}
} else {
val[$cond] = schematype.castForQueryWrapper({
$conditional: $cond,
val: nested,
context: context
});
}
} else {
val[$cond] = schematype.castForQueryWrapper({
$conditional: $cond,
val: nested,
context: context
});
continue;
}
continue;
cast(schematype.caster ? schematype.caster.schema : schema, nested, options, context);
} else {
val[$cond] = schematype.castForQueryWrapper({
$conditional: $cond,
val: nested,
context: context
});
}
cast(schematype.caster ? schematype.caster.schema : schema, nested, options, context);
} else {
val[$cond] = schematype.castForQueryWrapper({
$conditional: $cond,
val: nested,
context: context
});
}
}

return val;
} else if (Array.isArray(val) && ['Buffer', 'Array'].indexOf(schematype.instance) === -1) {
const casted = [];
for (let valIndex = 0; valIndex < val.length; valIndex++) {
Expand Down Expand Up @@ -335,4 +337,4 @@ function _cast(val, numbertype, context) {
}
}
}
}
}

0 comments on commit d483040

Please sign in to comment.