Using simple-schema, I'm having hard time realizing what's the correct way to use
default values in combination with
autoValue.
So I'm probably supposed to use some smarts in autoValue that will return a default value upon an insert, if the value is missing (and I'm using C2). That's at least my theory, but I'm unable to achieve this and I probably didn't find the correct combination of flags to use in autoValue. (flags such as this.isSet, this.isInsert etc).
My goal is for the field to have a default value, and if this field is set by a user, I want to sanitize it for HTML tags, so simply using defaultValue without autoValue will not do, I have to use autoValue (in order to modify and remove HTML tags).
What is the correct way to implement autoValue with the combination of defaultValue?
I tried many combinations, some of which did not work in the sense that the defaultValue wasn't applied to new documents, others did not work in the sense that all other fields in the document were empty (although they should have had values upon insert), except the ones that had a default value.
Here's different things I tried, which did not work (coffeescript).
defaultValue = 'xxx'
autoValue: (doc) ->
if @operator == null and @isSet and not @value
return defaultValue
if Meteor.isServer and @isSet
return sanitizeHtml(@value, sanitizeHtmlConf)
autoValue: (doc) ->
if defaultValue && @isInsert && (not @isSet || @value.length == 0)
return defaultValue
if Meteor.isServer and @isSet
return sanitizeHtml(@value, sanitizeHtmlConf)
autoValue: (doc) ->
if @isInsert and not @value
return defaultValue
if Meteor.isServer and @isSet
return sanitizeHtml(@value, sanitizeHtmlConf)
autoValue: (doc) ->
if Meteor.isServer and @isSet
return sanitizeHtml(@value, sanitizeHtmlConf)
else if not @value
return defaultValue
Thank you!