Comments (5)
The simple-schema package doesn't have this because the assumption is that you would set default values on the object before validating it, but there is development underway to add an autoValue
schema option for the collection2 package. This function could be used to set a default value. See Meteor-Community-Packages/meteor-collection2#6 and Meteor-Community-Packages/meteor-collection2#9.
from meteor-simple-schema.
The question is: if I define a autovalue
field in my schema and the user pass a document with a field with the same name, should we:
- overwrite the autovalue?
- overwrite the user field?
- return a validation error?
The first purpose of autoValue
fields was to create createdAt
and updatedAt
automatically populated on insert/update. Of course there are other use cases, but I don't think that a default
value is one of them. I don't want a user to be able to overwrite createdAt
, updatedAt
, nbUpdates
or randomId
field (see the implementation of those fields using autovalue).
So we need another option in the schema definition to handle the default value use case:
- One solution is to create a
defaultValue
function which works the same thatautoValue
but can be overwritten. - The other possibility is to add a
overwritable
boolean, default tofalse
, that works with theautoValue
field
Thoughts?
from meteor-simple-schema.
My thinking was that if a user passed in a document without a field that had an autoValue
or defaultValue
set it would insert that document with the field's autovalue.
So for example, if I was tracking the number of comments on a post and a new post was created, the comments field would default to 0 rather than undefined. Of course, there are many ways around this such as setting { comments: 0 }
in the object before it was passed to the collection like @aldeed mentioned, which might even make more sense. So by all means, don't feel like this should be implemented if it shouldn't be part of the schema.
If something is implemented in the future though, I'm in favor for a defaultValue
function that would be overwritten if a value was passed in. So that way, like you said, the createdAt
etc fields would still be locked.
from meteor-simple-schema.
The autoValue
function receives the whole document so I thought maybe users could do the checking in there and return the user-supplied value if they don't want to overwrite the value. So:
- If you want to force a value regardless of what the user might enter, make sure
autoValue
returns the value you want. - If you want to define a default value using
autoValue
, first check whether the value is already set. If so, return that value (or undefined), otherwise return the default value. We could also pass the user-supplied value as an argument so that eachautoValue
function won't have to individually implement the code to pull the value out ofdoc
.
Then we would always overwrite unless they returned undefined.
from meteor-simple-schema.
Closing this since it's being addressed in collection2 package.
from meteor-simple-schema.
Related Issues (20)
- [solved] custom function validation error HOT 1
- autoValue: calling fields() and siblingFields() yields no results on insert HOT 2
- Overwriting SimpleSchema regEx messages HOT 5
- Error: Invalid definition for inMenu field HOT 1
- Match.Where being instantiated as a class, causes error on Meteor 1.6.1 boot HOT 5
- Make a field conditionally required bug HOT 1
- Explicitly indicate this repo is deprecated HOT 1
- How to define a Custom Validation Error in Autoform >= 6.0 HOT 1
- Removing old field from old users? Ignore schema when using $unset ? HOT 1
- typo in dependencies in version 3.0.0 HOT 1
- 1.6.2 Has removed Deps from Tracker package HOT 4
- Warn / strip global regEx schemas? HOT 1
- the optional: true afFieldInput have the required attribute with it HOT 1
- error in Documentation [max] > {{max}} HOT 1
- "must be an object" error even though typeof() returns Object HOT 3
- $pull not behaving as expected HOT 1
- Move this package to Community Packages to support simpl-schema >=3.x HOT 1
- Update autoValue to accept async function HOT 2
- Typescript error when importing SimpleSchema HOT 6
- Using Collection Hook's direct version breaks defaultValue and autoValue on an insert HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from meteor-simple-schema.