Continuing from the jam:method thread as it’s true all my questions are schema related! ![:see_no_evil: :see_no_evil:](https://forums.meteor.com/images/emoji/twitter/see_no_evil.png?v=12)
I don’t have plans to support autovalues in jam:easy-schema
at this time. Like you said, I’m sort of against them. Having said that, can you give an example of how you use them and why you find them useful?
We have a base schema that we build other schemas upon which basically handles a few common variables. Here’s what it looks like. There are a handful of other places but as a general rule we try avoid ‘magic’ and things happening outside the main execution path.
import SimpleSchema from "simpl-schema";
SimpleSchema.extendOptions(["autoform"]);
export const BaseSchema = new SimpleSchema({
createdAt: {
type: Date,
optional: true,
autoValue: function() {
if (this.isInsert) {
return new Date();
} else if (this.isUpsert) {
return {$setOnInsert: new Date()};
} else {
//allow createdAt date to be set by migrations
//this.unset();
}
}
},
updatedAt: {
type: Date,
optional: true,
autoValue: function() {
//both updating and inserting should modifying the updated date.
if (this.isUpdate || this.isInsert || this.isUpsert) {
return new Date();
}
}
},
creatorId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
optional: true,
autoValue: function() {
if (this.isInsert) {
return this.userId;
} else if (this.isUpsert) {
return {$setOnInsert: this.userId};
} else {
this.unset();
}
}
},
updaterId: {
type: String,
regEx: SimpleSchema.RegEx.Id,
optional: true,
autoValue: function() {
if (this.isInsert || this.isUpdate || this.isUpsert) {
return this.userId;
}
}
},
deleted: {
type: Boolean,
optional: true
}
});
I haven’t really. My initial thought: I think these are so tied to the view layer you’re using that I think it’d be better to have someone write a package for their view layer that takes an Easy Schema as an input to magically generate a form.
Yes, but as you’re generating the jsonSchema that might be an easier path as a lot of form libs support that.
In general, I think migrating from SimpleSchema to Easy Schema should hopefully be relatively straightforward. It could also make your schemas a lot more straightforward to grok. Though it will depend on how many features of SimpleSchema you rely on. Easy Schema intentionally doesn’t have all the SimpleSchema features.
It would be cool maybe to have a comparison table in the repo? To jog the memory of long time users who have maybe forgotten what magic they are using from SS. I’m just thinking about the custom validation function now and that could be a problem. Do you have any solution to interdependent fields? So if type = x
then status is required
or that type of rule?
Hmm, not sure I’m following
. Which code are you referring to?
This code here
Easy Schema does a conversion to Mongo JSON Schema automatically so hopefully your use case is covered. Can you give an example of what you’re looking for?
As it looks like it might be easy to migrate SS → JsonSchema I was just wondering if having the ability to define EasySchemas in JsonSchema might make life easier. But yeah, that loses the brevity of easyschema and is probably quite a bit of work (particularly infering the check side of things from the jsonschema) so probably makes no sense for this package.