simpl-schema
Advanced tools
Comparing version 0.0.3 to 0.0.4
@@ -104,3 +104,3 @@ 'use strict'; | ||
var outerDef = ss._schema[gKey]; | ||
var outerDef = ss.schema(gKey); | ||
var def = outerDef && outerDef.type.definitions[0]; | ||
@@ -107,0 +107,0 @@ |
@@ -133,36 +133,29 @@ 'use strict'; | ||
// We always need to start by checking the array itself in case | ||
// it has items that have objects that need autoValue run. If not, we | ||
// will later check for the exact field name. | ||
if (isOrIsWithinArray) test = fieldName.slice(0, lastDollar + 1); | ||
// Starting from the whole dotted field name for which the autoValue function | ||
// is defined, work backwards until finding one that is set. | ||
while (test.length > 0) { | ||
var currentPositions = mongoObject.getPositionsInfoForGenericKey(test); | ||
if (fieldName !== test && currentPositions.length > 0) { | ||
(function () { | ||
var lastPart = ''; | ||
if (fieldName.indexOf(test + '.') === 0) { | ||
lastPart = fieldName.replace(test + '.', ''); | ||
if (lastPart.startsWith('$.')) lastPart = lastPart.slice(2); | ||
} | ||
currentPositions = _underscore2.default.map(currentPositions, function (position) { | ||
position.key = position.key + '.' + lastPart; | ||
position.position = position.position + '[' + lastPart.replace(/\./g, '][') + ']'; | ||
position.value = mongoObject.getValueForPosition(position.position); | ||
return position; | ||
}); | ||
})(); | ||
} | ||
positions = positions.concat(currentPositions); | ||
while (positions.length === 0 && test.length > 0) { | ||
positions = mongoObject.getPositionsInfoForGenericKey(test); | ||
if (positions.length > 0) { | ||
if (fieldName !== test) { | ||
if (fieldName.indexOf('.$.') > -1) { | ||
(function () { | ||
var lastPart = ''; | ||
if (fieldName.indexOf(test + '.') === 0) { | ||
lastPart = fieldName.replace(test + '.', ''); | ||
} | ||
positions = _underscore2.default.map(positions, function (position) { | ||
position.key = position.key + '.' + lastPart; | ||
position.position = position.position + '[' + lastPart + ']'; | ||
position.value = mongoObject.getValueForPosition(position.position); | ||
return position; | ||
}); | ||
})(); | ||
} else { | ||
positions = []; | ||
break; | ||
} | ||
} | ||
// Do the parent | ||
lastDot = test.lastIndexOf('.'); | ||
if (lastDot > -1) { | ||
test = test.slice(0, lastDot); | ||
} else { | ||
lastDot = test.lastIndexOf('.'); | ||
if (lastDot > -1) { | ||
test = test.slice(0, lastDot); | ||
} else { | ||
test = ''; | ||
} | ||
test = ''; | ||
} | ||
@@ -172,6 +165,3 @@ } | ||
if (positions.length === 0) { | ||
if (isOrIsWithinArray) { | ||
positions = mongoObject.getPositionsInfoForGenericKey(fieldName); | ||
} else { | ||
// Not set directly or indirectly | ||
if (!isOrIsWithinArray) { | ||
positions.push({ | ||
@@ -178,0 +168,0 @@ key: fieldName, |
@@ -51,2 +51,3 @@ 'use strict'; | ||
var schema = _ref.schema; | ||
var validationContext = _ref.validationContext; | ||
@@ -146,2 +147,3 @@ // First do some basic checks of the object, and throw errors if necessary | ||
validationContext: validationContext, | ||
value: val, | ||
@@ -148,0 +150,0 @@ // Value checks are not necessary for null or undefined values, |
@@ -8,4 +8,2 @@ 'use strict'; | ||
require('babel-polyfill'); | ||
var _SimpleSchema = require('./SimpleSchema'); | ||
@@ -12,0 +10,0 @@ |
@@ -28,2 +28,4 @@ 'use strict'; | ||
Email: /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/, | ||
// Like Email but requires the TLD (.com, etc) | ||
EmailWithTLD: /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+(?:\.[A-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[A-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[A-z0-9]{2,}(?:[a-z0-9-]*[a-z0-9])?$/, | ||
@@ -46,3 +48,3 @@ Domain: new RegExp('^' + rxDomain + '$'), | ||
ZipCode: /^\d{5}(?:[-\s]\d{4})?$/, | ||
// taken from google's libphonenumber library | ||
// taken from Google's libphonenumber library | ||
// https://github.com/googlei18n/libphonenumber/blob/master/javascript/i18n/phonenumbers/phonenumberutil.js | ||
@@ -49,0 +51,0 @@ // reference the VALID_PHONE_NUMBER_PATTERN key |
@@ -14,2 +14,6 @@ 'use strict'; | ||
var _deepExtend = require('deep-extend'); | ||
var _deepExtend2 = _interopRequireDefault(_deepExtend); | ||
var _mongoObject = require('mongo-object'); | ||
@@ -19,2 +23,10 @@ | ||
var _underscore = require('underscore'); | ||
var _underscore2 = _interopRequireDefault(_underscore); | ||
var _messageBox = require('message-box'); | ||
var _messageBox2 = _interopRequireDefault(_messageBox); | ||
var _humanize = require('./humanize.js'); | ||
@@ -36,10 +48,2 @@ | ||
var _underscore = require('underscore'); | ||
var _underscore2 = _interopRequireDefault(_underscore); | ||
var _messageBox = require('message-box'); | ||
var _messageBox2 = _interopRequireDefault(_messageBox); | ||
var _clean2 = require('./clean'); | ||
@@ -55,2 +59,6 @@ | ||
var _defaultMessages = require('./defaultMessages'); | ||
var _defaultMessages2 = _interopRequireDefault(_defaultMessages); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -69,4 +77,2 @@ | ||
var regExpMessages = [{ exp: _regExp2.default.Email, msg: 'must be a valid email address' }, { exp: _regExp2.default.WeakEmail, msg: 'must be a valid email address' }, { exp: _regExp2.default.Domain, msg: 'must be a valid domain' }, { exp: _regExp2.default.WeakDomain, msg: 'must be a valid domain' }, { exp: _regExp2.default.IP, msg: 'must be a valid IPv4 or IPv6 address' }, { exp: _regExp2.default.IPv4, msg: 'must be a valid IPv4 address' }, { exp: _regExp2.default.IPv6, msg: 'must be a valid IPv6 address' }, { exp: _regExp2.default.Url, msg: 'must be a valid URL' }, { exp: _regExp2.default.Id, msg: 'must be a valid alphanumeric ID' }]; | ||
var SimpleSchema = function () { | ||
@@ -98,43 +104,4 @@ function SimpleSchema() { | ||
// Define default validation error messages | ||
this.messageBox = new _messageBox2.default({ | ||
initialLanguage: 'en', | ||
messages: { | ||
en: { | ||
required: '{{label}} is required', | ||
minString: '{{label}} must be at least {{min}} characters', | ||
maxString: '{{label}} cannot exceed {{max}} characters', | ||
minNumber: '{{label}} must be at least {{min}}', | ||
maxNumber: '{{label}} cannot exceed {{max}}', | ||
minNumberExclusive: '{{label}} must be greater than {{min}}', | ||
maxNumberExclusive: '{{label}} must be less than {{max}}', | ||
minDate: '{{label}} must be on or after {{min}}', | ||
maxDate: '{{label}} cannot be after {{max}}', | ||
badDate: '{{label}} is not a valid date', | ||
minCount: 'You must specify at least {{minCount}} values', | ||
maxCount: 'You cannot specify more than {{maxCount}} values', | ||
noDecimal: '{{label}} must be an integer', | ||
notAllowed: '{{value}} is not an allowed value', | ||
expectedType: '{{label}} must be of type {{dataType}}', | ||
regEx: function regEx(_ref) { | ||
var label = _ref.label; | ||
var regExp = _ref.regExp; | ||
this.messageBox = new _messageBox2.default(_defaultMessages2.default); | ||
// See if there's one where exp matches this expression | ||
var msgObj = void 0; | ||
if (regExp) { | ||
msgObj = _underscore2.default.find(regExpMessages, function (o) { | ||
return o.exp && o.exp.toString() === regExp; | ||
}); | ||
} | ||
var regExpMessage = msgObj ? msgObj.msg : 'failed regular expression validation'; | ||
return label + ' ' + regExpMessage; | ||
}, | ||
keyNotInSchema: '{{name}} is not allowed by the schema' | ||
} | ||
} | ||
}); | ||
// Schema-level defaults for cleaning | ||
@@ -304,6 +271,6 @@ this._cleanOptions = _extends({ | ||
if (!(typeDef.type instanceof SimpleSchema)) return; | ||
result = result.concat(typeDef.type.autoValueFunctions().map(function (_ref2) { | ||
var func = _ref2.func; | ||
var fieldName = _ref2.fieldName; | ||
var closestSubschemaFieldName = _ref2.closestSubschemaFieldName; | ||
result = result.concat(typeDef.type.autoValueFunctions().map(function (_ref) { | ||
var func = _ref.func; | ||
var fieldName = _ref.fieldName; | ||
var closestSubschemaFieldName = _ref.closestSubschemaFieldName; | ||
@@ -416,3 +383,4 @@ return { | ||
value: function objectKeys(keyPrefix) { | ||
return keyPrefix ? this._objectKeys[keyPrefix + '.'] || [] : this._firstLevelSchemaKeys; | ||
if (!keyPrefix) return this._firstLevelSchemaKeys; | ||
return this._objectKeys[keyPrefix + '.'] || []; | ||
} | ||
@@ -424,3 +392,3 @@ | ||
* @param {SimpleSchema|Object} schema | ||
* @returns undefined | ||
* @returns The SimpleSchema instance (chainable) | ||
*/ | ||
@@ -478,11 +446,31 @@ | ||
if (definition.autoValue) _this5._autoValues[fieldName] = definition.autoValue; | ||
}); | ||
// Store child keys keyed by parent | ||
if (fieldName.indexOf('.') > -1 && fieldName.slice(-2) !== '.$') { | ||
var parentKey = fieldName.slice(0, fieldName.lastIndexOf('.')); | ||
var parentKeyWithDot = parentKey + '.'; | ||
_this5._objectKeys[parentKeyWithDot] = _this5._objectKeys[parentKeyWithDot] || []; | ||
_this5._objectKeys[parentKeyWithDot].push(fieldName.slice(fieldName.lastIndexOf('.') + 1)); | ||
} | ||
}); | ||
// Store child keys keyed by parent. This needs to be done recursively to handle | ||
// subschemas. | ||
var setObjectKeys = function setObjectKeys(curSchema, schemaParentKey) { | ||
_underscore2.default.each(curSchema, function (definition, fieldName) { | ||
fieldName = schemaParentKey ? schemaParentKey + '.' + fieldName : fieldName; | ||
if (fieldName.indexOf('.') > -1 && fieldName.slice(-2) !== '.$') { | ||
var parentKey = fieldName.slice(0, fieldName.lastIndexOf('.')); | ||
var parentKeyWithDot = parentKey + '.'; | ||
_this5._objectKeys[parentKeyWithDot] = _this5._objectKeys[parentKeyWithDot] || []; | ||
_this5._objectKeys[parentKeyWithDot].push(fieldName.slice(fieldName.lastIndexOf('.') + 1)); | ||
} | ||
// If the current field is a nested SimpleSchema, | ||
// iterate over the child fields and cache their properties as well | ||
definition.type.definitions.forEach(function (_ref2) { | ||
var type = _ref2.type; | ||
if (type instanceof SimpleSchema) { | ||
setObjectKeys(type._schema, fieldName); | ||
} | ||
}); | ||
}); | ||
}; | ||
setObjectKeys(this._schema); | ||
return this; | ||
} | ||
@@ -771,2 +759,7 @@ }, { | ||
SimpleSchema.ValidationContext = _ValidationContext2.default; | ||
SimpleSchema.setDefaultMessages = function (messages) { | ||
(0, _deepExtend2.default)(_defaultMessages2.default, messages); | ||
}; | ||
function mergeSchemas(schemas) { | ||
@@ -798,3 +791,2 @@ var mergedSchema = {}; | ||
if (!(def.type instanceof SimpleSchema)) return; | ||
console.log(key, def.type); | ||
@@ -801,0 +793,0 @@ _underscore2.default.each(def.type._schema, function (subVal, subKey) { |
@@ -225,3 +225,4 @@ 'use strict'; | ||
obj: obj, | ||
schema: this._simpleSchema | ||
schema: this._simpleSchema, | ||
validationContext: this | ||
}); | ||
@@ -228,0 +229,0 @@ |
{ | ||
"name": "simpl-schema", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A schema validation package that supports direct validation of MongoDB update modifier objects.", | ||
@@ -30,5 +30,4 @@ "author": "Eric Dobbertin <aldeed@gmail.com>", | ||
"dependencies": { | ||
"babel-polyfill": "^6.9.1", | ||
"clone": "^1.0.2", | ||
"message-box": "0.0.1", | ||
"message-box": "0.0.2", | ||
"mongo-object": "0.0.1", | ||
@@ -35,0 +34,0 @@ "underscore": "^1.8.3" |
@@ -93,3 +93,3 @@ # simple-schema | ||
If you are migrating from the Meteor package, refer to the [CHANGELOG](https://github.com/aldeed/meteor-simple-schema/blob/devel/CHANGELOG.md#200) | ||
If you are migrating from the Meteor package, refer to the [CHANGELOG](https://github.com/aldeed/meteor-simple-schema/blob/v2/CHANGELOG.md#200) | ||
@@ -104,2 +104,4 @@ ## Installation | ||
NOTE: You may also need to load the `babel-polyfill` package if you get any errors related to missing ES6 functions in certain browsers. | ||
## Lingo | ||
@@ -202,2 +204,6 @@ | ||
TO DO | ||
### Set Default Cleaning Options | ||
```js | ||
@@ -579,3 +585,4 @@ import SimpleSchema from 'simpl-schema'; | ||
The `SimpleSchema.RegEx` object defines standard regular expressions you can use as the value for the `regEx` key. | ||
- `SimpleSchema.RegEx.Email` for emails (uses a permissive regEx recommended by W3C, which most browsers use) | ||
- `SimpleSchema.RegEx.Email` for emails (uses a permissive regEx recommended by W3C, which most browsers use. Does not require a TLD) | ||
- `SimpleSchema.RegEx.EmailWithTLD` for emails that must have the TLD portion (.com, etc.). Emails like `me@localhost` and `me@192.168.1.1` won't pass this one. | ||
- `SimpleSchema.RegEx.Domain` for external domains and the domain only (requires a tld like `.com`) | ||
@@ -589,2 +596,3 @@ - `SimpleSchema.RegEx.WeakDomain` for less strict domains and IPv4 and IPv6 | ||
- `SimpleSchema.RegEx.ZipCode` for 5- and 9-digit ZIP codes | ||
- `SimpleSchema.RegEx.Phone` for phone numbers (taken from Google's libphonenumber library) | ||
@@ -723,3 +731,3 @@ ### blackbox | ||
- Call `SimpleSchema.validate(obj, schema, options)` static function as a shortcut for `mySimpleSchema.validate` if you don't want to create `mySimpleSchema` first. The `schema` argument can be just the schema object, in which case it will be passed to the `SimpleSchema` constructor for you. This is like `check(obj, schema)` but without the `check` dependency and with the ability to pass full schema error details back to a callback on the client. | ||
- Call `mySimpleSchema.validator()` to get a function that calls `mySimpleSchema.validate` for whatever object is passed to it. This means you can do `validate: mySimpleSchema.validator()` in the [mdg:method](https://github.com/meteor/method) package. | ||
- Call `mySimpleSchema.validator()` to get a function that calls `mySimpleSchema.validate` for whatever object is passed to it. This means you can do `validate: mySimpleSchema.validator()` in the [mdg:validated-method](https://github.com/meteor/validated-method) package. | ||
@@ -781,2 +789,3 @@ #### Customize the Error That is Thrown | ||
* `operator`: The Mongo operator for which we're doing validation. Might be `null`. | ||
* `validationContext`: The current `ValidationContext` instance | ||
* `field()`: Use this method to get information about other fields. Pass a field name (non-generic schema key) as the only argument. The return object will have `isSet`, `value`, and `operator` properties for that field. | ||
@@ -850,7 +859,7 @@ * `siblingField()`: Use this method to get information about other fields that have the same parent object. Works the same way as `field()`. This is helpful when you use sub-schemas or when you're dealing with arrays of objects. | ||
unique: true, | ||
custom: function () { | ||
custom() { | ||
if (Meteor.isClient && this.isSet) { | ||
Meteor.call("accountsIsUsernameAvailable", this.value, function (error, result) { | ||
Meteor.call("accountsIsUsernameAvailable", this.value, (error, result) => { | ||
if (!result) { | ||
Meteor.users.simpleSchema().namedContext("createUserForm").addValidationErrors([{ | ||
this.validationContext.addValidationErrors([{ | ||
name: "username", | ||
@@ -857,0 +866,0 @@ type: "notUnique" |
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
142431
4
23
2145
1049
+ Addedmessage-box@0.0.2(transitive)
- Removedbabel-polyfill@^6.9.1
- Removedbabel-polyfill@6.26.0(transitive)
- Removedbabel-runtime@6.26.0(transitive)
- Removedcore-js@2.6.12(transitive)
- Removedmessage-box@0.0.1(transitive)
- Removedregenerator-runtime@0.10.50.11.1(transitive)
Updatedmessage-box@0.0.2