Skip to content

Commit

Permalink
feat: add keepRawDefinition option (#395)
Browse files Browse the repository at this point in the history
Add basic support for save and get raw definition with a new keepRawDefinition option

Co-authored-by: andy camacho <[email protected]>
Co-authored-by: Eric Dobbertin <[email protected]>
  • Loading branch information
3 people authored Apr 8, 2021
1 parent 1a067c4 commit 3810591
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions package/lib/SimpleSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
16 changes: 16 additions & 0 deletions package/lib/SimpleSchema.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 3810591

Please sign in to comment.