diff --git a/README.md b/README.md index 47ae458..1268002 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,7 @@ There are also reasons not to choose this package. Because of all it does, this - [Overriding When Extending](#overriding-when-extending) - [Subschemas](#subschemas) - [Extracting Schemas](#extracting-schemas) + - [Raw Definition](#raw-definition) - [Schema Keys](#schema-keys) - [Schema Rules](#schema-rules) - [type](#type) @@ -609,6 +610,23 @@ const addressSchema = schema.getObjectSchema('address'); // }); ``` +### Raw Definition + +Sometimes if you want to get the `rawDefinition` of some schema just pass in the options `{ keepRawDefinition: true}`(if not arg is passed the value will be null). Example: +```javascript + const userSchema = new SimpleSchema({ + name: String, + number: 'SimpleSchema.Integer', + email: String +}, { keepRawDefintion: true }); +userSchema.rawDefinition; +//{ +// name: String, +// number: 'SimpleSchema.Integer', +// email: String +//} +``` + ## Schema Keys A basic schema key is just the name of the key (property) to expect in the objects that will be validated. diff --git a/package/lib/SimpleSchema.js b/package/lib/SimpleSchema.js index a36aace..fa3d312 100644 --- a/package/lib/SimpleSchema.js +++ b/package/lib/SimpleSchema.js @@ -76,12 +76,22 @@ class SimpleSchema { this._depsLabels = {}; this.extend(schema); + // Clone raw definition and save if keepRawDefinition is active + this._rawDefinition = this._constructorOptions.keepRawDefinition ? schema : null; + // Define default validation error messages this.messageBox = new MessageBox(clone(defaultMessages)); this.version = SimpleSchema.version; } + /** + /* @returns {Object} The entire raw schema definition passed in the constructor + */ + get rawDefinition() { + return this._rawDefinition; + } + forEachAncestorSimpleSchema(key, func) { const genericKey = MongoObject.makeKeyGeneric(key); diff --git a/package/lib/SimpleSchema.tests.js b/package/lib/SimpleSchema.tests.js index ae6c14a..445de87 100644 --- a/package/lib/SimpleSchema.tests.js +++ b/package/lib/SimpleSchema.tests.js @@ -1144,6 +1144,22 @@ describe('SimpleSchema', function () { expect(foo instanceof SimpleSchema).toBe(true); }); + it('issue #390 - Should get null rawDefinition if keepRawDefiniton is false', function () { + const foo = new SimpleSchema({ + foo: String, + }); + expect(foo instanceof SimpleSchema).toBe(true); + expect(foo.rawDefinition).toEqual(null); + }); + + it('issue #390 - Should get rawDefinition if keepRawDefiniton is true', function () { + const foo = new SimpleSchema({ + foo: String, + }, { keepRawDefinition: true }); + expect(foo instanceof SimpleSchema).toBe(true); + expect(foo.rawDefinition).toEqual({ foo: String }); + }); + describe('SimpleSchema.Any', function () { const schema = new SimpleSchema({ testAny: SimpleSchema.Any,