From 8cac62026a0f467929d99213b760af45f3ddae84 Mon Sep 17 00:00:00 2001 From: andy camacho Date: Sat, 18 Jul 2020 10:38:19 -0400 Subject: [PATCH 1/5] Add basic support for save and get raw definition --- package/lib/SimpleSchema.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/package/lib/SimpleSchema.js b/package/lib/SimpleSchema.js index 2a28647..1db4583 100644 --- a/package/lib/SimpleSchema.js +++ b/package/lib/SimpleSchema.js @@ -57,6 +57,7 @@ class SimpleSchema { humanizeAutoLabels = true, requiredByDefault = true, tracker, + keepRawDefinition = false, } = {}) { // Stash the options object this._constructorOptions = { @@ -91,12 +92,22 @@ class SimpleSchema { this._depsLabels = {}; this.extend(schema); + // Clone raw definition and save if keepRawDefinition is active + this._rawDefinition = keepRawDefinition && schema; + // 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); From 0947eeacf269a1381523f6f8951864cb266b81db Mon Sep 17 00:00:00 2001 From: andy camacho Date: Sat, 25 Jul 2020 11:31:26 -0400 Subject: [PATCH 2/5] Change constructor for save null if keepRawDefinition is false --- package/lib/SimpleSchema.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package/lib/SimpleSchema.js b/package/lib/SimpleSchema.js index 1db4583..de64c91 100644 --- a/package/lib/SimpleSchema.js +++ b/package/lib/SimpleSchema.js @@ -93,7 +93,7 @@ class SimpleSchema { this.extend(schema); // Clone raw definition and save if keepRawDefinition is active - this._rawDefinition = keepRawDefinition && schema; + this._rawDefinition = keepRawDefinition ? schema : null; // Define default validation error messages this.messageBox = new MessageBox(clone(defaultMessages)); From c3ba892a329af11b8b1b84380415fe3a66c90658 Mon Sep 17 00:00:00 2001 From: andy camacho Date: Sat, 25 Jul 2020 11:39:56 -0400 Subject: [PATCH 3/5] Add tests for new rawDefinition --- package/lib/SimpleSchema.tests.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/package/lib/SimpleSchema.tests.js b/package/lib/SimpleSchema.tests.js index e5aad7d..e51e8a7 100644 --- a/package/lib/SimpleSchema.tests.js +++ b/package/lib/SimpleSchema.tests.js @@ -1058,6 +1058,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, From 65292fb36a2c1b3403927670d7df2ae1d35cc5c9 Mon Sep 17 00:00:00 2001 From: andy camacho Date: Sat, 25 Jul 2020 11:49:59 -0400 Subject: [PATCH 4/5] Update readme file --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 9409272..003828e 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,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) @@ -581,6 +582,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. From 63e969e5a6b5a0eeababbb163f8c8d3e83dd0d1b Mon Sep 17 00:00:00 2001 From: andy camacho Date: Sat, 25 Jul 2020 11:52:30 -0400 Subject: [PATCH 5/5] Fix lint errors --- package/lib/SimpleSchema.tests.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package/lib/SimpleSchema.tests.js b/package/lib/SimpleSchema.tests.js index e51e8a7..030c09f 100644 --- a/package/lib/SimpleSchema.tests.js +++ b/package/lib/SimpleSchema.tests.js @@ -1060,7 +1060,7 @@ describe('SimpleSchema', function () { it('issue #390 - Should get null rawDefinition if keepRawDefiniton is false', function () { const foo = new SimpleSchema({ - foo: String + foo: String, }); expect(foo instanceof SimpleSchema).toBe(true); expect(foo.rawDefinition).toEqual(null); @@ -1068,7 +1068,7 @@ describe('SimpleSchema', function () { it('issue #390 - Should get rawDefinition if keepRawDefiniton is true', function () { const foo = new SimpleSchema({ - foo: String + foo: String, }, { keepRawDefinition: true }); expect(foo instanceof SimpleSchema).toBe(true); expect(foo.rawDefinition).toEqual({ foo: String });