simpl-schema
Advanced tools
Comparing version 0.1.1 to 0.2.0
# simpl-schema CHANGELOG | ||
## 0.2.0 | ||
- Added `ssInstance.getQuickTypeForKey(key)` | ||
- Added `ssInstance.getObjectSchema(key)` | ||
## 0.1.1 | ||
- Improved error for missing `type` property | ||
@@ -5,0 +11,0 @@ - Use _.contains instead of Array.includes to fix some compatibility issues (thanks @DerekTBrown) |
@@ -238,2 +238,74 @@ 'use strict'; | ||
/** | ||
* Returns a string identifying the best guess data type for a key. For keys | ||
* that allow multiple types, the first type is used. This can be useful for | ||
* building forms. | ||
* | ||
* @param {String} key Generic or specific schema key | ||
* @returns {String} A type string. One of: | ||
* string, number, boolean, date, stringArray, numberArray, booleanArray, | ||
* dateArray, object | ||
*/ | ||
}, { | ||
key: 'getQuickTypeForKey', | ||
value: function getQuickTypeForKey(key) { | ||
var type = void 0; | ||
var fieldSchema = this.schema(key); | ||
if (!fieldSchema) return; | ||
var fieldType = fieldSchema.type.singleType; | ||
if (fieldType === String) { | ||
type = 'string'; | ||
} else if (fieldType === Number || fieldType === SimpleSchema.Integer) { | ||
type = 'number'; | ||
} else if (fieldType === Boolean) { | ||
type = 'boolean'; | ||
} else if (fieldType === Date) { | ||
type = 'date'; | ||
} else if (fieldType === Array) { | ||
var arrayItemFieldSchema = this.schema(key + '.$'); | ||
if (!arrayItemFieldSchema) return; | ||
var arrayItemFieldType = arrayItemFieldSchema.type.singleType; | ||
if (arrayItemFieldType === String) { | ||
type = 'stringArray'; | ||
} else if (arrayItemFieldType === Number || arrayItemFieldType === SimpleSchema.Integer) { | ||
type = 'numberArray'; | ||
} else if (arrayItemFieldType === Boolean) { | ||
type = 'booleanArray'; | ||
} else if (arrayItemFieldType === Date) { | ||
type = 'dateArray'; | ||
} | ||
} else if (fieldType === Object) { | ||
type = 'object'; | ||
} | ||
return type; | ||
} | ||
/** | ||
* Given a key that is an Object, returns a new SimpleSchema instance scoped to that object. | ||
* | ||
* @param {String} key Generic or specific schema key | ||
*/ | ||
}, { | ||
key: 'getObjectSchema', | ||
value: function getObjectSchema(key) { | ||
var newSchemaDef = {}; | ||
var genericKey = _mongoObject2.default.makeKeyGeneric(key); | ||
var searchString = genericKey + '.'; | ||
_underscore2.default.each(this.mergedSchema(), function (val, k) { | ||
if (k.indexOf(searchString) === 0) { | ||
newSchemaDef[k.slice(searchString.length)] = val; | ||
} | ||
}); | ||
return new SimpleSchema(newSchemaDef); | ||
} | ||
// Returns an array of all the autovalue functions, including those in subschemas all the | ||
@@ -240,0 +312,0 @@ // way down the schema tree |
@@ -7,2 +7,4 @@ 'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
var _mongoObject = require('mongo-object'); | ||
@@ -16,25 +18,36 @@ | ||
var SimpleSchemaGroup = function SimpleSchemaGroup() { | ||
_classCallCheck(this, SimpleSchemaGroup); | ||
var SimpleSchemaGroup = function () { | ||
function SimpleSchemaGroup() { | ||
_classCallCheck(this, SimpleSchemaGroup); | ||
for (var _len = arguments.length, definitions = Array(_len), _key = 0; _key < _len; _key++) { | ||
definitions[_key] = arguments[_key]; | ||
} | ||
for (var _len = arguments.length, definitions = Array(_len), _key = 0; _key < _len; _key++) { | ||
definitions[_key] = arguments[_key]; | ||
} | ||
this.definitions = definitions.map(function (definition) { | ||
if (_mongoObject2.default.isBasicObject(definition)) return definition; | ||
this.definitions = definitions.map(function (definition) { | ||
if (_mongoObject2.default.isBasicObject(definition)) return definition; | ||
if (definition instanceof RegExp) { | ||
if (definition instanceof RegExp) { | ||
return { | ||
type: String, | ||
regEx: definition | ||
}; | ||
} | ||
return { | ||
type: String, | ||
regEx: definition | ||
type: definition | ||
}; | ||
}); | ||
} | ||
_createClass(SimpleSchemaGroup, [{ | ||
key: 'singleType', | ||
get: function get() { | ||
return this.definitions[0].type; | ||
} | ||
}]); | ||
return { | ||
type: definition | ||
}; | ||
}); | ||
}; | ||
return SimpleSchemaGroup; | ||
}(); | ||
exports.default = SimpleSchemaGroup; |
{ | ||
"name": "simpl-schema", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "A schema validation package that supports direct validation of MongoDB update modifier objects.", | ||
@@ -5,0 +5,0 @@ "author": "Eric Dobbertin <aldeed@gmail.com>", |
@@ -200,2 +200,9 @@ # simple-schema | ||
Passing in `Tracker` causes the following functions to become reactive: | ||
- ValidationContext#keyIsInvalid | ||
- ValidationContext#keyErrorMessage | ||
- ValidationContext#isValid | ||
- ValidationContext#validationErrors | ||
- SimpleSchema#label | ||
### Automatically Clean the Object Before Validating It | ||
@@ -447,4 +454,6 @@ | ||
Sometimes you have one large SimpleSchema object, and you need just a subset of it for some purpose. To pull out certain schema keys into a new schema, you can use the `pick` method: | ||
Sometimes you have one large SimpleSchema object, and you need just a subset of it for some purpose. | ||
To pull out certain schema keys into a new schema, you can use the `pick` method: | ||
```js | ||
@@ -462,2 +471,44 @@ import SimpleSchema from 'simpl-schema'; | ||
To keep all but certain keys in a new schema, you can use the `omit` method: | ||
```js | ||
import SimpleSchema from 'simpl-schema'; | ||
const schema = new SimpleSchema({ | ||
firstName: String, | ||
lastName: String, | ||
username: String, | ||
}); | ||
const nameSchema = schema.omit('username'); | ||
``` | ||
To pull a subschema out of an `Object` key in a larger schema, you can use `getObjectSchema`: | ||
```js | ||
import SimpleSchema from 'simpl-schema'; | ||
const schema = new SimpleSchema({ | ||
firstName: String, | ||
lastName: String, | ||
address: Object, | ||
'address.street1': String, | ||
'address.street2': { type: String, optional: true }, | ||
'address.city': String, | ||
'address.state': String, | ||
'address.postalCode': String, | ||
}); | ||
const addressSchema = schema.getObjectSchema('address'); | ||
// addressSchema is now the same as this: | ||
// new SimpleSchema({ | ||
// street1: String, | ||
// street2: { type: String, optional: true }, | ||
// city: String, | ||
// state: String, | ||
// postalCode: String, | ||
// }); | ||
``` | ||
## Schema Keys | ||
@@ -464,0 +515,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
148765
2227
1106