Simple Schema: How to set defaultvalue with autoValue

2,846 views
Skip to first unread message

Ran Tavory

unread,
Nov 20, 2014, 6:20:12 PM11/20/14
to meteo...@googlegroups.com
Using simple-schema, I'm having hard time realizing what's the correct way to use default values in combination with autoValue
A field cannot have both defaultValue and autoValue, that's OK, it makes sense (https://github.com/aldeed/meteor-simple-schema/blob/master/simple-schema.js#L73)

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!

Daniel Dornhardt - Daniel Dornhardt Development

unread,
Nov 20, 2014, 6:53:18 PM11/20/14
to meteo...@googlegroups.com
Hi, I found autovalues confusing at first too.

Actually that's still the case :)

But the collection2 - documentation luckily has some examples to steal, I mean learn from:


- there's examples for a createdAt and updatedAt - dates and more examples. Maybe one of those is close to what you need?

Actually, be careful, because some of the features are only available in connection with a collection2 - database - operation. But maybe those examples can helpful.

Best wishes and good night! :)

Daniel Dornhardt, Daniel Dornhardt Development
+49 152 - 56 17 22 61

--
You received this message because you are subscribed to the Google Groups "meteor-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to meteor-talk...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/meteor-talk/00b4a0e6-0531-4e37-a859-2c66e904c675%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ran Tavory

unread,
Nov 21, 2014, 2:10:58 AM11/21/14
to meteo...@googlegroups.com, dan...@dornhardt.com
Thanks Daniel, I looked at those examples, I learned a lot from them but I still can't make my use case work :(

It seems that each insert triggers the autoValue function twice, first time with the this.value set this.isSet == true  (if the value is indeed set during the insert), but then another time, now this.value == undefined and this.isSet == false. 
I don't know if this behavior is by design, but it sure makes it difficult to use autoValue with defaultValue for when isInsert == true but value == undefined. 

Anyone else had successfully used the combination of autoValue with default values for inserts? 

thanks. 
Reply all
Reply to author
Forward
0 new messages