Meteor-Community-Packages / meteor-simple-schema

Meteor integration package for simpl-schema
https://github.com/Meteor-Community-Packages/meteor-simple-schema
MIT License
920 stars 162 forks source link

What is the correct way to validate() a schema containing an AutoValue key? #714

Closed Bandit closed 6 years ago

Bandit commented 7 years ago

Every time I validate(), it throws an error because the AutoValue key doesn't exist (because the client doesn't run AutoValue functions I'm guessing). Is the correct way to deal with this to add an optional: true flag?

Relevant closed issue: https://github.com/aldeed/meteor-autoform/issues/1218

Paul-Vandell commented 7 years ago

Hey Bandit, Can you show us your simpl-schema ?

Bandit commented 7 years ago

Here's an example schema showing my current approach to this problem

Items.schema = new SimpleSchema({
  _id: {
    type: String,
    label: 'The ID of this item',
    optional: true,
    custom() {
      // only required on an update
      if (!this.isSet && this.operator === '$set') return 'required';
    },
  },
  createdAt: {
    type: String,
    label: 'The date this item was created',
    optional: true,
    autoValue() {
      if (this.isInsert) return (new Date()).toISOString();
    },
  },
  updatedAt: {
    type: String,
    label: 'The date this item was last updated',
    optional: true,
    autoValue() {
      if (this.isInsert || this.isUpdate) return (new Date()).toISOString();
    },
  },
  [...]
});
ziedmahdi commented 6 years ago

try running ss.clean frist then ss.validate.

Items.schema.clean(item);
Items.schema.validate(item);