-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Feature request : default values and required for object fields #3587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
one way to achieve this is through a I could see an argument for adding the ability to set validation for a class in the dashboard obviating the need for doing it programatically. |
The beforeSave hook is what I'm using now. But it much more elegant to just be able to set it directly in the dashboard. And it saves many lines of repeated code throughout the classes. |
I agree with @AmbroiseCollon . If Schema has required field option, it saves many boring codes. :) |
@AmbroiseCollon , @yomybaby any one of you wanting to get started on that feature? |
Any news on this? Just wanted to know before starting to code the beforeSave from my app. |
I have a library called static beforeSave(ParseConstraints, req, res) {
var parseConstraints = new ParseConstraints(req, res, 'Fund');
return parseConstraints.existsKeys([
'fundName'
])
.then(() => parseConstraints.defaults({
'fundManagers': [],
'fundClosingDates': []
}))
.then(() => {
/// .....
})
.then(() => {
return res.success();
}).catch((err) => {
res.error(err);
});
} Each This is what the basic code for /* parseConstraints.js */
var DB = function(req, res, className) {
this.req = req;
this.res = res;
this.className = className;
};
// The included keys must exist for the object to be saved
DB.prototype.existsKeys = function(keys) {
// eslint-disable-next-line
var p = new Parse.Promise();
var object = this.req.object;
keys = _toArray(keys);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var value = object.get(key);
// The empty string does not count as 'existing'
if (value === undefined || value === null || (typeof value === 'string' && value.length === 0)) {
this.res.error('existsKeys Error: A `' + this.className + '` requires a value for key `' + key + '`');
p.reject(key);
return p;
}
}
p.resolve(object);
return p;
}
// Pass in an object with { key: value } for each field and its default value
DB.prototype.defaults = function(defaults) {
// eslint-disable-next-line
var p = new Parse.Promise();
var object = this.req.object;
var keys = Object.keys(defaults);
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var value = object.get(key);
if (value === undefined || value === null) {
object.set(key, defaults[key]);
}
}
p.resolve(object);
return p;
};
module.exports = DB; I might be interested in taking on this feature. My solution would be to define required fields in the schema, preferably complying with the official jsonschema docs. Then perform validation just before the beforeSave trigger, thus emulating what I do with the library. Open to any thoughts on this |
The label |
Inspired from meteor.js technology, it could be a really valuable addition to add two properties to object fields :
A field with a default value should be required. It would save a lot of dumb code in before and after save method.
Process could be like :
a. if the object has already a value for the field => OK
b. if the object has no value for the field :
i. if field is not required => OK
ii. if field is required :
- if field has a default value => fill field with default value
- if field has no default value => Error : field cannot be undefined
Maybe the easiest way to do this is to create meta collection aside from actual collection that owns all this required / default value data. But this is where my understanding of Parse Server behind the scenes stops.
What do you think ?
The text was updated successfully, but these errors were encountered: