simpl-schema
Advanced tools
Comparing version 1.3.0 to 1.4.0
@@ -11,5 +11,5 @@ 'use strict'; | ||
var _mongoObject = require('mongo-object'); | ||
var _clone = require('clone'); | ||
var _mongoObject2 = _interopRequireDefault(_mongoObject); | ||
var _clone2 = _interopRequireDefault(_clone); | ||
@@ -20,2 +20,6 @@ var _lodash = require('lodash.isempty'); | ||
var _mongoObject = require('mongo-object'); | ||
var _mongoObject2 = _interopRequireDefault(_mongoObject); | ||
var _utility = require('./utility'); | ||
@@ -33,6 +37,2 @@ | ||
var _clone = require('clone'); | ||
var _clone2 = _interopRequireDefault(_clone); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -39,0 +39,0 @@ |
@@ -13,4 +13,6 @@ 'use strict'; | ||
var _utility = require('../utility'); | ||
var _clone = require('clone'); | ||
var _clone2 = _interopRequireDefault(_clone); | ||
var _lodash = require('lodash.includes'); | ||
@@ -20,2 +22,4 @@ | ||
var _utility = require('../utility'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -127,3 +131,3 @@ | ||
mongoObject.removeValueForPosition(position); | ||
mongoObject.setValueForPosition(op + '[' + affectedKey + ']', newValue); | ||
mongoObject.setValueForPosition(op + '[' + affectedKey + ']', (0, _clone2.default)(newValue)); | ||
return; | ||
@@ -133,4 +137,5 @@ } | ||
// Update/change value | ||
mongoObject.setValueForPosition(position, autoValue); | ||
// Update/change value. Cloning is necessary in case it's an object, because | ||
// if we later set some keys within it, they'd be set on the original object, too. | ||
mongoObject.setValueForPosition(position, (0, _clone2.default)(autoValue)); | ||
} | ||
@@ -137,0 +142,0 @@ }]); |
@@ -19,4 +19,4 @@ 'use strict'; | ||
function convertToProperType(value, type) { | ||
// Can't and shouldn't convert arrays or objects | ||
if (Array.isArray(value) || value && (typeof value === 'function' || (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object') && !(value instanceof Date)) return value; | ||
// Can't and shouldn't convert arrays or objects or null | ||
if (Array.isArray(value) || value && (typeof value === 'function' || (typeof value === 'undefined' ? 'undefined' : _typeof(value)) === 'object') && !(value instanceof Date) || value === null) return value; | ||
@@ -23,0 +23,0 @@ // Convert to String type |
@@ -98,3 +98,3 @@ 'use strict'; | ||
newKey = newKey + '.' + fieldName.slice(newKey.length + 1); | ||
wouldBePosition = operator + '[' + newKey + ']'; | ||
wouldBePosition = '$set[' + newKey + ']'; | ||
} else { | ||
@@ -111,3 +111,3 @@ var lastPart2 = (0, _utility.getLastPartOfKey)(fieldName, parentPath); | ||
value: undefined, | ||
operator: operator, | ||
operator: operator ? '$set' : null, | ||
position: wouldBePosition | ||
@@ -114,0 +114,0 @@ }); |
@@ -252,3 +252,2 @@ 'use strict'; | ||
affectedKeyGeneric = _mongoObject2.default.makeKeyGeneric(affectedKey); | ||
def = schema.getDefinition(affectedKey); | ||
@@ -260,2 +259,3 @@ var shouldValidateKey = !keysToValidate || keysToValidate.some(function (keyToValidate) { | ||
// Perform validation for this key | ||
def = schema.getDefinition(affectedKey); | ||
if (shouldValidateKey) { | ||
@@ -262,0 +262,0 @@ validate(val, affectedKey, affectedKeyGeneric, def, operator, isInArrayItemObject, isInSubObject); |
@@ -92,4 +92,13 @@ 'use strict'; | ||
var schema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, | ||
check = _ref.check, | ||
cleanOptions = _ref.clean, | ||
defaultLabel = _ref.defaultLabel, | ||
_ref$humanizeAutoLabe = _ref.humanizeAutoLabels, | ||
humanizeAutoLabels = _ref$humanizeAutoLabe === undefined ? true : _ref$humanizeAutoLabe, | ||
_ref$requiredByDefaul = _ref.requiredByDefault, | ||
requiredByDefault = _ref$requiredByDefaul === undefined ? true : _ref$requiredByDefaul, | ||
tracker = _ref.tracker; | ||
_classCallCheck(this, SimpleSchema); | ||
@@ -101,4 +110,9 @@ | ||
// Stash the options object | ||
this._constructorOptions = _extends({}, options); | ||
if (this._constructorOptions.humanizeAutoLabels !== false) this._constructorOptions.humanizeAutoLabels = true; | ||
this._constructorOptions = { | ||
check: check, | ||
defaultLabel: defaultLabel, | ||
humanizeAutoLabels: humanizeAutoLabels, | ||
requiredByDefault: requiredByDefault, | ||
tracker: tracker | ||
}; | ||
@@ -121,3 +135,3 @@ // Custom validators for this instance | ||
extendAutoValueContext: {} | ||
}, options.clean); | ||
}, cleanOptions); | ||
@@ -136,4 +150,4 @@ // Clone, expanding shorthand, and store the schema object in this._schema | ||
_createClass(SimpleSchema, [{ | ||
key: 'findFirstAncestorSimpleSchema', | ||
value: function findFirstAncestorSimpleSchema(key, func) { | ||
key: 'forEachAncestorSimpleSchema', | ||
value: function forEachAncestorSimpleSchema(key, func) { | ||
var _this = this; | ||
@@ -143,5 +157,3 @@ | ||
var foundSchema = false; | ||
(0, _utility.forEachKeyAncestor)(genericKey, function (ancestor) { | ||
if (foundSchema) return; // skip remaining once we've found it | ||
var def = _this._schema[ancestor]; | ||
@@ -152,8 +164,5 @@ if (!def) return; | ||
func(typeDef.type, ancestor, genericKey.slice(ancestor.length + 1)); | ||
foundSchema = true; | ||
} | ||
}); | ||
}); | ||
return foundSchema; | ||
} | ||
@@ -192,3 +201,3 @@ | ||
// If in subschema | ||
this.findFirstAncestorSimpleSchema(key, function (simpleSchema, ancestor, subSchemaKey) { | ||
this.forEachAncestorSimpleSchema(key, function (simpleSchema, ancestor, subSchemaKey) { | ||
// Pass tracker down so that we get reactivity even if the subschema | ||
@@ -219,4 +228,6 @@ // didn't have tracker option set | ||
if (!keySchema) { | ||
this.findFirstAncestorSimpleSchema(key, function (simpleSchema, ancestor, subSchemaKey) { | ||
keySchema = simpleSchema.schema(subSchemaKey); | ||
var found = false; | ||
this.forEachAncestorSimpleSchema(key, function (simpleSchema, ancestor, subSchemaKey) { | ||
if (!found) keySchema = simpleSchema.schema(subSchemaKey); | ||
if (keySchema) found = true; | ||
}); | ||
@@ -244,3 +255,3 @@ } | ||
Object.keys(this._schema).forEach(function (key) { | ||
this._schemaKeys.forEach(function (key) { | ||
var keySchema = _this2._schema[key]; | ||
@@ -272,5 +283,7 @@ mergedSchema[key] = keySchema; | ||
key: 'getDefinition', | ||
value: function getDefinition(key, propList, functionContext) { | ||
value: function getDefinition(key, propList) { | ||
var _this3 = this; | ||
var functionContext = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var defs = this.schema(key); | ||
@@ -285,3 +298,5 @@ if (!defs) return; | ||
if (propsThatCanBeFunction.indexOf(prop) > -1 && typeof val === 'function') { | ||
newObj[prop] = val.call(functionContext || {}); | ||
newObj[prop] = val.call(_extends({ | ||
key: key | ||
}, functionContext)); | ||
// Inflect label if undefined | ||
@@ -383,3 +398,3 @@ if (prop === 'label' && typeof newObj[prop] !== 'string') newObj[prop] = inflectedLabel(key, _this3._constructorOptions.humanizeAutoLabels); | ||
return new SimpleSchema(newSchemaDef, this._constructorOptions); | ||
return this._copyWithSchema(newSchemaDef); | ||
} | ||
@@ -395,23 +410,11 @@ | ||
var result = []; | ||
var result = [].concat(this._autoValues); | ||
function addFuncs(autoValues, closestSubschemaFieldName) { | ||
Object.keys(autoValues).forEach(function (fieldName) { | ||
result.push({ | ||
func: autoValues[fieldName], | ||
fieldName: fieldName, | ||
closestSubschemaFieldName: closestSubschemaFieldName | ||
}); | ||
}); | ||
} | ||
addFuncs(this._autoValues, ''); | ||
Object.keys(this._schema).forEach(function (key) { | ||
this._schemaKeys.forEach(function (key) { | ||
_this4._schema[key].type.definitions.forEach(function (typeDef) { | ||
if (!SimpleSchema.isSimpleSchema(typeDef.type)) return; | ||
result = result.concat(typeDef.type.autoValueFunctions().map(function (_ref) { | ||
var func = _ref.func, | ||
fieldName = _ref.fieldName, | ||
closestSubschemaFieldName = _ref.closestSubschemaFieldName; | ||
result = result.concat(typeDef.type.autoValueFunctions().map(function (_ref2) { | ||
var func = _ref2.func, | ||
fieldName = _ref2.fieldName, | ||
closestSubschemaFieldName = _ref2.closestSubschemaFieldName; | ||
@@ -438,3 +441,3 @@ return { | ||
var blackboxKeys = this._blackboxKeys; | ||
Object.keys(this._schema).forEach(function (key) { | ||
this._schemaKeys.forEach(function (key) { | ||
_this5._schema[key].type.definitions.forEach(function (typeDef) { | ||
@@ -532,2 +535,32 @@ if (!SimpleSchema.isSimpleSchema(typeDef.type)) return; | ||
/** | ||
* Copies this schema into a new instance with the same validators, messages, | ||
* and options, but with different keys as defined in `schema` argument | ||
* | ||
* @param {Object} schema | ||
* @returns The new SimpleSchema instance (chainable) | ||
*/ | ||
}, { | ||
key: '_copyWithSchema', | ||
value: function _copyWithSchema(schema) { | ||
var cl = new SimpleSchema(schema, (0, _clone2.default)(this._constructorOptions, false, 1)); | ||
cl._cleanOptions = this._cleanOptions; | ||
cl.messageBox = this.messageBox.clone(); | ||
return cl; | ||
} | ||
/** | ||
* Clones this schema into a new instance with the same schema keys, validators, | ||
* and options. | ||
* | ||
* @returns The new SimpleSchema instance (chainable) | ||
*/ | ||
}, { | ||
key: 'clone', | ||
value: function clone() { | ||
return this._copyWithSchema(this._schema); | ||
} | ||
/** | ||
* Extends (mutates) this schema with another schema, key by key. | ||
@@ -553,4 +586,4 @@ * | ||
this._docValidators = this._docValidators.concat(schema._docValidators); | ||
this._cleanOptions = (0, _extend3.default)(true, this._cleanOptions, schema._cleanOptions); | ||
this._constructorOptions = (0, _extend3.default)(true, this._constructorOptions, schema._constructorOptions); | ||
this._cleanOptions = (0, _extend3.default)(false, this._cleanOptions, schema._cleanOptions); | ||
this._constructorOptions = (0, _extend3.default)(false, this._constructorOptions, schema._constructorOptions); | ||
} else { | ||
@@ -582,4 +615,4 @@ schemaObj = (0, _expandShorthand2.default)(schema); | ||
// Set/Reset all of these | ||
this._schemaKeys = []; | ||
this._autoValues = {}; | ||
this._schemaKeys = Object.keys(this._schema); | ||
this._autoValues = []; | ||
this._blackboxKeys = []; | ||
@@ -590,8 +623,5 @@ this._firstLevelSchemaKeys = []; | ||
// Update all of the information cached on the instance | ||
Object.keys(this._schema).forEach(function (fieldName) { | ||
this._schemaKeys.forEach(function (fieldName) { | ||
var definition = _this8._schema[fieldName]; | ||
// Keep list of all keys for speedier checking | ||
_this8._schemaKeys.push(fieldName); | ||
// Keep list of all top level keys | ||
@@ -610,4 +640,10 @@ if (fieldName.indexOf('.') === -1) _this8._firstLevelSchemaKeys.push(fieldName); | ||
// Keep list of autoValue functions by key | ||
if (definition.autoValue) _this8._autoValues[fieldName] = definition.autoValue; | ||
// Keep list of autoValue functions | ||
if (typeof definition.autoValue === 'function') { | ||
_this8._autoValues.push({ | ||
closestSubschemaFieldName: '', | ||
fieldName: fieldName, | ||
func: definition.autoValue | ||
}); | ||
} | ||
}); | ||
@@ -630,4 +666,4 @@ | ||
// iterate over the child fields and cache their properties as well | ||
definition.type.definitions.forEach(function (_ref2) { | ||
var type = _ref2.type; | ||
definition.type.definitions.forEach(function (_ref3) { | ||
var type = _ref3.type; | ||
@@ -653,5 +689,3 @@ if (SimpleSchema.isSimpleSchema(type)) { | ||
var defs = this.getDefinition(key, ['allowedValues']); | ||
return defs && defs.type[0].allowedValues; | ||
return this.get(key, 'allowedValues'); | ||
} | ||
@@ -668,3 +702,3 @@ }, { | ||
if (!this._validationContexts[name]) { | ||
this._validationContexts[name] = new SimpleSchema.ValidationContext(this); | ||
this._validationContexts[name] = new _ValidationContext2.default(this); | ||
} | ||
@@ -693,10 +727,13 @@ return this._validationContexts[name]; | ||
key: 'validate', | ||
value: function validate(obj, options) { | ||
value: function validate(obj) { | ||
var _this9 = this; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
// For Meteor apps, `check` option can be passed to silence audit-argument-checks | ||
if (typeof this._constructorOptions.check === 'function') { | ||
var check = options.check || this._constructorOptions.check; | ||
if (typeof check === 'function') { | ||
// Call check but ignore the error | ||
try { | ||
this._constructorOptions.check(obj); | ||
check(obj); | ||
} catch (e) {/* ignore error */} | ||
@@ -848,7 +885,5 @@ } | ||
// Get label for one field | ||
var def = this.getDefinition(key, ['label']); | ||
if (!def) return null; | ||
this.reactiveLabelDependency(key); | ||
return def.label; | ||
var label = this.get(key, 'label'); | ||
if (label) this.reactiveLabelDependency(key); | ||
return label || null; | ||
} | ||
@@ -859,6 +894,5 @@ | ||
* | ||
* @param {String} [key] The schema key, specific or generic. | ||
* Omit this argument to get a dictionary of all labels. | ||
* @param {String} [prop] Name of the property to get. | ||
* | ||
* @param {String} key The schema key, specific or generic. | ||
* @param {String} prop Name of the property to get for that schema key | ||
* @param {Object} [functionContext] The `this` context to use if prop is a function | ||
* @returns {any} The property value | ||
@@ -869,4 +903,4 @@ */ | ||
key: 'get', | ||
value: function get(key, prop) { | ||
var def = this.getDefinition(key, ['type', prop]); | ||
value: function get(key, prop, functionContext) { | ||
var def = this.getDefinition(key, ['type', prop], functionContext); | ||
@@ -1106,4 +1140,4 @@ if (!def) return undefined; | ||
// Make sure the `type`s are OK | ||
definition.type.definitions.forEach(function (_ref3) { | ||
var type = _ref3.type; | ||
definition.type.definitions.forEach(function (_ref4) { | ||
var type = _ref4.type; | ||
@@ -1189,3 +1223,3 @@ if (!type) throw new Error('Invalid definition for ' + fieldName + ' field: "type" option is required'); | ||
var newSchema = {}; | ||
Object.keys(this._schema).forEach(function (key) { | ||
this._schemaKeys.forEach(function (key) { | ||
// Pick/omit it if it IS in the array of keys they want OR if it | ||
@@ -1202,5 +1236,3 @@ // STARTS WITH something that is in the array plus a period | ||
var subSchema = new SimpleSchema(newSchema, this._constructorOptions); | ||
subSchema.messageBox = this.messageBox; | ||
return subSchema; | ||
return this._copyWithSchema(newSchema); | ||
}; | ||
@@ -1207,0 +1239,0 @@ } |
@@ -6,87 +6,75 @@ 'use strict'; | ||
}); | ||
exports.looksLikeModifier = exports.isObjectWeShouldTraverse = exports.getParentOfKey = exports.getLastPartOfKey = exports.forEachKeyAncestor = exports.dateToDateString = exports.appendAffectedKey = undefined; | ||
var _getLastPartOfKey = require('./getLastPartOfKey'); | ||
var _appendAffectedKey = require('./appendAffectedKey'); | ||
var _getLastPartOfKey2 = _interopRequireDefault(_getLastPartOfKey); | ||
Object.defineProperty(exports, 'appendAffectedKey', { | ||
enumerable: true, | ||
get: function get() { | ||
return _interopRequireDefault(_appendAffectedKey).default; | ||
} | ||
}); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var _dateToDateString = require('./dateToDateString'); | ||
function appendAffectedKey(affectedKey, key) { | ||
if (key === '$each') return affectedKey; | ||
return affectedKey ? affectedKey + '.' + key : key; | ||
} | ||
Object.defineProperty(exports, 'dateToDateString', { | ||
enumerable: true, | ||
get: function get() { | ||
return _interopRequireDefault(_dateToDateString).default; | ||
} | ||
}); | ||
function dateToDateString(date) { | ||
var m = date.getUTCMonth() + 1; | ||
if (m < 10) m = '0' + m; | ||
var d = date.getUTCDate(); | ||
if (d < 10) d = '0' + d; | ||
return date.getUTCFullYear() + '-' + m + '-' + d; | ||
} | ||
var _forEachKeyAncestor = require('./forEachKeyAncestor'); | ||
function isObjectWeShouldTraverse(val) { | ||
// Some of these types don't exist in old browsers so we'll catch and return false in those cases | ||
try { | ||
if (val !== Object(val)) return false; | ||
// There are some object types that we know we shouldn't traverse because | ||
// they will often result in overflows and it makes no sense to validate them. | ||
if (val instanceof Date) return false; | ||
if (val instanceof Int8Array) return false; | ||
if (val instanceof Uint8Array) return false; | ||
if (val instanceof Uint8ClampedArray) return false; | ||
if (val instanceof Int16Array) return false; | ||
if (val instanceof Uint16Array) return false; | ||
if (val instanceof Int32Array) return false; | ||
if (val instanceof Uint32Array) return false; | ||
if (val instanceof Float32Array) return false; | ||
if (val instanceof Float64Array) return false; | ||
} catch (e) { | ||
return false; | ||
Object.defineProperty(exports, 'forEachKeyAncestor', { | ||
enumerable: true, | ||
get: function get() { | ||
return _interopRequireDefault(_forEachKeyAncestor).default; | ||
} | ||
}); | ||
return true; | ||
} | ||
var _getKeysWithValueInObj = require('./getKeysWithValueInObj'); | ||
function looksLikeModifier(obj) { | ||
return !!Object.keys(obj || {}).find(function (key) { | ||
return key.substring(0, 1) === '$'; | ||
}); | ||
} | ||
Object.defineProperty(exports, 'getKeysWithValueInObj', { | ||
enumerable: true, | ||
get: function get() { | ||
return _interopRequireDefault(_getKeysWithValueInObj).default; | ||
} | ||
}); | ||
/** | ||
* Run loopFunc for each ancestor key in a dot-delimited key. For example, | ||
* if key is "a.b.c", loopFunc will be called first with ('a.b', 'c') and then with ('a', 'b.c') | ||
*/ | ||
function forEachKeyAncestor(key, loopFunc) { | ||
var lastDot = void 0; | ||
var _getLastPartOfKey = require('./getLastPartOfKey'); | ||
// Iterate the dot-syntax hierarchy | ||
var ancestor = key; | ||
do { | ||
lastDot = ancestor.lastIndexOf('.'); | ||
if (lastDot !== -1) { | ||
ancestor = ancestor.slice(0, lastDot); | ||
var remainder = key.slice(ancestor.length + 1); | ||
loopFunc(ancestor, remainder); // Remove last path component | ||
} | ||
} while (lastDot !== -1); | ||
} | ||
Object.defineProperty(exports, 'getLastPartOfKey', { | ||
enumerable: true, | ||
get: function get() { | ||
return _interopRequireDefault(_getLastPartOfKey).default; | ||
} | ||
}); | ||
/** | ||
* Returns the parent of a key. For example, returns 'a.b' when passed 'a.b.c'. | ||
* If no parent, returns an empty string. If withEndDot is true, the return | ||
* value will have a dot appended when it isn't an empty string. | ||
*/ | ||
function getParentOfKey(key, withEndDot) { | ||
var lastDot = key.lastIndexOf('.'); | ||
return lastDot === -1 ? '' : key.slice(0, lastDot + Number(!!withEndDot)); | ||
} | ||
var _getParentOfKey = require('./getParentOfKey'); | ||
exports.appendAffectedKey = appendAffectedKey; | ||
exports.dateToDateString = dateToDateString; | ||
exports.forEachKeyAncestor = forEachKeyAncestor; | ||
exports.getLastPartOfKey = _getLastPartOfKey2.default; | ||
exports.getParentOfKey = getParentOfKey; | ||
exports.isObjectWeShouldTraverse = isObjectWeShouldTraverse; | ||
exports.looksLikeModifier = looksLikeModifier; | ||
Object.defineProperty(exports, 'getParentOfKey', { | ||
enumerable: true, | ||
get: function get() { | ||
return _interopRequireDefault(_getParentOfKey).default; | ||
} | ||
}); | ||
var _isObjectWeShouldTraverse = require('./isObjectWeShouldTraverse'); | ||
Object.defineProperty(exports, 'isObjectWeShouldTraverse', { | ||
enumerable: true, | ||
get: function get() { | ||
return _interopRequireDefault(_isObjectWeShouldTraverse).default; | ||
} | ||
}); | ||
var _looksLikeModifier = require('./looksLikeModifier'); | ||
Object.defineProperty(exports, 'looksLikeModifier', { | ||
enumerable: true, | ||
get: function get() { | ||
return _interopRequireDefault(_looksLikeModifier).default; | ||
} | ||
}); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } |
@@ -6,2 +6,3 @@ 'use strict'; | ||
}); | ||
exports.default = allowedValuesValidator; | ||
@@ -24,4 +25,2 @@ var _SimpleSchema = require('../SimpleSchema'); | ||
return isAllowed ? true : _SimpleSchema.SimpleSchema.ErrorTypes.VALUE_NOT_ALLOWED; | ||
} | ||
exports.default = allowedValuesValidator; | ||
} |
@@ -6,12 +6,7 @@ 'use strict'; | ||
}); | ||
exports.default = requiredValidator; | ||
var _SimpleSchema = require('../SimpleSchema'); | ||
function getMapIterator(obj, matchKey, keyAdjust) { | ||
return function (key) { | ||
var val = obj[key]; | ||
if (val === undefined || val === null) return null; | ||
if (keyAdjust(key) === matchKey) return key; | ||
}; | ||
} | ||
var _utility = require('../utility'); | ||
@@ -27,52 +22,45 @@ // Check for missing required values. The general logic is this: | ||
function requiredValidator() { | ||
var _this = this; | ||
var definition = this.definition, | ||
isInArrayItemObject = this.isInArrayItemObject, | ||
isInSubObject = this.isInSubObject, | ||
key = this.key, | ||
obj = this.obj, | ||
operator = this.operator, | ||
value = this.value; | ||
var optional = definition.optional; | ||
if (this.definition.optional) return; | ||
if (optional) return; | ||
// If value is null, no matter what, we add required | ||
if (this.value === null) return _SimpleSchema.SimpleSchema.ErrorTypes.REQUIRED; | ||
if (value === null) return _SimpleSchema.SimpleSchema.ErrorTypes.REQUIRED; | ||
// If operator would remove, we add required | ||
if (this.operator === '$unset' || this.operator === '$rename') return _SimpleSchema.SimpleSchema.ErrorTypes.REQUIRED; | ||
if (operator === '$unset' || operator === '$rename') return _SimpleSchema.SimpleSchema.ErrorTypes.REQUIRED; | ||
// The rest of these apply only if the value is undefined | ||
if (this.value !== undefined) return; | ||
if (value !== undefined) return; | ||
// At this point, if it's a normal, non-modifier object, then a missing value is an error | ||
if (!this.operator) return _SimpleSchema.SimpleSchema.ErrorTypes.REQUIRED; | ||
if (!operator) return _SimpleSchema.SimpleSchema.ErrorTypes.REQUIRED; | ||
// Everything beyond this point deals with modifier objects only | ||
// We can skip the required check for keys that are ancestors | ||
// of those in $set or $setOnInsert because they will be created | ||
// by MongoDB while setting. | ||
var obj = this.obj; | ||
var keysWithValueInSet = Object.keys(obj.$set || {}).map(getMapIterator(obj.$set, this.key + '.', function (k) { | ||
return k.slice(0, _this.key.length + 1); | ||
})).filter(function (v) { | ||
return !!v; | ||
}); | ||
var keysWithValueInSetOnInsert = Object.keys(obj.$setOnInsert || {}).map(getMapIterator(obj.$setOnInsert, this.key + '.', function (k) { | ||
return k.slice(0, _this.key.length + 1); | ||
})).filter(function (v) { | ||
return !!v; | ||
}); | ||
var willBeCreatedAutomatically = keysWithValueInSet.concat(keysWithValueInSetOnInsert).some(function (v) { | ||
return v; | ||
}); | ||
if (willBeCreatedAutomatically) return; | ||
// We can skip the required check for keys that are ancestors of those in $set or | ||
// $setOnInsert because they will be created by MongoDB while setting. | ||
var keysWithValueInSet = (0, _utility.getKeysWithValueInObj)(obj.$set, key); | ||
if (keysWithValueInSet.length) return; | ||
var keysWithValueInSetOnInsert = (0, _utility.getKeysWithValueInObj)(obj.$setOnInsert, key); | ||
if (keysWithValueInSetOnInsert.length) return; | ||
// In the case of $set and $setOnInsert, the value may be undefined here | ||
// but it is set in another operator. So check that first. | ||
var fieldInfo = this.field(this.key); | ||
var fieldInfo = this.field(key); | ||
if (fieldInfo.isSet && fieldInfo.value !== null) return; | ||
// Required if in an array or sub object | ||
if (this.isInArrayItemObject || this.isInSubObject) return _SimpleSchema.SimpleSchema.ErrorTypes.REQUIRED; | ||
if (isInArrayItemObject || isInSubObject) return _SimpleSchema.SimpleSchema.ErrorTypes.REQUIRED; | ||
// If we've got this far with an undefined $set or $setOnInsert value, | ||
// it's a required error. | ||
if (this.operator === '$set' || this.operator === '$setOnInsert') return _SimpleSchema.SimpleSchema.ErrorTypes.REQUIRED; | ||
} | ||
exports.default = requiredValidator; | ||
// If we've got this far with an undefined $set or $setOnInsert value, it's a required error. | ||
if (operator === '$set' || operator === '$setOnInsert') return _SimpleSchema.SimpleSchema.ErrorTypes.REQUIRED; | ||
} |
{ | ||
"name": "simpl-schema", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "A schema validation package that supports direct validation of MongoDB update modifier objects.", | ||
@@ -24,3 +24,4 @@ "author": "Eric Dobbertin <aldeed@gmail.com>", | ||
"lint": "eslint .", | ||
"prepare": "npm run lint && npm test && npm run build && (cd ../tests/meteor-app && meteor npm i && npm test)", | ||
"lintAndTest": "npm run lint && npm test", | ||
"prepublishOnly": "npm run lintAndTest && npm run build", | ||
"test": "mocha --compilers js:babel-core/register --recursive \"lib/**/*.tests.js\"", | ||
@@ -42,3 +43,3 @@ "test:watch": "npm test -- --watch" | ||
"lodash.uniq": "^4.5.0", | ||
"message-box": "^0.1.1", | ||
"message-box": "^0.2.0", | ||
"mongo-object": "^0.1.1" | ||
@@ -62,3 +63,6 @@ }, | ||
"babel": { | ||
"presets": ["es2015", "stage-0"] | ||
"presets": [ | ||
"es2015", | ||
"stage-0" | ||
] | ||
}, | ||
@@ -78,3 +82,6 @@ "eslintConfig": { | ||
"no-underscore-dangle": 0, | ||
"no-use-before-define": [2, "nofunc"], | ||
"no-use-before-define": [ | ||
2, | ||
"nofunc" | ||
], | ||
"no-unused-expressions": 0, | ||
@@ -81,0 +88,0 @@ "no-console": 0, |
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
119670
31
2672
+ Addedmessage-box@0.2.7(transitive)
- Removedlodash.merge@4.6.2(transitive)
- Removedmessage-box@0.1.1(transitive)
Updatedmessage-box@^0.2.0