simpl-schema
Advanced tools
Comparing version 1.5.3 to 1.5.4
@@ -49,2 +49,4 @@ 'use strict'; | ||
* @param {Boolean} [options.isModifier=false] - Is doc a modifier object? | ||
* @param {Boolean} [options.isUpsert=false] - Will the modifier object be used to do an upsert? This is used | ||
* to determine whether $setOnInsert should be added to it for defaultValues. | ||
* @param {Boolean} [options.mongoObject] - If you already have the mongoObject instance, pass it to improve performance | ||
@@ -63,3 +65,4 @@ * @param {Object} [options.extendAutoValueContext] - This object will be added to the `this` context of autoValue functions. | ||
options = _extends({ | ||
isModifier: (0, _utility.looksLikeModifier)(doc) | ||
isModifier: (0, _utility.looksLikeModifier)(doc), | ||
isUpsert: false | ||
}, ss._cleanOptions, options); | ||
@@ -157,3 +160,3 @@ | ||
// Set automatic values | ||
options.getAutoValues && (0, _setAutoValues2.default)(ss.autoValueFunctions(), mongoObject, options.isModifier, options.extendAutoValueContext); | ||
options.getAutoValues && (0, _setAutoValues2.default)(ss.autoValueFunctions(), mongoObject, options.isModifier, options.isUpsert, options.extendAutoValueContext); | ||
@@ -160,0 +163,0 @@ // Ensure we don't have any operators set to an empty object |
@@ -56,2 +56,3 @@ 'use strict'; | ||
isModifier = _options.isModifier, | ||
isUpsert = _options.isUpsert, | ||
mongoObject = _options.mongoObject; | ||
@@ -81,2 +82,4 @@ | ||
isModifier: isModifier, | ||
isUpsert: isUpsert, | ||
isSet: value !== undefined, | ||
@@ -83,0 +86,0 @@ key: affectedKey, |
@@ -6,2 +6,3 @@ 'use strict'; | ||
}); | ||
exports.sortAutoValueFunctions = sortAutoValueFunctions; | ||
@@ -19,2 +20,27 @@ var _getPositionsForAutoValue = require('./getPositionsForAutoValue'); | ||
/** | ||
* @method sortAutoValueFunctions | ||
* @private | ||
* @param {Array} autoValueFunctions - Array of objects to be sorted | ||
* @returns {Array} Sorted array | ||
* | ||
* Stable sort of the autoValueFunctions (preserves order at the same field depth). | ||
*/ | ||
function sortAutoValueFunctions(autoValueFunctions) { | ||
var defaultFieldOrder = autoValueFunctions.reduce(function (acc, _ref, index) { | ||
var fieldName = _ref.fieldName; | ||
acc[fieldName] = index; | ||
return acc; | ||
}, {}); | ||
// Sort by how many dots each field name has, asc, such that we can auto-create | ||
// objects and arrays before we run the autoValues for properties within them. | ||
// Fields of the same level (same number of dots) preserve should order from the original array. | ||
return autoValueFunctions.sort(function (a, b) { | ||
var depthDiff = a.fieldName.split('.').length - b.fieldName.split('.').length; | ||
return depthDiff === 0 ? defaultFieldOrder[a.fieldName] - defaultFieldOrder[b.fieldName] : depthDiff; | ||
}); | ||
} | ||
/** | ||
* @method setAutoValues | ||
@@ -31,13 +57,9 @@ * @private | ||
*/ | ||
function setAutoValues(autoValueFunctions, mongoObject, isModifier, extendedAutoValueContext) { | ||
// Sort by how many dots each field name has, asc, such that we can auto-create | ||
// objects and arrays before we run the autoValues for properties within them. | ||
var sortedAutoValueFunctions = autoValueFunctions.sort(function (a, b) { | ||
return a.fieldName.split('.').length - b.fieldName.split('.').length; | ||
}); | ||
function setAutoValues(autoValueFunctions, mongoObject, isModifier, isUpsert, extendedAutoValueContext) { | ||
var sortedAutoValueFunctions = sortAutoValueFunctions(autoValueFunctions); | ||
sortedAutoValueFunctions.forEach(function (_ref) { | ||
var func = _ref.func, | ||
fieldName = _ref.fieldName, | ||
closestSubschemaFieldName = _ref.closestSubschemaFieldName; | ||
sortedAutoValueFunctions.forEach(function (_ref2) { | ||
var func = _ref2.func, | ||
fieldName = _ref2.fieldName, | ||
closestSubschemaFieldName = _ref2.closestSubschemaFieldName; | ||
@@ -49,2 +71,3 @@ var avRunner = new _AutoValueRunner2.default({ | ||
isModifier: isModifier, | ||
isUpsert: isUpsert, | ||
mongoObject: mongoObject | ||
@@ -51,0 +74,0 @@ }); |
@@ -12,26 +12,22 @@ 'use strict'; | ||
var _extend2 = require('extend'); | ||
var _clone = require('clone'); | ||
var _extend3 = _interopRequireDefault(_extend2); | ||
var _clone2 = _interopRequireDefault(_clone); | ||
var _mongoObject = require('mongo-object'); | ||
var _lodash = require('lodash.every'); | ||
var _mongoObject2 = _interopRequireDefault(_mongoObject); | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
var _lodash = require('lodash.omit'); | ||
var _extend2 = require('extend'); | ||
var _lodash2 = _interopRequireDefault(_lodash); | ||
var _extend3 = _interopRequireDefault(_extend2); | ||
var _lodash3 = require('lodash.every'); | ||
var _lodash3 = require('lodash.includes'); | ||
var _lodash4 = _interopRequireDefault(_lodash3); | ||
var _lodash5 = require('lodash.pick'); | ||
var _lodash5 = require('lodash.isempty'); | ||
var _lodash6 = _interopRequireDefault(_lodash5); | ||
var _lodash7 = require('lodash.uniq'); | ||
var _lodash8 = _interopRequireDefault(_lodash7); | ||
var _messageBox = require('message-box'); | ||
@@ -41,9 +37,17 @@ | ||
var _lodash9 = require('lodash.includes'); | ||
var _mongoObject = require('mongo-object'); | ||
var _mongoObject2 = _interopRequireDefault(_mongoObject); | ||
var _lodash7 = require('lodash.omit'); | ||
var _lodash8 = _interopRequireDefault(_lodash7); | ||
var _lodash9 = require('lodash.pick'); | ||
var _lodash10 = _interopRequireDefault(_lodash9); | ||
var _clone = require('clone'); | ||
var _lodash11 = require('lodash.uniq'); | ||
var _clone2 = _interopRequireDefault(_clone); | ||
var _lodash12 = _interopRequireDefault(_lodash11); | ||
@@ -288,3 +292,3 @@ var _humanize = require('./humanize.js'); | ||
return function (prop) { | ||
if (Array.isArray(propList) && !(0, _lodash10.default)(propList, prop)) return; | ||
if (Array.isArray(propList) && !(0, _lodash4.default)(propList, prop)) return; | ||
var val = obj[prop]; | ||
@@ -441,3 +445,3 @@ // For any options that support specifying a function, evaluate the functions | ||
}); | ||
return (0, _lodash8.default)(blackboxKeys); | ||
return (0, _lodash12.default)(blackboxKeys); | ||
} | ||
@@ -593,3 +597,3 @@ | ||
} | ||
_this8._schema[fieldName] = _extends({}, _this8._schema[fieldName], (0, _lodash2.default)(definition, 'type')); | ||
_this8._schema[fieldName] = _extends({}, _this8._schema[fieldName], (0, _lodash8.default)(definition, 'type')); | ||
if (definition.type) _this8._schema[fieldName].type.extend(definition.type); | ||
@@ -627,3 +631,3 @@ } else { | ||
// XXX For now if any oneOf type is blackbox, then the whole field is. | ||
(0, _lodash4.default)(definition.type.definitions, function (oneOfDef) { | ||
(0, _lodash2.default)(definition.type.definitions, function (oneOfDef) { | ||
if (oneOfDef.blackbox === true) { | ||
@@ -682,4 +686,4 @@ _this8._blackboxKeys.push(fieldName); | ||
} | ||
return [].concat(_toConsumableArray(this.get(key, 'allowedValues'))); | ||
var allowedValues = this.get(key, 'allowedValues'); | ||
return (0, _lodash6.default)(allowedValues) ? null : [].concat(_toConsumableArray(allowedValues)); | ||
} | ||
@@ -898,3 +902,3 @@ }, { | ||
if ((0, _lodash10.default)(schemaDefinitionOptions, prop)) { | ||
if ((0, _lodash4.default)(schemaDefinitionOptions, prop)) { | ||
return def[prop]; | ||
@@ -1089,3 +1093,3 @@ } | ||
// Handle the case when pulling an object from an array the object contains a field | ||
// which has a defaultValue. We don't wan't the default value to be returned in this case | ||
// which has a defaultValue. We don't want the default value to be returned in this case | ||
if (this.operator === '$pull') return; | ||
@@ -1100,5 +1104,4 @@ | ||
// We don't know whether it's an upsert, but if it's not, this seems to be ignored, | ||
// so this is a safe way to make sure the default value is added on upsert insert. | ||
return { $setOnInsert: defaultValue }; | ||
// Make sure the default value is added on upsert insert | ||
if (this.isUpsert) return { $setOnInsert: defaultValue }; | ||
}; | ||
@@ -1109,3 +1112,3 @@ } | ||
function standardizeDefinition(def) { | ||
var standardizedDef = (0, _lodash2.default)(def, oneOfProps); | ||
var standardizedDef = (0, _lodash8.default)(def, oneOfProps); | ||
@@ -1118,3 +1121,3 @@ // Internally, all definition types are stored as groups for simplicity of access. | ||
} else { | ||
var groupProps = (0, _lodash6.default)(def, oneOfProps); | ||
var groupProps = (0, _lodash10.default)(def, oneOfProps); | ||
standardizedDef.type = new _SimpleSchemaGroup2.default(groupProps); | ||
@@ -1148,2 +1151,6 @@ } | ||
if (type.constructor === Object && (0, _lodash6.default)(type)) { | ||
throw new Error('Invalid definition for ' + fieldName + ' field: "type" may not be an object. Change it to Object'); | ||
} | ||
if (type === Array) couldBeArray = true; | ||
@@ -1150,0 +1157,0 @@ |
{ | ||
"name": "simpl-schema", | ||
"version": "1.5.3", | ||
"version": "1.5.4", | ||
"description": "A schema validation package that supports direct validation of MongoDB update modifier objects.", | ||
@@ -60,3 +60,3 @@ "author": "Eric Dobbertin <aldeed@gmail.com>", | ||
"expect": "^1.20.1", | ||
"mocha": "^2.5.3" | ||
"mocha": "^5.2.0" | ||
}, | ||
@@ -63,0 +63,0 @@ "babel": { |
@@ -684,2 +684,15 @@ # SimpleSchema (simpl-schema NPM package) | ||
**Note**: If you wish to restrict the items allowed in an `Array`, the `allowedValues` property must be on the array item definition. | ||
```javascript | ||
const schema = new SimpleSchema({ | ||
myArray: { | ||
type: Array | ||
}, | ||
'myArray.$': { | ||
type: String, | ||
allowedValues: ['foo', 'bar'] | ||
} | ||
}); | ||
``` | ||
### regEx | ||
@@ -686,0 +699,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
176036
2639
1318