Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

simpl-schema

Package Overview
Dependencies
Maintainers
2
Versions
73
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simpl-schema - npm Package Compare versions

Comparing version 1.1.2 to 1.2.0

5

CHANGELOG.md

@@ -6,2 +6,3 @@ <!-- START doctoc generated TOC please keep comment here to allow auto update -->

- [simpl-schema CHANGELOG](#simpl-schema-changelog)
- [1.2.0](#120)
- [1.1.2](#112)

@@ -30,2 +31,6 @@ - [1.1.1](#111)

## 1.2.0
The performance of `clean`, specifically of looping through the object to apply autoValues and defaultValues, has been greatly improved for large objects.
## 1.1.2

@@ -32,0 +37,0 @@

180

dist/clean/getPositionsForAutoValue.js

@@ -16,20 +16,2 @@ 'use strict';

// Extracts operator piece, if present, from position string
function extractOp(position) {
var firstPositionPiece = position.slice(0, position.indexOf('['));
return firstPositionPiece.substring(0, 1) === '$' ? firstPositionPiece : null;
}
function objectsThatKeyWillCreate(key) {
var objs = [];
do {
var lastDotPosition = key.lastIndexOf('.');
key = lastDotPosition === -1 ? '' : key.slice(0, lastDotPosition);
if (key.length && !key.endsWith('.$')) objs.push(key);
} while (key.length);
return objs;
}
/**

@@ -55,2 +37,6 @@ * A position is a place in the object where this field exists.

* }
*
* To make matters more complicated, we want to include not only the existing positions
* but also the positions that might exist due to their parent object existing or their
* parent object being auto-created by a MongoDB modifier that implies it.
*/

@@ -62,116 +48,70 @@ function getPositionsForAutoValue(_ref) {

var positions = [];
// Positions for this field
var positions = mongoObject.getPositionsInfoForGenericKey(fieldName);
// Loop through every position
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
// For simple top-level fields, just add an undefined would-be position
// if there isn't a real position.
if (fieldName.indexOf('.') === -1 && positions.length === 0) {
positions.push({
key: fieldName,
value: undefined,
operator: isModifier ? '$set' : null,
position: isModifier ? '$set[' + fieldName + ']' : fieldName
});
return positions;
}
try {
var _loop = function _loop() {
var position = _step.value;
var parentPath = (0, _utility.getParentOfKey)(fieldName);
var lastPart = (0, _utility.getLastPartOfKey)(fieldName, parentPath);
var lastPartWithBraces = lastPart.replace(/\./g, '][');
var parentPositions = mongoObject.getPositionsInfoForGenericKey(parentPath);
var genericAffectedKey = mongoObject._genericAffectedKeys[position];
var affectedKey = mongoObject._affectedKeys[position];
var hasOperator = position.startsWith('$');
// For pull, push, addToSet, and pop, mongoObject appends .0/.$ to the keys
// but really we want to think of them as being related to the array itself
// rather than the items. XXX It's possible that the change should be made in
// mongo-object package, too.
var isArrayOperator = ['$pull', '$push', '$addToSet', '$pop'].some(function (op) {
return position.startsWith(op);
});
if (genericAffectedKey === fieldName || isArrayOperator && genericAffectedKey === fieldName + '.$') {
if (parentPositions.length) {
parentPositions.forEach(function (info) {
var childPosition = info.position + '[' + lastPartWithBraces + ']';
if (!positions.find(function (i) {
return i.position === childPosition;
})) {
positions.push({
key: isArrayOperator ? affectedKey.slice(-2) : affectedKey,
value: mongoObject.getValueForPosition(position),
operator: extractOp(position),
position: position
});
}
// If the existing position is a parent and value is set, run for would-be
// child position
var parentPath = (0, _utility.getParentOfKey)(fieldName);
if (genericAffectedKey === parentPath || genericAffectedKey + '.$' === parentPath) {
var lastPart = (0, _utility.getLastPartOfKey)(fieldName, parentPath);
var lastPartWithBraces = lastPart.replace(/\./g, '][');
var childPosition = position + '[' + lastPartWithBraces + ']';
positions.push({
key: affectedKey + '.' + lastPart,
value: mongoObject.getValueForPosition(childPosition),
operator: extractOp(position),
key: info.key + '.' + lastPart,
value: undefined,
operator: info.operator,
position: childPosition
});
}
});
} else if (parentPath.slice(-2) !== '.$') {
// positions that will create parentPath
mongoObject.getPositionsThatCreateGenericKey(parentPath).forEach(function (info) {
var operator = info.operator,
position = info.position;
// If the parent path exists in the object, then we don't need to
// do the next part
if (mongoObject.getPositionsForGenericKey(parentPath).length) return 'continue';
var wouldBePosition = void 0;
if (operator) {
var next = position.slice(position.indexOf('[') + 1, position.indexOf(']'));
var nextPieces = next.split('.');
// If this position will implicitly create the parent object
var objects = objectsThatKeyWillCreate(genericAffectedKey);
if (parentPath.slice(-2) !== '.$' && objects.indexOf(parentPath) > -1) {
if (hasOperator) {
var operator = position.slice(0, position.indexOf('['));
var next = position.slice(position.indexOf('[') + 1, position.indexOf(']'));
var nextPieces = next.split('.');
var newPieces = [];
var newKey = void 0;
while (nextPieces.length && newKey !== parentPath) {
newPieces.push(nextPieces.shift());
newKey = newPieces.join('.');
}
newKey = newKey + '.' + fieldName.slice(newKey.length + 1);
var wouldBePosition = operator + '[' + newKey + ']';
positions.push({
key: _mongoObject2.default._positionToKey(wouldBePosition),
value: mongoObject.getValueForPosition(wouldBePosition),
operator: operator,
position: wouldBePosition
});
} else {
var _lastPart = (0, _utility.getLastPartOfKey)(fieldName, parentPath);
var _lastPartWithBraces = _lastPart.replace(/\./g, '][');
var _wouldBePosition = position.slice(0, position.lastIndexOf('[')) + '[' + _lastPartWithBraces + ']';
positions.push({
key: _mongoObject2.default._positionToKey(_wouldBePosition),
value: mongoObject.getValueForPosition(_wouldBePosition),
operator: null,
position: _wouldBePosition
});
var newPieces = [];
var newKey = void 0;
while (nextPieces.length && newKey !== parentPath) {
newPieces.push(nextPieces.shift());
newKey = newPieces.join('.');
}
newKey = newKey + '.' + fieldName.slice(newKey.length + 1);
wouldBePosition = operator + '[' + newKey + ']';
} else {
var lastPart2 = (0, _utility.getLastPartOfKey)(fieldName, parentPath);
var lastPartWithBraces2 = lastPart2.replace(/\./g, '][');
wouldBePosition = position.slice(0, position.lastIndexOf('[')) + '[' + lastPartWithBraces2 + ']';
}
};
for (var _iterator = Object.getOwnPropertyNames(mongoObject._genericAffectedKeys)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var _ret = _loop();
if (_ret === 'continue') continue;
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
if (!positions.find(function (i) {
return i.position === wouldBePosition;
})) {
positions.push({
key: _mongoObject2.default._positionToKey(wouldBePosition),
value: undefined,
operator: operator,
position: wouldBePosition
});
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
if (fieldName.indexOf('.') === -1 && positions.length === 0) {
positions.push({
key: fieldName,
value: undefined,
operator: isModifier ? '$set' : null,
position: isModifier ? '$set[' + fieldName + ']' : fieldName
});

@@ -178,0 +118,0 @@ }

{
"name": "simpl-schema",
"version": "1.1.2",
"version": "1.2.0",
"description": "A schema validation package that supports direct validation of MongoDB update modifier objects.",

@@ -43,3 +43,3 @@ "author": "Eric Dobbertin <aldeed@gmail.com>",

"message-box": "^0.1.1",
"mongo-object": "^0.0.3"
"mongo-object": "^0.1.1"
},

@@ -46,0 +46,0 @@ "devDependencies": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc