Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic support for save and get raw definition #395

Merged
merged 6 commits into from Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions package/lib/SimpleSchema.js
Expand Up @@ -57,6 +57,7 @@ class SimpleSchema {
humanizeAutoLabels = true,
requiredByDefault = true,
tracker,
keepRawDefinition = false,
} = {}) {
// Stash the options object
this._constructorOptions = {
Expand Down Expand Up @@ -91,12 +92,22 @@ class SimpleSchema {
this._depsLabels = {};
this.extend(schema);

// Clone raw definition and save if keepRawDefinition is active
this._rawDefinition = 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
Expand Up @@ -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,
Expand Down