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

Allowing custom properties #44

Closed
matteodem opened this issue Jan 10, 2014 · 10 comments
Closed

Allowing custom properties #44

matteodem opened this issue Jan 10, 2014 · 10 comments

Comments

@matteodem
Copy link

What do you think of allowing custom properties to let other packages have the possibility to enhance the Simple Schema with additional information? As an example:

var schema = new SimpleSchema({
    'image' : {
        'type' : String,
        'otherPackage' : {
            'animated' : true
        }
    }
});

I'd also be okay with only one property called "custom" or something where one can define such informations.

@aldeed
Copy link
Collaborator

aldeed commented Jan 10, 2014

Good question. A few points:

  • This is possible using SimpleSchema.extendOptions and mySimpleSchema.validator. These are undocumented, but collection2 uses them to add unique, autoValue, denyInsert, and denyUpdate (and the upcoming index option). Note that validator adds a custom validator to the SimpleSchema instance rather than to all. This is necessary for one of the validations that collection2 does, but in general maybe there should be a SimpleSchema.addValidator or SimpleSchema.addKeyValidator that adds support to all SS instances. I think this is what you're asking for.
  • A related idea on my list is to add a custom option that takes a function or array of functions. The functions would return either true or an error type string (same as how mySimpleSchema.validator works now, but user-definable per field).

You can keep this open as a reminder to make these changes.

@aldeed
Copy link
Collaborator

aldeed commented Jan 10, 2014

Also the custom option would replace valueIsAllowed.

@matteodem
Copy link
Author

Thanks for letting me know! I'll have a closer look at the source code then.

@serkandurusoy
Copy link

I have hit on a requirement that in fact seems very related to this. Here's a discusson thread that's related, and below is a simplified example of what I'd like available:

BaseSchema = new SimpleSchema({
  name: {
    type: String,
    min: 3
  },
  middleName: {
    type: String,
    optional: true
  },
  lastName: {
    type: String,
    min: 3
  }
});

ExtendedSchema = new SimpleSchema([
  BaseSchema,
  {
    name: {
      min: 5,
      max: 10
    },
    middleName: {
      optional: false
    },
    age: {
      type: Number,
      min: 1,
      max: 100
    }
  }
]);

As you can see, I'd like the ability to override or add to the properties of another schema that's included into this one.

@aldeed
Copy link
Collaborator

aldeed commented Jan 22, 2014

Yep, we'd just need to _.extend the individual field definitions rather than the schema object as a whole. It's actually related to Meteor-Community-Packages/meteor-collection2#44.

@serkandurusoy
Copy link

Hmm, apart from the users collection enhancement, I don't see a specific reason to do this on collection2. I feel, keeping this on simple-schema on the other hand, would also empower those who don't use collection2.

Also contextually speaking, I believe rules about schema validation should reside in the schema object, rather than the collection object.

@aldeed
Copy link
Collaborator

aldeed commented Jan 22, 2014

Agreed, it would be done on SS, but used by C2 for Meteor.users. I might do the development later today since it should be relatively easy.

@serkandurusoy
Copy link

$('<button/>', {text: 'Like :)'}).appendTo(this).click();

@aldeed
Copy link
Collaborator

aldeed commented Jan 22, 2014

@serkandurusoy, you should now be able to merge/extend schema fields in the way you describe. @matteodem, take a look at Custom Validation. This should address your original issue (and more!).

@aldeed aldeed closed this as completed Jan 22, 2014
@serkandurusoy
Copy link

Great, thanks. I'll dive right in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants